Quine in Haskell

I've constructed a neat little quine in Haskell, the popular research language we all adore. ;-) It's so short, I could have pasted it right into the title! :-D

putStr(p++show p)where p="putStr(p++show p)where p="


That's what I like about Haskell: the solutions you write in it are usually elegant, conscious and close to the problem space in representation. You read the above source as follows: print out the program accompanied by a quoted (shown) version of the same, where the program is just what I said. Can you put it any simpler than that?!

I've first started on a traditional route by introducing constants for backslash, quotation mark and all that, until I've realized how foolish I was. As you may have already known, Haskell has a handy function called 'show' that does all quoting for you. I've been using that all over the place if you have read any of my sources, so I'm not sure why it slipped my mind. Sometimes we overlook the most obvious solutions.

I also made a fancy version, just for kicks:
#!/usr/bin/runhugs
module Main where
import Data.List

main = putStrLn(unlines p++" ["++s p++"]") where
s = concat . intersperse ",\n " . map show
p =
["#!/usr/bin/runhugs",
"module Main where",
"import Data.List",
"",
"main = putStrLn(unlines p++\" [\"++s p++\"]\") where",
" s = concat . intersperse \",\\n \" . map show",
" p ="]

Comments

Popular posts from this blog

Tftp secret of TL-WR740N uncovered

Hidden TFTP of TP-Link routers

Haskell for embedded: C output, compilers, monads, Timber