## 2014年5月16日金曜日

### Scheme - データによる抽象の構築(抽象データの多重表現(データ主導プログラミングと加法性(複素数をメッセージパッシングで実装)))

その他参考書籍

コード(BBEdit, Emacs)

sample.scm

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

;; これまでに書いた手続き

(define (make-from-mag-ang r a)
(define (dispatch op)
(cond ((eq? op 'real-part)
(* r (cos a)))
((eq? op 'imag-part)
(* r (sin a)))
((eq? op 'magnitude) r)
((eq? op 'angle) a)
(else
(error "Unknown op -- MAKE-FROM-MAG-ANG" op))))
dispatch)

(define pi 3.14)
(define a (make-from-mag-ang 1 0))
(define b (make-from-mag-ang 2 0))
(define c (make-from-mag-ang 0 (/ pi 2)))
(define d (make-from-mag-ang 0 pi))
(define e (make-from-mag-ang 2 (/ pi 6)))
(define f (make-from-mag-ang (sqrt 2) (/ pi 4)))
(define g (make-from-mag-ang 2 (/ pi 3)))
(define h (make-from-mag-ang 2 (* 5 (/ pi 6))))
(define i (make-from-mag-ang (sqrt 2) (* 5 (/ pi 4))))
(define j (make-from-mag-ang 2 (* 5 (/ pi 3))))

(for-each (lambda (x)
(print x "\n"
"実部: " (x 'real-part)
" 虚部: " (x 'imag-part)
" 絶対値: " (x 'magnitude)
" 偏角: " (x 'angle)))
(list a b c d e f g h i j))
```

```\$ ./sample.scm
#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

#<closure (make-from-mag-ang dispatch)>

\$
```