## 2014年4月3日木曜日

### Scheme - データによる抽象の構築(階層データ構造と閉包性(公認インターフェースとしての並び(並びの演算(matrix, vectorと基本演算))))

その他参考書籍

コード(BBEdit, Emacs)

sample.scm

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

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

(define (dot-product v w)
(accumulate + 0 (map * v w)))

(define (matrix-*-vector m v)
(map (lambda (w)
(dot-product v w))
m))

(define (transpose mat)
(accumulate-n cons '() mat))

(define (matrix-*-matrix m n)
(let ((cols (transpose n)))
(map (lambda (v)
(map (lambda (w)
(dot-product v w))
cols))
m)))

(define v1 '(1 2 3 4))
(define v2 '(4 5 6 6))
(define v3 '(6 7 8 9))
(define m (list v1 v2 v3))
(define n (list '(1 2)
'(3 4)
'(5 6)
'(7 8)))

(for-each (lambda (v)
(print "(matrix-*-vector " m " " v ") => "
(matrix-*-vector m v)))
(list v1 v2 v3))

(print "(transpose " m ") => " (transpose m))

(print "(matrix-*-matrix " m " " n ") => "
(matrix-*-matrix m n))
```

```\$ ./sample.scm
(matrix-*-vector ((1 2 3 4) (4 5 6 6) (6 7 8 9)) (1 2 3 4)) => (30 56 80)
(matrix-*-vector ((1 2 3 4) (4 5 6 6) (6 7 8 9)) (4 5 6 6)) => (56 113 161)
(matrix-*-vector ((1 2 3 4) (4 5 6 6) (6 7 8 9)) (6 7 8 9)) => (80 161 230)
(transpose ((1 2 3 4) (4 5 6 6) (6 7 8 9))) => ((1 4 6) (2 5 7) (3 6 8) (4 6 9))
(matrix-*-matrix ((1 2 3 4) (4 5 6 6) (6 7 8 9)) ((1 2) (3 4) (5 6) (7 8))) => ((50 60) (91 112) (130 160))
\$
```