2015年2月2日月曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の1(手続きによる抽象の構築)、1.2(手続きとその生成するプロセス)、1.2.4(べき乗)、問題 1.16.を解いてみる。

その他参考書籍

問題 1.16.

コード(BBEdit, Emacs)

sample16.scm

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

(define square (lambda (x) (* x x)))

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

(define (fast-expt b n a)
  (cond ((= n 0) a)
        ((even? n)
         (fast-expt (square b)
                    (/ n 2)
                    a))
        (else
         (fast-expt b
                    (- n 1)
                    (* b a)))))
(define (expt b n)
  (fast-expt b n 1))

(print (expt 2 10))
(print (expt 2 31))

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

$ ./sample16.scm
1024
2147483648
$

0 コメント:

コメントを投稿