fork download
  1. ; Can you use P and Q to build a stream?
  2. ; ------------------------------
  3. ; The Little Lisper 3rd Edition
  4. ; Chapter 9
  5. ; Exercise 10
  6. ; Common Lisp
  7. ; http://t...content-available-to-author-only...r.com/thelittlelisper
  8. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper-chapter-9-lamdba-ultimate.html
  9. ; http://t...content-available-to-author-only...t.com/2010/06/little-lisper.html
  10. ; ------------------------------
  11.  
  12. (defun add1 (n)
  13. (+ 1 n))
  14.  
  15. (defun build (a b)
  16. (cons a (cons b '())))
  17.  
  18. (defun str-maker (next n)
  19. (build n (lambda ()
  20. (str-maker next (funcall next n)))))
  21.  
  22. (set 'int-maker (str-maker (function add1) 0))
  23.  
  24. (defun first$ (l)
  25. (first l))
  26.  
  27. (defun second$ (l)
  28. (funcall (second l)))
  29.  
  30. (defun /_(m n)
  31. (cond
  32. ((zero m) 0)
  33. ((< m 0) 0)
  34. ((< m n) 0)
  35. (t (+ 1 (/_ (- m n) n)))))
  36.  
  37.  
  38. (defun remainder (n m)
  39. (cond
  40. (t (- n (* m (/_ n m))))))
  41.  
  42. (defun Q (str n)
  43. (cond
  44. ((zero (remainder (first$ str) n))
  45. (Q (second$ str) n))
  46. (t (build (first$ str) (lambda () (Q (second$ str) n))))))
  47.  
  48. (defun P (str)
  49. (build (first$ str) (lambda () (P (Q str (first$ str))))))
  50.  
  51. (defun zero (n)
  52. (= 0 n))
  53.  
  54. (defun sub1 (n)
  55. (- n 1))
  56.  
  57. (defun frontier (str n)
  58. (cond
  59. ((zero n) '())
  60. (t (cons (first$ str) (frontier (second$ str)(sub1 n))))))
  61.  
  62. (print (frontier (P (second$ (second$ int-maker))) 10))
  63. ; (2 3 5 7 11 13 17 19 23 29)
  64. ; This is a stream of prime numbers
  65.  
  66.  
Success #stdin #stdout 0.01s 10624KB
stdin
Standard input is empty
stdout
(2 3 5 7 11 13 17 19 23 29)