Skip to main content
Commonmark migration
Source Link

If (and only if) your language is unable to accept any kind of user input, you may hardcode the input in your program.

 

Output has to be written to STDOUT or closest alternative.

If (and only if) your language is unable to accept any kind of user input, you may hardcode the input in your program.

 

Output has to be written to STDOUT or closest alternative.

If (and only if) your language is unable to accept any kind of user input, you may hardcode the input in your program.

Output has to be written to STDOUT or closest alternative.

Source Link

GHC 8.2.2, 716 bytes

{-#LANGUAGE FlexibleInstances,FunctionalDependencies,UndecidableInstances,CPP#-} #define i instance #define c class data F data T data S n data C x s c A p q r|p q->r i A F q F i A T q q c E p x y z|p x y->z i E F x y y i E T x y x c G x y b|x y->b i G x F T i G F(S y)F i G x y b=>G(S x)(S y)b c M x y z|x y->z i M x F x i M F(S y)F i M x y z=>M(S x)(S y)z c D x y b|x y->b i D x F T i(G(S y)x p,M(S y)x z,D x z q,A p q b)=>D x(S y)b c K f x y|f x->y i D m n b=>K(S n)m b c I f s n|f s->n i I f F F i(K f x b,I f s m,E b(S m)m n)=>I f(C x s)n c R n s|n->s i R F F i R n s=>R(S n)(C(S n)s) c Q m n b|m n->b i Q F F T i Q F(S y)F i Q(S x)F F i Q x y b=>Q(S x)(S y)b c P n b|n->b i(R n s,I(S n)s m,Q m(S(S F))b)=>P n b 

Try it online!

This has got to be the most beautiful code I've ever written. It's a compile-time metaprogram that determines whether a type (represented Peano-style, where F is zero and S n is n's successor) is prime.

This works in GHC 8.2.2, the version TIO currently uses. It works locally in 8.8.3 as well. I have included the language extensions in the code, all except one—the TIO example linked above has MonoLocalBinds to suppress a warning. The program works just fine without them.

If (and only if) your language is unable to accept any kind of user input, you may hardcode the input in your program.

Output has to be written to STDOUT or closest alternative.

As far as I know, metaprograms like this have no means of I/O. With FunctionalDependencies, typeclasses can act much like functions, and in this way, the typeclass P is a "function" from natural numbers to booleans (T and F—yes, the same type represents zero, false, and the empty list). I believe this to be the closest alternative to the standard streams.

The test suite I provided covers numbers from one to twenty. Try changing any T to F or vice-versa, and the program will fail to compile.

I know there are improvements that can be made, and I'll be updating this answer soonish with them.