2015年6月3日水曜日

開発環境

Land of Lisp (M.D. Conrad Barski (著)、川合 史朗 (翻訳)、オライリージャパン)の2章(はじめてのLispプログラム)、2.4(グローバル関数を定義する)をSchemeで取り組んでみる。

2.4(グローバル関数を定義する)

コード(Emacs)

(define print
  (lambda (x)
    (begin (display x)
           (newline))))

(define for-each
  (lambda (proc items)
    (if (not (null? items))
        (begin (proc (car items))
               (for-each proc (cdr items))))))

(define *small* 1)
(define *big* 100)

(define guess-my-number
  (lambda ()
    (quotient (+ *small* *big*) 2)))

(define smaller
  (lambda ()
    (set! *big* (- (guess-my-number) 1))
    (guess-my-number)))

(define bigger
  (lambda ()
    (set! *small* (+ (guess-my-number) 1))
    (guess-my-number)))

(define start-over
  (lambda ()
    (set! *small* 1)
    (set! *big* 100)
    (guess-my-number)))


(begin
  (newline)
  (for-each print
            (list (bigger)
                  (smaller)
                  (smaller)))
  (newline)
  (start-over)
  (for-each print
            (list (bigger)
                  (smaller)
                  (smaller)))
  (newline)
  (start-over)
  (for-each print
            (list (smaller)
                  (bigger)
                  (bigger)))  
  (newline)
  (start-over)
  (for-each print
            (list (smaller)
                  (bigger)
                  (bigger)))  
  (newline)
  (start-over)
  (for-each print
            (list (smaller)
                  (bigger)
                  (smaller))))

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

$ kscheme < sample2_4.scm
kscm> kscm> kscm> kscm> kscm> kscm> kscm> kscm> kscm> 
75
62
56

75
62
56

25
37
43

25
37
43

25
37
31
#<undefined>
kscm> $

0 コメント:

コメントを投稿