計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Yosemite - 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))の1(手続きによる抽象の構築)、1.3(高階手続きによる抽象)、1.3.3(一般方法としての手続き)、問題 1.39.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
- Scheme手習い
問題 1.39.
コード(BBEdit, Emacs)
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
(define square (lambda (x) (* x x)))
(define cont-frac
(lambda (n d k)
(define iter
(lambda (i result)
(if (= i 1)
result
(iter (- i 1)
(/ (n (- i 1))
(+ (d (- i 1))
result))))))
(iter k (/ (n k)
(d k)))))
(define tan-cf
(lambda (x k)
(cont-frac
(lambda (i)
(if (= i 1)
x
(* -1 (square x))))
(lambda (i) (- (* 2 i) 1))
k)))
(define k 100)
(for-each
(lambda (x)
(display "tan ") (display x) (display " = ") (display (* 1.0 (tan-cf x k)))
(newline))
(list 0 1 2 3 4 5 6 7 8 9 10))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample39.scm tan 0 = 0.0 tan 1 = 1.557407724654902 tan 2 = -2.185039863261519 tan 3 = -0.1425465430742778 tan 4 = 1.1578212823495777 tan 5 = -3.380515006246586 tan 6 = -0.2910061913847492 tan 7 = 0.8714479827243188 tan 8 = -6.799711455220379 tan 9 = -0.4523156594418099 tan 10 = 0.6483608274590866 $
0 コメント:
コメントを投稿