diff options
| author | Steve Lee <me@xiangyangli.com> | 2016-11-02 02:42:01 +0800 |
|---|---|---|
| committer | Steve Lee <me@xiangyangli.com> | 2016-11-02 02:42:01 +0800 |
| commit | 38f1804506888956e1fc95e3e405a366e17d4812 (patch) | |
| tree | 5c497c52d66292939a73cafdea519940636d2ac8 /SICP/ex2_17.scm | |
| parent | 4baf0af60ba3e2bd27a181f52f3dcec312905fd4 (diff) | |
| download | Personal-38f1804506888956e1fc95e3e405a366e17d4812.tar.xz Personal-38f1804506888956e1fc95e3e405a366e17d4812.zip | |
add sicp ex
Diffstat (limited to 'SICP/ex2_17.scm')
| -rw-r--r-- | SICP/ex2_17.scm | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/SICP/ex2_17.scm b/SICP/ex2_17.scm index ac8f0b2..37e3eb3 100644 --- a/SICP/ex2_17.scm +++ b/SICP/ex2_17.scm @@ -139,3 +139,50 @@ one-through-four (define (squares items) (map (lambda (x) (* x x)) items)) (squares (list 1 2 3 4)) + +;; Exercise2.22 +(define (square x) (* x x)) +(define (square-list items) + (define (iter things answer) + (if (null? things) + answer + (iter (cdr things) + (cons (square (car things)) + answer)))) + (iter items nil)) +;; Modify one +(define (square-list items) + (define (iter things answer) + (if (null? things) + answer + (iter (cdr things) + (cons answer + (square + (car things)))))) + (iter items nil)) +(square-list (list 1 2 3 4 5)) +(list 1 4 9 16 25) +;; The right one; +(cons 1 (cons 4 (cons 9 (cons 16 (cons 25 nil))))) +;; The wrong one +(cons (cons (cons (cons (cons nil 1) 4) 9) 16) 25) + +;; Exercise2.23 +;; why nil print '() +(define (for-each proc items) + (define (iter proc items item) + (if (null? item) + nil + (proc item)) + (if (null? items) + nil + (iter proc (cdr items) (car items)))) + (iter proc items nil)) + +(for-each + (lambda (x) (newline) (display x)) + (list 57 321 88)) + +(cons (list 1 2) (list 3 4)) + + |
