2014年1月31日金曜日

開発環境

計算機プログラムの構造と解釈(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.17.を解いてみる。

その他参考書籍

問題 1.17.

コード(BBEdit, Emacs)

sample.scm

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

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

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

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

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

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

$ gosh
gosh> (load "./sample.scm")
#t
gosh> (fast-* 0 0)
0
gosh> (fast-* 0 1)
0
gosh> (fast-* 1 0)
0
gosh> (fast-* 1 1)
1
gosh> (fast-* 1 2)
2
gosh> (fast-* 2 0)
0
gosh> (fast-* 2 1)
2
gosh> (fast-* 2 2)
4
gosh> (fast-* 2 3)
6
gosh> (fast-* 3 0)
0
gosh> (fast-* 3 1)
3
gosh> (fast-* 3 2)
6
gosh> (fast-* 3 3)
9
gosh> (fast-* 3 4)
12
gosh> (fast-* 12 34)
408
gosh> (exit)
$

0 コメント:

コメントを投稿