2015年6月30日火曜日

開発環境

Land of Lisp (M.D. Conrad Barski (著)、川合 史朗 (翻訳)、オライリージャパン)の5章(世界とのインターフェース: Lisp でのデータの読み書き)、6.1(テキストの表示と読み書き)を Scheme で取り組んでみる。

6.1(テキストの表示と読み書き)

コード(Emacs)

(begin   
  (newline)
  ;; Common Lisp の print 手続きは Scheme にはないっぽいので定義
  ;; Gauche には print はあるけど、write ではなく、display と同様の機能みたい
  (define print (lambda (x) (write x) (newline)))  
  (print "foo")
  (begin (print "this")
         (print "is")
         (print "a")
         (print "test"))
  ;; Common Lisp の print1 手続きは Scheme にはない
  ;; write が同様の手続きに近いっぽい
  (begin (write "this")
         (write "is")
         (write "a")
         (write "test"))

  (newline)

  ;; kscheme に入力に関係する手続き( read, read-line )がうまく実装できてないっぽいから
  ;; 関係するコメントアウトしてある箇所は、対話型コマンドラインに直接入力
  
  ;; p.79 訳注 と少し動作違うかも
  ;; (define say-hello
  ;;   (lambda ()
  ;;     (print "Please type your name:")
  ;;     (let ((name (read)))
  ;;       (print "Nice to meet you, ")
  ;;       (print name))))
  
  ;; (say-hello)

  ;; (define add-five
  ;;   (lambda ()
  ;;     (print "please enter a number:")
  ;;     (let ((num (read)))
  ;;       (print "When I add five I get")
  ;;       (print (+ num 5)))))
  
  ;; (add-five)

  (print (quote 3))
  (print (quote 3.4))
  (print (quote foo))
  (print (quote "foo"))
  ;; kscheme に文字型は、まだ未実装
  (print (quote "a"))

  ;; Common Lisp の princ 手続きは Scheme の display 手続きと同様の機能っぽい
  (display (quote 3))
  (display (quote 3.4))
  (display (quote foo))
  (display (quote "foo"))
  (display (quote "a"))

  (newline)
  (begin (display "This sentence will be interrupted")
         (display "
")
         (display "by an annoying newline character."))

  ;; kscheme の read-line 手続きの実装は、Scheme の仕様とは若干違うかも
  ;; C言語の readline 関数を使って実装してある
  ;; (define say-hello
  ;;   (lambda ()
  ;;     (display "Please type your name:")
  ;;     (let ((name (read-line)))
  ;;       (display "Nice to meet you, ")
  ;;       (display name))))

  ;; (say-hello)
  (quote done))

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

$ kscheme < sample1.scm
kscm> 
"foo"
"this"
"is"
"a"
"test"
"this""is""a""test"
3
0.34e1
foo
"foo"
"a"
30.34e1foofooa
This sentence will be interrupted
by an annoying newline character.done
kscm> $ kscheme 
kscm> (define print (lambda (x) (write x) (newline)))  
kscm> 
  (define say-hello
    (lambda ()
      (print "Please type your name:")
      (let ((name (read)))
        (print "Nice to meet you, ")
        (print name))))

kscm> (say-hello)
"Please type your name:"
"bob"
"Nice to meet you, "
"bob"
#<undefined>
kscm> 
  (define add-five
    (lambda ()
      (print "please enter a number:")
      (let ((num (read)))
        (print "When I add five I get")
        (print (+ num 5)))))
kscm> (add-five)
"please enter a number:"
4
"When I add five I get"
9
#<undefined>
kscm> 
  (define say-hello
    (lambda ()
      (display "Please type your name:")
      (let ((name (read-line)))
        (display "Nice to meet you, ")
        (display name))))
kscm> (say-hello)
Please type your name:Bob O'Malley
Nice to meet you, Bob O'Malley#<undefined>
kscm> $ 

0 コメント:

コメントを投稿

Comments on Google+: