Inside F#

Brian's thoughts on F# and .NET

An oldie but a goodie…

Posted by Brian on October 17, 2008

What does the following F# program do?  Why not run it and find out?

open System
let nl = new String([|char 13;char 10|])
let quote = char 34
let Quote (s:String) = s.Replace(new String([|quote|]), new String([|char 92; quote|]))
let Quine s =
    String.Format("let s = {0}{1}{2}{3}printf {4}%s%s{5} s (Quine s){6}"
        [| box quote; box (Quote s); box quote; box nl; box quote; box quote; box nl|])
let s = "open System
let nl = new String([|char 13;char 10|])
let quote = char 34
let Quote (s:String) = s.Replace(new String([|quote|]), new String([|char 92; quote|]))
let Quine s =
    String.Format(\"let s = {0}{1}{2}{3}printf {4}%s%s{5} s (Quine s){6}\", 
        [| box quote; box (Quote s); box quote; box nl; box quote; box quote; box nl|])
"
printf "%s%s" s (Quine s)
Advertisements

2 Responses to “An oldie but a goodie…”

  1. 冷血儿 said

    I remember that!"If someone can write some code print themself. Then he can catch his hair and flying in the sky."

  2. OJ said

    The word "quine" is a bit of a giveaway :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: