A Third Order Quine in Three Languages
Suppose Fnxy is a program, written in language x, that takes as input n string arguments as input, G1,...,Gn and outputs a program in language y that is the application of the function whose source code is G2 to the strings G2,G3,...,Gn,G1. Then F3xy(F3xy,F3yz,F3zx) will be a program in language x that that outputs a program in y that computes F3yz(F3yz,F3zx,F3xy).
We can put this into practice:
This is a Haskell program that outputs a Python program that outputs a Ruby program that outputs the original Haskell program.
Apologies for the lack of line breaks. If it's not readable, it should be possible to copy and paste that source. Also, you may need to tweak it if your OS doesn't treat character 10 as a new line.
We can put this into practice:
q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
main=q "q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']" "def q(a,b,c):print b+chr(10)+'q('+repr(b)+','+repr(c)+','+repr(a)+')'" "def e(x) return 34.chr+x+34.chr end;def q(a,b,c) print b+10.chr+'main=q '+e(b)+' '+e(c)+' '+e(a)+' '+10.chr end"
This is a Haskell program that outputs a Python program that outputs a Ruby program that outputs the original Haskell program.
Apologies for the lack of line breaks. If it's not readable, it should be possible to copy and paste that source. Also, you may need to tweak it if your OS doesn't treat character 10 as a new line.
Labels: haskell, programming, self-reference
15 Comments:
That is awesome.
[15:27 /]$ ghci
___ ___ _
/ _ \ /\ /\/ __(_)
/ /_\// /_/ / / | | GHC Interactive, version 6.6.1, for Haskell 98.
/ /_\\/ __ / /___| | http://www.haskell.org/ghc/
\____/\/ /_/\____/|_| Type :? for help.
Prelude> q a b c=putStrLn $ b ++ [toEnum 10,'q','('] ++ show b ++ [','] ++ show c ++ [','] ++ show a ++ [')']
< interactive >:1:7: parse error on input `='
Impressive O_O
nice!
Nikolay: you won't be able to just paste it into ghci like that, you'll have to put it in a file and then load the file into ghci.
nikolay:
Write it into .hs file and use runhaskell to execute it.
ghci expects let before name bindings to distinguish them from expressions.
Ooh, I see. Thanks! It is really awesome!
Wow! That is really awesome.
Wow! You're all dorks
Amazing :)
...please where can I buy a unicorn?
Gorgeous!
This is so fascinating! You are awesome =]
What this really needs is a recursive function
it formatted my pr0n.
runhaskell 3quine |python|ruby|diff 3quine -
*blinks*
*awe*
Post a Comment
<< Home