Monthly Archives: May 2012

SICP solution exercise 2.23

Solution for exercise 2.23 of SICP: ; The value returned by the call to for-each (not illustrated above) can be something arbitrary, ; such as true. Give an implementation of for-each. (define nil ‘()) (define (for-each-alt proc list) (define (for-each-iter … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.22

Solution for exercise 2.22 of SICP: (define nil ‘()) (define (square x) (* x x)) (define (square-list-1 items) (define (iter things answer) (if (null? things) answer (iter (cdr things) (cons (square (car things)) answer)))) (iter items nil)) ;; test (square-list-1 … Continue reading

Posted in SICP | Tagged , | Leave a comment

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 (square-list-1 items) (if (null? items) nil (cons (* … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.20

Solution for exercise 2.20 of SICP: (define nil ‘()) (define (mapc proc cond-proc items) (if (null? items) nil (if (cond-proc (car items)) (cons (proc (car items)) (mapc proc cond-proc (cdr items))) (mapc proc cond-proc (cdr items))))) (define (same-parity . w) … Continue reading

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.19

Solution for exercise 2.19 of SICP: (define us-coins1 (list 50 25 10 5 1)) (define us-coins2 (list 25 50 1 5 10)) (define uk-coins (list 100 50 20 10 5 2 1 0.5)) (define (cc amount coin-values) (cond ((= amount … Continue reading

Posted in SICP | Tagged , | Leave a comment

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

Posted in SICP | Tagged , | Leave a comment

SICP solution exercise 2.17

Solution for exercise 2.17 of SICP: (define (last-pair a) (if (null? (cdr a)) a (last-pair (cdr a)))) (define a (list 23 72 149 34)) (define b (list 5 23 44 93 156)) (last-pair a) (last-pair b)

Posted in SICP | Tagged , | Leave a comment