3
在小策士的第9章中,作者提出了以下两种功能将Little Schemer的Q和P函数转换为Common Lisp?
(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)
你会怎么写P和Q Common Lisp中的函数?
(我有翻译在Y Combinator的自己 - 但我发现这一个挑战)
--Helper Functions--
(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))))))
(免责声明 - 这是不是一门功课的问题 - 这是我的理解和学习)
为什么'Q'和'frontier'定义完全一样? “second $”和“first $”的定义是什么(它们与“second”和“first”相同)? – Svante 2009-11-11 17:13:55
谢谢 - 更正 – hawkeye 2009-11-11 21:39:40