Exercise 2.30. Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows:
(square-tree
(list 1
(list 2 (list 3 4) 5)
(list 6 7)))
(1 (4 (9 16) 25) (36 49))
Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (define nil '()) | |
| (define (square-tree tree) | |
| (cond | |
| ((null? tree) | |
| nil) | |
| ((not (pair? tree)) | |
| (* tree tree)) | |
| (else | |
| (cons | |
| (square-tree (car tree)) | |
| (square-tree (cdr tree)))))) | |
| (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))) | |
| (define (square-tree tree) | |
| (map | |
| (lambda (x) | |
| (cond | |
| ((null? x) nil) | |
| ((not (pair? x)) | |
| (* x x)) | |
| ((list? x) (square-tree x)))) | |
| tree)) | |
| (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7))) | |