Read Chapters 2, 4, and 8 in the EOPL book.
For each of the following problems, write a recursive definition, and then convert it to continuation-passing style. The variable I below is defined as the identity procedure (lambda (x) x).
(fib 0) ==> 0 (fib 1) ==> 1 (fib 2) ==> 1 (fib 8) ==> 21 (fibcps 0 I) ==> 0 (fibcps 1 I) ==> 1 (fibcps 2 I) ==> 1 (fibcps 8 I) ==> 21
(remfirst 3 '(1 2 3 4 5 3)) ==> (1 2 4 5 3) (remfirst 3 '()) ==> () (remfirstcps 3 '(1 2 3 4 5 3) I) ==> (1 2 4 5 3) (remfirstcps 3 '() I) ==> ()
(remall 3 '(1 2 3 4 5 3)) ==> (1 2 4 5) (remall 3 '()) ==> () (remallcps 3 '(1 2 3 4 5 3) I) ==> (1 2 4 5) (remallcps 3 '() I) ==> ()
(insert 1 3 '(1 2 3 4 5)) ==> (1 2 3 1 4 5) (insert 1 3 '()) ==> () (insertcps 1 3 '(1 2 3 4 5) I) ==> (1 2 3 1 4 5) (insertcps 1 3 '() I) ==> ()
(member? 3 '(1 2 3 4 3 3)) ==> #t (member? #\x '(#\a #\e #\u #\x #\y)) ==> #t (member? 3 '()) ==> #f (membercps? 3 '(1 2 3 4 3 3) I) ==> #t (membercps? #\x '(#\a #\e #\u #\x #\y) I) ==> #t (membercps? 3 '() I) ==> #f
(set? '(1 2 3 4)) ==> #t (set? '()) ==> #t (set? '(1 1)) ==> #f (setcps? '(1 2 3 4) I) ==> #t (setcps? '() I) ==> #t (setcps? '(1 1) I) ==> #f
(filter even? '(1 2 3 4 5 6)) ==> (2 4 6) (filter (lambda (x) (> x 8)) '(1 9 2 10 3 11 4 12)) ==> (9 10 11 12) (filter (lambda (x) (and (> x 8) (< x 10))) '(1 9 2 10 3 11 4 12)) ==> (9) (filtercps (lambda (x k) (k (even? x))) '(1 2 3 4 5 6) I) ==> (2 4 6) (filtercps (lambda (x k) (k (> x 8))) '(1 9 2 10 3 11 4 12) I) ==> (9 10 11 12) (filtercps (lambda (x k) (k (and (> x 8) (< x 10)))) '(1 9 2 10 3 11 4 12) I) ==> (9)In other words, when converting to CPS, assume that the predicate has been also converted to CPS.
(map add1 '(1 2 3)) ==> (2 3 4) (map (lambda (x) (* x 3)) '(1 2 3)) ==> (3 6 9) (mapcps (lambda (x k) (k (add1 x))) '(1 2 3) I) ==> (2 3 4) (mapcps (lambda (x k) (k (* x 3))) '(1 2 3) I) ==> (3 6 9)
(powerset '()) ==> '(()) (powerset '(1 2 3)) ==> ((),(3),(2),(2,3),(1),(1,3),(1,2),(1,2,3)) (powersetcps '() I) ==> '(()) (powersetcps '(1 2 3) I) ==> ((),(3),(2),(2,3),(1),(1,3),(1,2),(1,2,3))
(takeWhile even? '(2 4 6 1 8 10 12)) ==> (2 4 6) (takeWhile even? '()) ==> () (takeWhilecps (lambda (x k) (k (even? x))) '(2 4 6 1 8 10 12) I) ==> (2 4 6) (takeWhilecps (lambda (x k) (k (even? x))) '() I) ==> ()
(dropWhile even? '(2 4 6 1 8 10 12)) ==> (1 8 10 12) (dropWhile even? '()) ==> () (dropWhilecps (lambda (x k) (k (even? x))) '(2 4 6 1 8 10 12) I) ==> (1 8 10 12) (dropWhilecps (lambda (x k) (k (even? x))) '() I) ==> ()
(piglatin "able") ==> "ableyay" (piglatin "stripe") ==> "ipestray" (piglatincps "able" I) ==> "ableyay" (piglatincps "stripe" I) ==> "ipestray"