; ======================================================================
;
; Structure and Interpretation of Computer Programs
; (trial answer to excercises)
;
; 计算机程序的构造和解释(习题试解)
;
; created: code17 08/19/05
; modified:
; (保持内容完整不变前提下,可以任意转载)
; ======================================================================
;; SICP No.2.33
(define (map p sequence)
(accumulate (lambda (x y) (cons (p x) y)) () sequence))
(define (append seq1 seq2)
(accumulate cons seq2 seq1))
(define (length sequence)
(accumulate (lambda (x y) (+ y 1)) 0 sequence))
;; Test-it:
;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc.
;; > map
;; #<procedure:map>
;; > append
;; #<procedure:append>
;; > length
;; #<procedure:length>
;; ;; 注意到此时map,append,length的原始定义已被新的覆盖,
;; ;; 因为它们现在是procedure而不是primitive
;;
;; > (map sqrt (list 1 9 16 25))
;; (1 3 4 5)
;; > (append (list 1 2 (list 3 4)) (list 5 6))
;; (1 2 (3 4) 5 6)
;; > (length (list 1 2 (list 3 4)))
;; 3