計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.3(可変データでのモデル化)、3.3.1(可変リスト構造)、問題 3.14.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.14.
mysteryはリストの逆順を返す手続き。元のリストは先頭の要素のみのリストになる。
vが束縛されているリストを表現する箱とポインタ図。
(define (mystery v))を評価した後の構造vとwを示す箱とポインタ図。
vとwの値として、それぞれ(a)、(d c b a)が印字される。
確認。
コード(BBEdit, Emacs)
sample3_13.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; mysteryは1つの引数リストをとる手続きで、そのリストの逆順のリストを返す
(define (mystery x)
(define (loop x y)
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))
(loop x '()))
(for-each (lambda (items)
(display items)
(display ": ")
(print (mystery items)))
(list (list)
(list 0)
(list 1 2 3 4 5)))
(define v (list 'a 'b 'c 'd))
(define w (mystery v))
(print v) ; (a)
(print w) ; (d c b a)
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample3_14.scm (): () (0): (0) (1 2 3 4 5): (5 4 3 2 1) (a) (d c b a) $
0 コメント:
コメントを投稿