2014年2月1日土曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.2(手続きとその生成するプロセス)、1.2.4(べき乗)、問題 1.18.を解いてみる。

その他参考書籍

問題 1.18.

コード(BBEdit, Emacs)

sample.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

(define (double x) (+ x x))

(define (halve x) (/ x 2))

(define (* a b)
  (*-iter a b 0))

(define (*-iter a b c)
  (cond ((= b 0) c)
        ((even? b) (*-iter (double a) (halve b) c))
        (else (*-iter a (- b 1) (+ c a)))))

(define (even? n)
  (= (remainder n 2) 0))

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

$ gosh
gosh> (load "./sample.scm") 
#t
gosh> (* 0 0)
0
gosh> (* 0 1)
0
gosh> (* 1 0)
0
gosh> (* 1 1)
1
gosh> (* 1 2)
2
gosh> (* 2 0)
0
gosh> (* 2 2)
4
gosh> (* 2 3)
6
gosh> (* 5 6)
30
gosh> (* 6 5)
30
gosh> (exit)
$

0 コメント:

コメントを投稿