
Recent Posts
Archives
Categories
Monthly Archives: May 2012
SICP solution exercise 2.23
Solution for exercise 2.23 of SICP: ; The value returned by the call to foreach (not illustrated above) can be something arbitrary, ; such as true. Give an implementation of foreach. (define nil ‘()) (define (foreachalt proc list) (define (foreachiter … Continue reading
SICP solution exercise 2.22
Solution for exercise 2.22 of SICP: (define nil ‘()) (define (square x) (* x x)) (define (squarelist1 items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons (square (car things)) answer)))) (iter items nil)) ;; test (squarelist1 … Continue reading
SICP solution exercise 2.21
Solution for exercise 2.21 of SICP: (define nil ‘()) ;; definition map (define (map proc items) (if (null? items) nil (cons (proc (car items)) (map proc (cdr items))))) ;; definition 1 (define (squarelist1 items) (if (null? items) nil (cons (* … Continue reading
SICP solution exercise 2.20
Solution for exercise 2.20 of SICP: (define nil ‘()) (define (mapc proc condproc items) (if (null? items) nil (if (condproc (car items)) (cons (proc (car items)) (mapc proc condproc (cdr items))) (mapc proc condproc (cdr items))))) (define (sameparity . w) … Continue reading
SICP solution exercise 2.19
Solution for exercise 2.19 of SICP: (define uscoins1 (list 50 25 10 5 1)) (define uscoins2 (list 25 50 1 5 10)) (define ukcoins (list 100 50 20 10 5 2 1 0.5)) (define (cc amount coinvalues) (cond ((= amount … Continue reading
SICP solution exercise 2.18
Solution for exercise 2.18 of SICP: (define (reverse a) (if (null? a) (list ) (append (reverse (cdr a)) (cons (car a) (list ))))) (define a (list 25 16 9 4 1)) (reverse a) (define b (list 3)) (reverse b) (define … Continue reading
SICP solution exercise 2.17
Solution for exercise 2.17 of SICP: (define (lastpair a) (if (null? (cdr a)) a (lastpair (cdr a)))) (define a (list 23 72 149 34)) (define b (list 5 23 44 93 156)) (lastpair a) (lastpair b)