## 2014年6月4日水曜日

### Scheme - データによる抽象の構築(汎用演算のシステム(例: 記号代数(多項式の算術演算、項リストの表現、記号代数における型の階層構造(拡張問題: 有理関数))))

その他参考書籍

コード(BBEdit, Emacs)

sample.scm

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

(define (install-rational-package)
;; 内部手続き
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (make-rat n d) (cons n d))
(make-rat (add (mul (number x) (denom x))
(mul (denom x) (denom y)))
(mul (denom x) (denom y))))
(define (sub-rat x y)
(make-rat (sub (mul (numer x) (denom y))
(mul (numer y) (denom x)))
(mul (denom x) (denom y))))
(define (mul-rat x y)
(make-rat (mul (numer x) (numer y))
(mul (denom x) (numer y))))
(define (div-rat x y)
(make-rat (mul (numer x) (denom y))
(mul (denom x) (numer y))))

;; システムの他の部分へのインターフェース
(define (tag x) (attach-tag 'rational x))
(put 'make 'rational
(lambda (n d) (tag (make-rat n d))))
(lambda (x y) (tag (add-rat x y))))
(put 'sub '(rational rational)
(lambda (x y) (tag (sub-rat x y))))
(put 'mul '(rational rational)
(lambda (x y) (tag (mul-rat x y))))
(put 'div '(ragional ragional)
(lambda (x y) (tag (div-rat x y))))
'done)

(define (make-rational n d)
((get 'make 'rational) n d))
```

```\$ ./sample.scm
\$
```