在 Little Schemer 的第 9 章中,作者介绍了以下两个函数
(define Q
(lambda (str n)
(cond
((zero? (remainder (first$ str ) n))
(Q (second$ str ) n))
(t (build (first$ str )
(lambda ( )
(Q (second$ str ) n)))))))
(define P
(lambda (str)
(build (first$ str)(lambda () (P (Q str (first$ str)))))))
并建议通过以下执行对它们进行评估:
(frontier (P (second$ (second$ int))) 10)
你会如何在 Common Lisp 中编写 P 和 Q 函数?
(我自己翻译了 Y-Combinator - 但我发现这个很有挑战性)
--辅助功能--
(define frontier
(lambda (str n)
(cond
((zero? n) (quote ()))
(t (cons (first$ str) (frontier (second$ str) (sub1 n)))))))
(define str-maker
(lambda (next n)
(build n (lambda () (str-maker next (next n))))))
(define int (str-maker add1 0))
(define second$
(lambda (str)
((second str))))
(define first$ first)
(define build
(lambda (a1 a2)
(cond
(t (cons a1
(cons a2 (quote ())))))))))
(define first
(lambda (p)
(cond
(t (car p)))))
(define second
(lambda (p)
(cond
(t (car (cdr p))))))
(define add1
(lambda (n)
(+ 1 n)))
(define remainder
(lambda (n m)
(cond
(t (- n (* m (/ n m ))))))
(免责声明 - 这不是家庭作业问题 - 这是为了我的理解和学习)