-
Notifications
You must be signed in to change notification settings - Fork 0
/
e.2.30.rkt
43 lines (34 loc) · 971 Bytes
/
e.2.30.rkt
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
#lang racket
(define square (lambda (x) (* x x)))
(define (square-tree l)
(cond ((null? l) '())
((pair? (car l))
(cons (square-tree (car l))
(square-tree (cdr l))))
(else (cons (square (car l))
(square-tree (cdr l))))))
(define (square-tree l)
(map (lambda (x)
(if (pair? x)
(square-tree x)
(square x)))
l))
(define (square-tree l)
(define (iter l co)
(cond ((null? l) (co '()))
((pair? (car l))
(iter (car l)
(lambda (x)
(iter (cdr l)
(lambda (y)
(co (cons x y)))))))
(else (iter (cdr l)
(lambda (x)
(co
(cons (square (car l)) x)))))))
(iter l (lambda (x) x)))
(square-tree '(1 2 3 9))
(square-tree
(list 1
(list 2 (list 3 4) 5)
(list 6 7)))