旧问题的 F# 新解法

Fsharp

在我还对程序开发比较迷糊的时候, 曾经听说过一个问题, 编写一段程序, 运行的结果是输出程序的全部源代码(一字不差).

当初曾经有很多人认为这是不可能的, 因为这与逻辑相悖, 不过后来真的有人用纯C做到了, 思路一出, 其他版本纷纷涌现.

这是一个老问题了, 今天在订阅里意外的看到一篇文章:

代码摘自: http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!599.entry

#!fsharp
#light 
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 = "#light 
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)