]> git.rkrishnan.org Git - sicp.git/commitdiff
solutions to 2.38 and 2.39.
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 24 Nov 2010 12:13:03 +0000 (17:43 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Wed, 24 Nov 2010 12:13:03 +0000 (17:43 +0530)
src/sicp/ex2_38.rkt [new file with mode: 0644]
src/sicp/ex2_39.rkt [new file with mode: 0644]

diff --git a/src/sicp/ex2_38.rkt b/src/sicp/ex2_38.rkt
new file mode 100644 (file)
index 0000000..446f7ff
--- /dev/null
@@ -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 (file)
index 0000000..712baa0
--- /dev/null
@@ -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))
+