From: Ramakrishnan Muthukrishnan Date: Wed, 24 Nov 2010 12:13:03 +0000 (+0530) Subject: solutions to 2.38 and 2.39. X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=87f19316259fa5c6787e6183527649fa2beacbb1;p=sicp.git solutions to 2.38 and 2.39. --- diff --git a/src/sicp/ex2_38.rkt b/src/sicp/ex2_38.rkt new file mode 100644 index 0000000..446f7ff --- /dev/null +++ b/src/sicp/ex2_38.rkt @@ -0,0 +1,11 @@ +#lang racket + +(define (fold-left op initial coll) + (define (iter result rest) + (if (null? rest) + result + (iter (op result (car rest)) + (cdr rest)))) + (iter initial coll)) + +(provide fold-left) \ No newline at end of file diff --git a/src/sicp/ex2_39.rkt b/src/sicp/ex2_39.rkt new file mode 100644 index 0000000..712baa0 --- /dev/null +++ b/src/sicp/ex2_39.rkt @@ -0,0 +1,14 @@ +#lang racket + +(require "utils.rkt" + "ex2_36.rkt" + "ex2_38.rkt") + +(define fold-right accumulate) + +(define (reverse1 coll) + (fold-right (lambda (x y) (append y (list x))) '() coll)) + +(define (reverse2 coll) + (fold-left (lambda (x y) (cons y x)) '() coll)) +