-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_2_22.clj
54 lines (51 loc) · 1.53 KB
/
ex_2_22.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(ns sicp.chapter-2.part-2.ex-2-22
(:require
[sicp.misc :as m]))
; Exercise 2.22
;
; Louis Reasoner tries to rewrite the first square-list procedure of Exercise 2.21
; so that it evolves an iterative process:
;
; (define (square-list items)
; (define (iter things answer)
; (if (null? things)
; answer
; (iter (cdr things)
; (cons (square (car things))
; answer))))
; (iter items nil))
;
; Unfortunately, defining square-list this way produces the answer list in the reverse
; order of the one desired. Why?
;
; Louis then tries to fix his bug by interchanging the arguments to cons:
;
; (define (square-list items)
; (define (iter things answer)
; (if (null? things)
; answer
; (iter (cdr things)
; (cons answer
; (square
; (car things))))))
; (iter items nil))
;
; This doesn’t work either. Explain.
(defn square-list-1
[items]
(letfn [(iter
[things answer]
(if (m/list-empty? things)
answer
(iter (m/cdr things)
(cons (m/square (m/car things))
answer))))]
(iter items nil)))
; (defn square-list-2 [items]
; (letfn [(iter [things answer]
; (if (empty? things)
; answer
; (iter (m/cdr things)
; (cons answer ; invalid structure of list here
; (m/square (m/car things)))
; (iter items nil)))