計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- kscheme, Gauche (処理系)
計算機プログラムの構造と解釈[第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.2(階層構造)、木の写像、問題2.31.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題2.31.
コード(BBEdit, Emacs)
(define square (lambda (x) (* x x)))
(define tree-map
(lambda (proc tree)
(cond ((null? tree) '())
((not (pair? tree))
(proc tree))
(else
(cons (tree-map proc (car tree))
(tree-map proc (cdr tree)))))))
(define square-tree
(lambda (tree)
(tree-map square tree)))
(define square-tree1
(lambda (tree)
(cond ((null? tree) '())
((not (pair? tree))
(square tree))
(else
(cons (square-tree (car tree))
(square-tree (cdr tree)))))))
(display
(square-tree1
(list 1
(list 2 (list 3 4) 5)
(list 6 7))))
(newline)
(display
(square-tree
(list 1
(list 2 (list 3 4) 5)
(list 6 7))))
(newline)
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ gosh sample31.scm (1 (4 (9 16) 25) (36 49)) (1 (4 (9 16) 25) (36 49)) $
0 コメント:
コメントを投稿