-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise2-22.lisp
More file actions
37 lines (35 loc) · 1001 Bytes
/
exercise2-22.lisp
File metadata and controls
37 lines (35 loc) · 1001 Bytes
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
(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 '()))
(display (square-list (list 1 2 3 4)))
(newline)
; the results are being accumulated in reverse order
(define (square x) (* x x))
(define (square-list items)
(define (iter things answer)
(if (null? things)
answer
(iter (cdr things)
(cons answer
(square (car things))))))
(iter items '()))
(display (square-list (list 1 2 3 4)))
(newline)
; this is the incorrect list with each pair flipped
(define (square x) (* x x))
(define (square-list items)
(define (iter things)
(if (null? things)
'()
(cons (square (car things))
(iter (cdr things)))))
(iter items))
(display (square-list (list 1 2 3 4)))
(newline)
; the correct order (or just use map)