開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション)の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.1(例: 有理数の算術演算子)の問題 2.1を解いてみる。
その他参考書籍
問題 2.1
コード
sample.scm
(define (make-rat n d) (let ((g (gcd n d))) (if (> d 0) (cons (/ n g) (/ d g)) (cons (* -1 (/ n g)) (* -1 (/ d g)))))) (define (numer x) (car x)) (define (denom x) (cdr x)) (define (print-rat x) (newline) (display (numer x)) (display "/") (display (denom x))) (define a (make-rat 5 10)) (define c (make-rat 5 -10)) (define b (make-rat -5 10)) (define d (make-rat -5 -10))
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (print-rat a) 1/2 ;Unspecified return value 1 ]=> (print-rat b) -1/2 ;Unspecified return value 1 ]=> (print-rat c) -1/2 ;Unspecified return value 1 ]=> (print-rat d) 1/2 ;Unspecified return value
0 コメント:
コメントを投稿