|
|
@ -25,9 +25,20 @@ |
|
|
|
|
|
|
|
|
|
|
|
(define (prepend first-value) (lambda (rest) (cons first-value rest))) |
|
|
|
(define (prepend first-value) (lambda (rest) (cons first-value rest))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;=============== combinators ====================== |
|
|
|
(define (Y f) (f (lambda (x) ((Y f) x)))) |
|
|
|
(define (Y f) (f (lambda (x) ((Y f) x)))) |
|
|
|
(define (Y2 f) (f (lambda (x y) ((Y2 f) x y)))) |
|
|
|
(define (Y2 f) (f (lambda (x y) ((Y2 f) x y)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(assert-eq "Y test 1 (factorial) failed" |
|
|
|
|
|
|
|
120 |
|
|
|
|
|
|
|
((Y (lambda (f) (lambda (n) (if (= n 0) 1 (* n (f (- n 1))))))) 5)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(assert-eq "Y2 test 1 (pascal triangle) failed" |
|
|
|
|
|
|
|
35 |
|
|
|
|
|
|
|
((Y2 (lambda (f) (lambda (a b) |
|
|
|
|
|
|
|
(if (= a 0) 1 (if (= b 0) 1 (+ (f (- a 1) b) (f a (- b 1)))))))) |
|
|
|
|
|
|
|
3 4)) |
|
|
|
|
|
|
|
|
|
|
|
(define (reduce-right initial reducer) (Y |
|
|
|
(define (reduce-right initial reducer) (Y |
|
|
|
(lambda (f) (lambda (values) |
|
|
|
(lambda (f) (lambda (values) |
|
|
|
(if (null? values) initial (reducer (car values) (f (cdr values)))))))) |
|
|
|
(if (null? values) initial (reducer (car values) (f (cdr values)))))))) |
|
|
|