免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4207 | 回复: 5
打印 上一主题 下一主题

给FP版带点好玩的东西,FP语言的Quine [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-15 20:24 |只看该作者 |倒序浏览
问:何为Quine ?
答:A program that generates a copy of its own source text as its complete output

下面的网站上还有很多其他语言的例子。
http://www.nyx.net/~gthompso/quine.htm


  1. Language: Haskell


  2. Author: Jon Fairbairn ([email]Jon.Fairbairn@cl.cam.ac.uk[/email])

  3. main = putStr (quine q)
  4. quine s = s ++ show s
  5. q = "main = putStr (quine q)\nquine s = s ++ show s\nq = "

  6. Author: Andrew Mitchell

  7. --Hi there all, this is a quine for hugs by Andrew Mitchell
  8. --just run with the command quines
  9. a1 z zz = [zz] ++ e ++ init (init (concat [x:c|x <- z]))
  10. a2 = unlines ([((f!!z):[]) ++ e ++ t ++ (m!!z) ++ t | z <- [0..15]] ++ [[yf!!(2*z)]++[yf!!(2*z+1)] ++ e ++ t ++ (ym!!z) ++ t | z <- [0..11]])
  11. b = "a1 z zz = [zz] ++ e ++ init (init (concat [x:c|x <- z]))"
  12. c = "++"
  13. d = "quine"
  14. e = " = "
  15. f = "bcdefghijklnopwx"
  16. g = "a2 = unlines ([((f!!z):[]) ++ e ++ t ++ (m!!z) ++ t | z <- [0..15]] ++ [[yf!!(2*z)]++[yf!!(2*z+1)] ++ e ++ t ++ (ym!!z) ++ t | z <- [0..11]])"
  17. h = "t"
  18. i = "u"
  19. j = "v"
  20. k = "n"
  21. l = "h++e++t++v++t++t"
  22. n = "i++e++t++v++k++t"
  23. o = "j++e++t++v++v++t"
  24. p = "quines = putStr (unlines ([yj] ++ [yk] ++ [b] ++ [g] ++ [a2] ++ [q] ++ [r] ++ [s] ++ [a1 ya (head yd)] ++ [a1 yb (head ye)] ++ [a1 yc (head yg)] ++ [yh] ++ [yi] ++ [p]))"
  25. w = "["
  26. x = "]"
  27. ya = "hetvtt"
  28. yb = "ietvkt"
  29. yc = "jetvvt"
  30. yd = "q"
  31. ye = "r"
  32. yf = "yaybycydyeyfygyhyiyjykyl"
  33. yg = "s"
  34. yh = "ym = [ya,yb,yc,yd,ye,yf,yg,yh,yi,yj,yk,yl]"
  35. yi = "m = [b,c,d,e,f,g,h,i,j,k,l,n,o,p,w,x]"
  36. yj = "--Hi there all, this is a quine for hugs by Andrew Mitchell"
  37. yk = "--just run with the command quines"
  38. yl = ""

  39. t = "\""
  40. u = "\n"
  41. v = "\\"
  42. q = h++e++t++v++t++t
  43. r = i++e++t++v++k++t
  44. s = j++e++t++v++v++t
  45. ym = [ya,yb,yc,yd,ye,yf,yg,yh,yi,yj,yk,yl]
  46. m = [b,c,d,e,f,g,h,i,j,k,l,n,o,p,w,x]
  47. quines = putStr (unlines ([yj] ++ [yk] ++ [b] ++ [g] ++ [a2] ++ [q] ++ [r] ++ [s] ++ [a1 ya (head yd)] ++ [a1 yb (head ye)] ++ [a1 yc (head yg)] ++ [yh] ++ [yi] ++ [p]))

复制代码



  1. Language: Emacs LISP

  2. Author: Douglass Auclair

  3. (let ((quine "(let ((quine %c%s%c)) (insert (format quine 34 quine 34)))"))
  4. (insert (format quine 34 quine 34)))

  5. Author: Sam Steingold

  6. (let ((a "(let ((a %c%s%c)) (format a 34 a 34))")) (format a 34 a 34))

复制代码


  1. Language:  Scheme  

  2. Author: Tanaka Tomoyuki([email]tanaka@ucdavis.edu[/email])
  3. Note: (Chez Scheme Version 5.0b)

  4. (call/cc
  5.   (lambda (c)
  6.           (c ((lambda (c) `(call/cc (lambda (c) (c (,c ',c)))))
  7.               '(lambda (c) `(call/cc (lambda (c) (c (,c ',c)))))))))


  8. Author: Tanaka Tomoyuki([email]tanaka@ucdavis.edu[/email])
  9. Note: (Chez Scheme Version 5.0b)

  10. (call/cc
  11.   (lambda (c)
  12.     (call/cc
  13.       (lambda (cc)
  14.         (c ((lambda (c)
  15.               `(call/cc
  16.                  (lambda (c) (call/cc (lambda (cc) (c (,c ',c)))))))
  17.             '(lambda (c)
  18.                `(call/cc
  19.                   (lambda (c) (call/cc (lambda (cc) (c (,c ',c)))))))))))))


  20. Author: Tanaka Tomoyuki([email]tanaka@ucdavis.edu[/email])
  21. Note: (Chez Scheme Version 5.0b)

  22. ((lambda (q qq) ((lambda (x) `((lambda (q qq) ,(q x)) . ,(q qq)))
  23.                   '(lambda (x) `((lambda (q qq) ,(q x)) . ,(q qq)))))
  24.   (lambda (q) `(,q ',q))
  25.   '(lambda (q) `(,q ',q)))


  26. Author: Tanaka Tomoyuki([email]tanaka@ucdavis.edu[/email])
  27. Note: (Chez Scheme Version 5.0b)

  28. ((lambda (c)
  29.    (if (procedure? c) (c 0)
  30.        ((lambda (c) `((lambda (c) (if (procedure? c) (c 0) (,c ',c)))
  31.                       (call/cc call/cc)))
  32.         '(lambda (c) `((lambda (c) (if (procedure? c) (c 0) (,c ',c)))
  33.                        (call/cc call/cc))))))
  34.   (call/cc call/cc))


  35. Author: Tanaka Tomoyuki([email]tanaka@ucdavis.edu[/email])
  36. Note: (Chez Scheme Version 5.0b)

  37. ((lambda (c)
  38.    (if (procedure? c)
  39.        (c '`((lambda (c) (if (procedure? c) (c ',c) ,c)) (call/cc call/cc)))
  40.        `((lambda (c) (if (procedure? c) (c ',c) ,c)) (call/cc call/cc))))
  41.   (call/cc call/cc))


  42. Author: Tanaka Tomoyuki

  43. ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))


  44. Author: Tanaka Tomoyuki

  45. ((lambda (q) ((lambda (x) `((lambda (q) ,((eval q) x)) ',q))
  46.               '(lambda (x) `((lambda (q) ,((eval q) x)) ',q))))
  47. '(lambda (x) `(,x ',x)))


  48. Author: Moshe Zadka ([email]moshez@math.huji.ac.il[/email])
  49. Note: guile (A GNU implementation of Scheme)

  50. #!/usr/bin/guile \
  51. -e main -s
  52. !#
  53. (define (main args)
  54.    (display program)
  55.    (write program)
  56.    (display #\))
  57.    (newline))

  58. (define program
  59. "#!/usr/bin/guile \\
  60. -e main -s
  61. !#
  62. (define (main args)
  63.    (display program)
  64.    (write program)
  65.    (display #\\))
  66.    (newline))

  67. (define program
  68. ")


  69. Author: Moshe Zadka ([email]moshez@math.huji.ac.il[/email])

  70. ((lambda (p) (write (list p (list (quote quote) p))))
  71. (quote (lambda (p) (write (list p (list (quote quote) p))))))


  72. Author: Tanaka Tomoyuki
  73. Note: Palindromic in nature (look carefully I missied it at first)

  74. ((lambda (x) `(,(reverse x) ',x)) '(`(,(reverse x) ',x) (x) lambda))
复制代码


  1. Language: LISP (or Scheme)


  2. Author: Unknown
  3. Notes: There are several silly examples like this.

  4. T


  5. Author: Steven M. Haflich
  6. Note common LISP only

  7. #1=(WRITE '#1# :CIRCLE T)


  8. Author: Peter Norvig

  9. #1=(setq *print-circle* '#1#)


  10. Author: Unknown
  11. Note: only works if print-circle is defined

  12. #1='#1#
  13. Author: Peter Norvig
  14. Note: common LISP, self evaluating

  15. #1=((lambda () (setq *print-circle* '#1#)))

  16. Author: Chris Hruska

  17. ((LAMBDA (X) (LIST X (LIST 'QUOTE X))) '(LAMBDA (X) (LIST X (LIST 'QU
  18. OTE X))))


  19. Author: Dave Seaman ([email]ags@seaman.cc.purdue.edu[/email])

  20. (let ((p "(let ((p ~s)) (format t p p))")) (format t p p))


  21. Author: Sam Steingold

  22. (let ((a '(list 'let (list (list 'a (list 'quote a))) a)))
  23.   (list 'let (list (list 'a (list 'quote a))) a))


  24. Author: Sam Steingold

  25. (let ((a '(list 'let (list (list 'a (list 'quote a))) a)))
  26.   `(let ((a (quote ,a))) ,a))
  27. Author: Unknown

  28. ((lambda (x) (list x x)) (lambda (x) (list x x)))


  29. Author: Joe Miller
  30. Notes: In Common Lisp this should actually evaluate to the above list. In some dialects, however, it evaluates to itself.

  31. ((LAMBDA (X) `(,X ',X)) '(LAMBDA (X) `(,X ',X)))


  32. Author: Peter Norvig

  33. ((lambda (x) (list `',x)) '(lambda (x) (list x `',x)))


  34. Author: Unknown
  35. Note: common LISP

  36. ((lambda (list) (list list `',list))

  37. '(lambda (list) (list list `',list)))


  38. Author: John McCarthy(creator of the language) and Carolyn Talcott


  39. ((lambda (x)
  40.    (list x (list (quote quote) x)))
  41.   (quote
  42.      (lambda (x)
  43.        (list x (list (quote quote) x)))))

  44. Author: John Burger, David Brill, Filip Machi

  45. (PRINTME (LAMBDA NIL (PROG (A B)
  46.      (SETQ A (QUOTE (PRINTME (LAMBDA NIL (PROG (A B)
  47.           (SETQA (QUOTE FOO))
  48.           (SETQ B (COPY A))
  49.           (RPLACA (CDADDR (CADDAR (CDDADR B)))A)
  50.      (SETQ B (COPY A))
  51.      (RPLACA (CDADDR (CADDAR (CDDADR B))) A)
  52.      (RETURN B))))


  53. Author: Louise Hay
  54. Note: Result of her quine-generating program

  55. ((LAMBDA NIL
  56.     ((LAMBDA (Y)
  57.         (LIST
  58.            (LIST
  59.               (QUOTE LAMBDA)
  60.               NIL
  61.               (LIST
  62.                  Y
  63.                  (LIST
  64.                     (QUOTE QUOTE)
  65.                     Y)))))
  66.       (QUOTE
  67.          (LAMBDA (Y)
  68.             (LIST
  69.                (LIST
  70.                   (QUOTE LAMBDA)
  71.                   NIL
  72.                   (LIST
  73.                      Y
  74.                      (LIST
  75.                         (QUOTE QUOTE)
  76.                          Y)))))))))


  77. Author: Pekka P. Pirinen

  78. Notes: This is a self-printing expression in the format sublanguage.
  79. It works by using the ~:* directive to back up in the argument list
  80. and reuse the previous argument; this is how it manages to use the
  81. same string to format the new expression and print a copy of itself
  82. inside the new expression.  Some CLs have a bug in the ~@?
  83. recursive format directive that prevents backing up beyond the
  84. control string argument, but the standard implies it ought to work.
  85. Tested in LispWorks 4.2 on Unix.

  86. (format t "~@?" "(format t \"~~@?\" ~:*~S)")


  87. Authors: Paul Bratley and Jean Millo

  88. define((
  89. (c (prog (a)
  90. (print (quote define))
  91. (print (list(list(list(quote c) (get(quote c) (quote expr))))))
  92. (print (quote c))
  93. (print (list a))
  94. (print (quote stop))
  95. (print (quote fin))))))
  96. c(nil)
  97. stop
  98. fin


  99. Author: [email]Olin.Shivers@cosmos.vlsi.cs.cmu.edu[/email]

  100. ((lambda (lambda) `(,lambda ',lambda)) '(lambda (lambda) `(,lambda ',lambda)))
复制代码

[ 本帖最后由 swordfish.cn 于 2008-12-15 20:26 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-12-15 21:27 |只看该作者


以前用 c 写过一个。

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2008-12-15 22:19 |只看该作者

回复 #1 swordfish.cn 的帖子

不错

Haskell 这个稍微改下,putStr 换为 putStrLn

论坛徽章:
0
4 [报告]
发表于 2008-12-15 22:37 |只看该作者
有意思

论坛徽章:
0
5 [报告]
发表于 2008-12-16 22:11 |只看该作者
汗......那个网页上居然还有TeX的 Quine ........................
随便输入个a, TeX不敢输出b来.....

论坛徽章:
0
6 [报告]
发表于 2008-12-18 21:16 |只看该作者
搜索到的另一个相关的网页
http://pluskid.lifegoo.com/wiki/Quine.html
打印自身的程序

感觉Quine就像生命体一样,可以自我繁殖。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP