In chapter 9 of the βSmall Chart," the author presents the following two functions:
(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)))))))
and suggests that they be evaluated with the following performance:
(frontier (P (second$ (second$ int))) 10)
How would you write P and Q functions in Common Lisp?
(I translated the Y-Combinator myself, but I find it difficult)
- Assistant 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 ))))))
(Disclaimer - this is not a domestic issue - this is for my understanding and training)
source
share