2015年4月29日水曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.2(階層データ構造と閉包性)、2.2.3(公認インターフェースとしての並び)、並びの演算、問題2.37.を解いてみる。

その他参考書籍

問題2.37.

コード(BBEdit, Emacs)

(define accumulate
  (lambda (op initial sequence)
    (if (null? sequence)
        initial
        (op (car sequence)
            (accumulate op initial (cdr sequence))))))

(define accumulate-n
  (lambda (op init seqs)
    (if (null? (car seqs))
        '()
        (cons (accumulate op init (map car seqs))
              (accumulate-n op init (map cdr seqs))))))
               
(define dot-product
  (lambda (v w)
    (accumulate + 0 (map * v w))))

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

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

(define matrix-*-matrix
  (lambda (m n)
    ((lambda (cols)
       (map (lambda (col)
              (map (lambda (row)
                     (dot-product row col))
                   m))
            cols))
     (transpose n))))

(define m '((1 2 3 4)
            (4 5 6 6)
            (6 7 8 9)))
(define n '((1 2 3)
            (4 5 6)
            (7 8 9)
            (10 11 12)))

(display (matrix-*-matrix m n))
(newline)

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ gosh sample37.scm
((70 126 180) (80 147 210) (90 168 240))
$

0 コメント:

コメントを投稿