-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathex2.27-deep-reverse.scm
45 lines (33 loc) · 1.04 KB
/
ex2.27-deep-reverse.scm
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
; (define (reverse1 l)
; (define (iter l rl)
; (if (null? l)
; rl
; (iter (cdr l) (cons (car l) rl))))
; (iter (cdr l) (list (car l)))
; ; (iter l '())
; )
; (define (reverse2 l)
; (if (null? l)
; l
; (append (reverse2 (cdr l)) (list (car l)))))
; (reverse2 (list 1 2 3))
(define (deep-reverse1 l)
(define (iter l rl)
(cond ((null? l) rl)
((not (pair? l)) l)
(else (iter (cdr l) (cons (deep-reverse1 (car l)) rl) ))))
(iter l (list ))
)
(deep-reverse1 (list 1 2 (list 10 (list 100 101 1002) 12 13)))
(define (deep-reverse2 l) ;; flat, wrong
(cond ((null? l) l)
((not (pair? l)) (list l))
(else (append (deep-reverse2 (cdr l)) (deep-reverse2 (car l))))))
(deep-reverse2 (list 1 2 (list 10 (list 100 101 1002) 12 13)))
; fix:
(define (deep-reverse2 l)
(cond ((null? l) l)
((not (pair? l)) l)
(else (append (deep-reverse2 (cdr l)) (list (deep-reverse2 (car l)))))))
; (deep-reverse2 (list 1 2 (list 10 (list 100 101 1002) 12 13)))
(deep-reverse2 (list 1 2 (list 10 (list 100 101 1002) 12 13)))