開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.1(データ抽象入門)、有理数の表現、問題 2.1を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 2.1
コード(BBEdit, Emacs)
sample.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; これまでに書いた手続き
(load "./procedures.scm")
(define (make-rat n d)
(let ((sign (if (or (and (>= n 0)
(>= d 0))
(and (< n 0)
(< d 0)))
1
-1))
(new-n (abs n))
(new-d (abs d)))
(let ((g (gcd new-n new-d)))
(cons (* sign
(/ new-n g))
(/ new-d g)))))
(for-each (lambda (n)
(print-rat (make-rat n -10)))
'(1 2 3 4 5))
(for-each (lambda (n)
(print-rat (make-rat (* -1 n)
10)))
'(1 2 3 4 5))
(for-each (lambda (n)
(print-rat (make-rat n 10)))
'(1 2 3 4 5))
(for-each (lambda (n)
(print-rat (make-rat (* -1 n)
-10)))
'(1 2 3 4 5))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
gosh> -1/10 -1/5 -3/10 -2/5 -1/2 -1/10 -1/5 -3/10 -2/5 -1/2 1/10 1/5 3/10 2/5 1/2 1/10 1/5 3/10 2/5 1/2 #t gosh>
0 コメント:
コメントを投稿