From: Ramakrishnan Muthukrishnan Date: Sun, 18 Dec 2011 07:16:06 +0000 (+0530) Subject: solution to 4.21 X-Git-Url: https://git.rkrishnan.org/pf/content/en/footer/rgr-080307.php?a=commitdiff_plain;h=7e4dba0e8b405ec49d697d11c75428584475837a;p=sicp.git solution to 4.21 --- diff --git a/src/sicp/ex4_21.rkt b/src/sicp/ex4_21.rkt new file mode 100644 index 0000000..a28f54f --- /dev/null +++ b/src/sicp/ex4_21.rkt @@ -0,0 +1,38 @@ +#lang racket + +(define Y (lambda (f) + ((lambda (g) + (g g)) + (lambda (h) + (lambda (x) + ((f (h h)) x)))))) + +;; factorial +((Y (lambda (fct) + (lambda (n) + (if (= n 1) + 1 + (* n (fct (- n 1))))))) + 10) + +;; fibonacci +((Y (lambda (fib) + (lambda (n) + (cond + ((= n 0) 0) + ((= n 1) 1) + (else (+ (fib (- n 1)) + (fib (- n 2)))))))) + 10) + +;; b +(define (f x) + ((lambda (even? odd?) + (even? even? odd? x)) + (lambda (ev? od? n) + (if (= n 0) true (od? ev? od? (- n 1)))) + (lambda (ev? od? n) + (if (= n 0) false (ev? ev? od? (- n 1)))))) + +(eq? (f 3) #f) +(eq? (f 4) #t) \ No newline at end of file