Tag Archives: Quine

A Haskell Quine

A Haskell quine. That is, a program whose output is itself.


main = putStr (s ++ [';',' ','s',' ','=',' '] ++ show s);
s = "main = putStr (s ++ [';',' ','s',' ','=',' '] ++ show s)"

To deal with string quoting, it is difficult to write a quine not assuming a particular encoding (e.g. ASCII). In Haskell, however, it is side-stepped by using the built-in show function for strings. See also the quine page.

Functional programmers would notice that such programs resembles the lambda expression (\x -> x x) (\x -> x x), which reduces to itself. As a simple extension, this self-expanding program inserts to itself one line of empty comment each time it is run. It resembles the Y combinator.

Yokoyama later came up with a more elegant quine.