開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Scheme (プログラミング言語)
- kscheme (ksi)(github) (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第2章(データによる抽象の構築)、2.1(データ抽象入門)、2.1.1(例: 有理数の算術演算)、問題2.1.を取り組んでみる。
その他参考書籍
問題2.1.
コード(Emacs)
(begin ;; (load "procedures.scm") (newline) (define (p x) (display x) (newline)) (define (make-rat n d) ((lambda (g) ((lambda (n d) (if (positive? (* n d)) (cons (abs n) (abs d)) (cons (- (abs n)) (abs d)))) (/ n g) (/ d g))) (gcd n d))) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (print-rat x) (newline) (display (numer x)) ((lambda (d) (if (not (= d 1)) ((lambda () (display "/") (display d))))) (denom x))) (define nums '(1 2 3 4 5 -6 -7 -8 -9 -10)) (for-each (lambda (n) (for-each (lambda (d) (print-rat (make-rat n d))) nums)) nums) (newline) 'done)
入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))
$ ksi < sample1.scm ksi> 1 1/2 1/3 1/4 1/5 -1/6 -1/7 -1/8 -1/9 -1/10 2 1 2/3 1/2 2/5 -1/3 -2/7 -1/4 -2/9 -1/5 3 3/2 1 3/4 3/5 -1/2 -3/7 -3/8 -1/3 -3/10 4 2 4/3 1 4/5 -2/3 -4/7 -1/2 -4/9 -2/5 5 5/2 5/3 5/4 1 -5/6 -5/7 -5/8 -5/9 -1/2 -6 -3 -2 -3/2 -6/5 1 6/7 3/4 2/3 3/5 -7 -7/2 -7/3 -7/4 -7/5 7/6 1 7/8 7/9 7/10 -8 -4 -8/3 -2 -8/5 4/3 8/7 1 8/9 4/5 -9 -9/2 -3 -9/4 -9/5 3/2 9/7 9/8 1 9/10 -10 -5 -10/3 -5/2 -2 5/3 10/7 5/4 10/9 1 => done ksi> $
0 コメント:
コメントを投稿