当前位置: 代码迷 >> 综合 >> sicp 习题2.30
  详细解决方案

sicp 习题2.30

热度:22   发布时间:2024-01-11 16:32:08.0
(define (square-tree lst)(cond ((null?  lst) nil)((not (pair? lst)) (square lst))(else (cons (square-tree (car lst)) (square-tree (cdr lst))))))
(define (square x) (* x x))

run:

> (square-tree (list 1 (list 2 (list 3 4) 5) (list 6 7)))
{mcons
 1
 {mcons
  {mcons
   4
   {mcons {mcons 9 {mcons 16 '()}} {mcons 25 '()}}}
  {mcons {mcons 36 {mcons 49 '()}} '()}}}

(define (square-tree tree)(map (lambda (sub-tree)(if (pair? sub-tree)(square-tree sub-tree)(square sub-tree)))tree))(define (square x) (* x x))