2015年6月21日日曜日

開発環境

Land of Lisp (M.D. Conrad Barski (著)、川合 史朗 (翻訳)、オライリージャパン)の5章(テキストゲームのエンジンを作る)、5.1(魔法使いのアドベンチャー)を Scheme で取り組んでみる。

5.1(魔法使いのアドベンチャー)

コード(Emacs)

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

  ;; kscheme に assoc 手続きはまだ実装してない
  (define assoc
    (lambda (obj alist)
      (if (null? alist)
          #f
          (let ((item (car alist)))
            (if (equal? obj (car item))
                item
                (assoc obj (cdr alist)))))))
  
  (define *nodes*
    (quote ((living-room (You are in the living-room. A wizard is snoring
                              loudly on the couch.))
            (garden (You are in a beatiful garden. There is a well in front of
                         you.))
            (attic (You are in the attic. There is a giant welding torch in
                        the corner.)))))                        
  (print (assoc (quote garden) *nodes*))

  (define describe-location
    (lambda (location nodes)
      (let ((item (assoc location nodes)))
        (if item
            (cadr item)
            #f))))
  (print (describe-location (quote living-room) *nodes*))
  (quote done))

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

$ kscheme < sample1.scm
kscm> 
(garden (You are in a beatiful garden. There is a well in front of you.))
(You are in the living-room. A wizard is snoring loudly on the couch.)
done
kscm> $

0 コメント:

コメントを投稿