While reading https://en.uncyclopedia.co/wiki/Haskell (and ignoring all the “offensive” stuff), I stumbled upon the following piece of obfuscated code:
fix$(<$>)<$>(:)<*>((<$>((:[{- thor's mother -}])<$>))(=<<)<$>(*)<$>(*2))$1
When I run that piece of code in ghci (after importing Data.Function and Control.Applicative), ghci prints the list of all powers of 2.
How does this piece of code work?
To begin with, we have the lovely definition
which by itself is a bit mind-bending if you’ve never seen it before. Anyway it’s a fairly standard trick of laziness and recursion. Now, we’ll get rid of the explicit recursion using
fix, and point-free-ify.The next thing we’re going to do is expand the
:section and make themapneedlessly complex.Well, now we have two copies of that constant
1. That will never do, so we’ll use the reader applicative to de-duplicate that. Also, function composition is a bit rubbish, so let’s replace that with(<$>)wherever we can.Next up: that call to
mapis much too readable. But there’s nothing to fear: we can use the monad laws to expand it a bit. In particular,fmap f x = x >>= return . f, soWe can point-free-ify, replace
(.)with(<$>), and then add some spurious sections:Substituting this equation in our previous step:
Finally, you break your spacebar and produce the wonderful final equation