2015年4月25日土曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.2(階層データ構造と閉包性)、2.2.3(公認インターフェースとしての並び)、並びの演算、問題2.33.を解いてみる。

その他参考書籍

問題2.33.

コード(BBEdit, Emacs)

(define square (lambda (x) (* x x)))

(define accumulate
  (lambda (op initial sequence)
    (if (null? sequence)
        initial
        (op (car sequence)
            (accumulate op initial (cdr sequence))))))

(define map
  (lambda (p sequence)
    (accumulate (lambda (x y)
                  (cons (p x) y))
                '()
                sequence)))

(define append
  (lambda (seq1 seq2)
    (accumulate cons
                seq2
                seq1)))

(define length
  (lambda (sequence)
    (accumulate (lambda (x y) (+ 1 y))
                0
                sequence)))

(map square '(1 2 3 4 5))
(append '(1 2) '(3 4 5))
(length '(1 2 3 4 5))

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ kscheme < sample33.scm
In : Out: square
;(total-pushes = 3 maximum-depth = 3)
In : Out: accumulate
;(total-pushes = 3 maximum-depth = 3)
In : Out: map
;(total-pushes = 3 maximum-depth = 3)
In : Out: append
;(total-pushes = 3 maximum-depth = 3)
In : Out: length
;(total-pushes = 3 maximum-depth = 3)
In : Out: (1 4 9 16 25)
;(total-pushes = 188 maximum-depth = 20)
In : Out: (1 2 3 4 5)
;(total-pushes = 62 maximum-depth = 11)
In : Out: 5
;(total-pushes = 150 maximum-depth = 20)
In : $

0 コメント:

コメントを投稿