2015年10月31日土曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Scheme (プログラミング言語)
  • kscheme (github) (処理系)

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第3章(標準部品化力、オブジェクトおよび状態)、3.3(可変データのモデル化)、3.3.3(表の表現)、局所表の作り方、問題3.24.を解いてみる。

その他参考書籍

問題3.24.

コード(Emacs)

(begin
  (define (print obj)
    (display obj)
    (newline))

  (define (make-table same-key?)
    (let ((local-table (list '*table*)))
      (define (assoc key records)
        (cond ((null? records) #f)
              ((same-key? key (caar records)) (car records))
              (else (assoc key (cdr records)))))
      (define (lookup key-1 key-2)
        (let ((subtable (assoc key-1 (cdr local-table))))
          (if subtable
              (let ((record (assoc key-2 (cdr subtable))))
                (if record
                    (cdr record)
                    #f))
              #f)))
      (define (insert! key-1 key-2 value)
        (let ((subtable (assoc key-1 (cdr local-table))))
          (if subtable
              (let ((record (assoc key-2 (cdr subtable))))
                (if record
                    (set-cdr! record value)
                    (set-cdr! subtable
                              (cons (cons key-2 value)
                                    (cdr subtable)))))
              (set-cdr! local-table
                        (cons (list key-1
                                    (cons key-2 value))
                              (cdr local-table)))))
        'ok)
      (define (dispatch m)
        (cond ((eq? m 'lookup-proc) lookup)
              ((eq? m 'insert-proc!) insert!)
              (else (display "Unknown operation -- TABLE ")
                    (print m))))
    dispatch))

  ;; 剰余類分解 (同値関係)
  (define operation-table (make-table (lambda (a b)
                                        (= (floor-remainder a 2)
                                           (floor-remainder b 2)))))
  (define get (operation-table 'lookup-proc))
  (define put (operation-table 'insert-proc!))

  (print (put 1 2 'a))
  (print (put 4 3 'b))
  (print (get 3 4))                     ; a
  (print (get 2 1))                     ; b
  (print (get 1 3))                     ; #f
  (print (get 4 4))                     ; #f
  (print (put 4 6 'c))
  (print (get 10 100))                  ; c
  )

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

$ ./kscheme sample24.scm
ok
error: unbound variable -- ok$
$ gosh sample24.scm
ok
ok
a
b
#f
#f
ok
c
$ guile < sample24.scm
GNU Guile 2.0.11
Copyright (C) 1995-2014 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
ok
ok
a
b
#f
#f
ok
c
scheme@(guile-user)> 
$ # kscheme に問題があるみたい

0 コメント:

コメントを投稿