オンラインSICP読書女子会 #20 (2.2.2(3)) - connpass
SICP/2.2.2.scm at master · cocodrips/SICP
(display "==========Scele tree===========")
(newline)
(define (scale-tree tree factor)
(cond
((null? tree) ())
((not (pair? tree)) (* tree factor))
(else (cons (scale-tree (car tree) factor)
(scale-tree (cdr tree) factor))))
)
(display (scale-tree (list 1 (list 2) 3) 10))
(newline)
(define (scale-tree tree factor)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(scale-tree sub-tree factor)
(* sub-tree factor)))
tree)
)
Ex 2.30
;; ex 2.30
(display "==========Ex 2.30===========")
(newline)
;; 普通に
(define (square-tree tree)
(cond
((null? tree) ())
((not (pair? tree)) (* tree tree))
(else (cons (square-tree (car tree)) (square-tree (cdr tree)))))
)
(display (square-tree (list 1 (list 2) 3)))
(newline)
;(1 (4) 9)
;; map
(define (square-tree tree)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(square-tree sub-tree)
(* sub-tree sub-tree)))
tree)
)
(display (square-tree (list 1 (list 2) 3)))
(newline)
;(1 (4) 9)
Ex 2.31
(display "==========Ex 2.31===========")
(newline)
(define (tree-map tree f)
(map (lambda (sub-tree)
(if (pair? sub-tree)
(tree-map sub-tree f)
(f sub-tree)))
tree)
)
(display (tree-map (list 1 (list 5) 7) (lambda (x) (* x x))))
(newline)
;(1 (25) 49)
Ex 2.32
(display "==========Ex 2.32===========")
(newline)
(define (subsets s)
(if (null? s)
(list ())
(let
((rest (subsets (cdr s))))
(append rest (map (lambda (x) (cons x (car s))) rest)))))
(display (subsets (list 1 4 7)))
(newline)
;(1) -> () (1)
;(1 2 3) : ()
; (2 3) : () (1)
; (3) : () (1) (2) (1 2)
; () : () (1) (2) (1 2) (3) (1 3) (2 3) (1 2 3)