2016年10月18日火曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第2章(データによる抽象の構築)、2.1(データ抽象入門)、2.1.3(データとは何か)、問題2.5.を取り組んでみる。

その他参考書籍

問題2.5.

コード(Emacs)

(begin
  (load "procedures.scm")
  (newline)
  (define (p x) (display x) (newline))

  ;; cons、car、cdr と他の手続きの依存関係の問題(kscheme) で別名に。  
  (define (my-cons a b) (* (expt 2 a) (expt 3 b)))

  (define (my-car pair)
    (define (iter n result)
      (if (= (remainder n 2) 0)
          (iter (/ n 2) (+ result 1))
          result))
    (iter pair 0))
    
  (define (my-cdr pair)
    (define (iter n result)
      (if (= (remainder n 3) 0)
          (iter (/ n 3) (+ result 1))
          result))
    (iter pair 0))

  (define (p a b)
    ((lambda (pair)
       (display pair) (display " ") (display (my-car pair)) (display " ")
       (display (my-cdr pair)) (newline))
     (my-cons a b)))

  (define nums '(0 1 2 3 4 5 6 7 8 9))

  (for-each (lambda (a)
              (for-each (lambda (b)
                          (p a b))
                        nums))
            nums)

  'done)

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

$ ksi < sample5.scm
ksi> 
1 0 0
3 0 1
9 0 2
27 0 3
81 0 4
243 0 5
729 0 6
2187 0 7
6561 0 8
19683 0 9
2 1 0
6 1 1
18 1 2
54 1 3
162 1 4
486 1 5
1458 1 6
4374 1 7
13122 1 8
39366 1 9
4 2 0
12 2 1
36 2 2
108 2 3
324 2 4
972 2 5
2916 2 6
8748 2 7
26244 2 8
78732 2 9
8 3 0
24 3 1
72 3 2
216 3 3
648 3 4
1944 3 5
5832 3 6
17496 3 7
52488 3 8
157464 3 9
16 4 0
48 4 1
144 4 2
432 4 3
1296 4 4
3888 4 5
11664 4 6
34992 4 7
104976 4 8
314928 4 9
32 5 0
96 5 1
288 5 2
864 5 3
2592 5 4
7776 5 5
23328 5 6
69984 5 7
209952 5 8
629856 5 9
64 6 0
192 6 1
576 6 2
1728 6 3
5184 6 4
15552 6 5
46656 6 6
139968 6 7
419904 6 8
1259712 6 9
128 7 0
384 7 1
1152 7 2
3456 7 3
10368 7 4
31104 7 5
93312 7 6
279936 7 7
839808 7 8
2519424 7 9
256 8 0
768 8 1
2304 8 2
6912 8 3
20736 8 4
62208 8 5
186624 8 6
559872 8 7
1679616 8 8
5038848 8 9
512 9 0
1536 9 1
4608 9 2
13824 9 3
41472 9 4
124416 9 5
373248 9 6
1119744 9 7
3359232 9 8
10077696 9 9
=> done
ksi> $

0 コメント:

コメントを投稿