計算機プログラムの構造と解釈[第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.17.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.17.
コード(BBEdit, Emacs)
sample3_17.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define (count-pairs x)
(let ((counted '()))
(define (iter y)
(cond ((not (pair? y)) 0)
((memq y counted) 0)
(else
(set! counted (cons y counted))
(+ (iter (car y))
(iter (cdr y))
1))))
(iter x)))
(define p1 (cons 'c '()))
(define p2 (cons p1 p1))
(define list1 (cons 'a (cons 'b p1)))
(print (count-pairs list1)) ; 3
(define list2 (cons 'a (cons p1 p1)))
(print (count-pairs list2)) ; 3
(define list3 (cons p2 p2))
(print (count-pairs list3)) ; 3
(define list4 (cons 'a (cons 'b p1)))
(set-cdr! (cddr list4) list4)
(print (count-pairs list4)) ; 3
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample3_17.scm 3 3 3 3 $
0 コメント:
コメントを投稿