Scheme 手習い

The Little Schemer の日本語版を読みましょう、な勉強会が実施される予定なのですが、なかなか始まらないので先んじて試験してみる事に。

準備

とりあえず atom? なファイルを作って以下を定義。

(define atom?
  (lambda (x)
    (and (not (pair? x)) (not (null? x)))))

で、gauche.test 使えば良いのかな。とりあえず最初の質問が以下なカンジ?

(use gauche.test)
(add-load-path ".")
(load "func")

(test-start "chapter 1")
(test-section "atom")
(test* "(atom? 'atom)" #t (atom? 'atom))

(test-end)

テスツ。

$ gosh 1.scm 
Testing chapter 1 ...                                            
<atom>-------------------------------------------------------------------------
test (atom? 'atom), expects #t ==> ok
passed.
$

ヨシ。というかテキストには_文字列_という記述があるけど、シンボル、って事でいいよね。このままいくつか追加。あ、下に (quote atom) で OK と書いてありますね。

最初のナニは

以下になりました。ちょっとだけ端折ってますが。

(use gauche.test)
(add-load-path ".")
(load "func")

(test-start "chapter 1")
(test-section "atom")
(test* "(atom? 'atom)" #t (atom? 'atom))
(test* "(atom? 'turkey)" #t (atom? 'turkey))
(test* "(atom? 1492)" #t (atom? 1492))
(test* "(atom? 'u)" #t (atom? 'u))
(test* "(atom? '*abc$)" #t (atom? '*abc$))
(test* "(list? '(atom))" #t (list? '(atom)))
(test* "(list? '(atom turkey or))" #t (list? '(atom turkey or)))
(test* "(list? '(atom turkey) 'or)" *test-error* (list? '(atom turkey) 'or))
(test* "(list? '((atom turkey) or))" #t (list? '((atom turkey) or)))
(test* "(list? '(((how) are) ((you) (doing so)) far))"
       #t
       (list? '(((how) are) ((you) (doing so)) far)))
(test* "(length '(((how) are) ((you) (doing so)) far))"
       3
       (length '(((how) are) ((you) (doing so)) far)))
(test* "(list? '())" #t (list? '()))
(test* "(atom? '())" #f (atom? '()))
(test* "(list? '(() () () ()))" #t (list? '(() () () ())))
(test* "(eq? 'a (car '(a b c)))" #t (eq? 'a (car '(a b c))))
(test* "(equal? '(a b c) (car '((a b c) x y z)))"
       #t
       (equal? '(a b c) (car '((a b c) x y z))))
(test* "(car 'hotdog)" *test-error* (car 'hotdog))
(test* "(car '())" *test-error* (car '()))

(test-end)

ファイル名は 1.scm で実行な出力が以下。

$ gosh 1.scm 
Testing chapter 1 ...                                            
<atom>-------------------------------------------------------------------------
test (atom? 'atom), expects #t ==> ok
test (atom? 'turkey), expects #t ==> ok
test (atom? 1492), expects #t ==> ok
test (atom? 'u), expects #t ==> ok
test (atom? '*abc$), expects #t ==> ok
test (list? '(atom)), expects #t ==> ok
test (list? '(atom turkey or)), expects #t ==> ok
test (list? '(atom turkey) 'or), expects #<error> ==> ok
test (list? '((atom turkey) or)), expects #t ==> ok
test (list? '(((how) are) ((you) (doing so)) far)), expects #t ==> ok
test (length '(((how) are) ((you) (doing so)) far)), expects 3 ==> ok
test (list? '()), expects #t ==> ok
test (atom? '()), expects #f ==> ok
test (list? '(() () () ())), expects #t ==> ok
test (eq? 'a (car '(a b c))), expects #t ==> ok
test (equal? '(a b c) (car '((a b c) x y z))), expects #t ==> ok
test (car 'hotdog), expects #<error> ==> ok
test (car '()), expects #<error> ==> ok
passed.
$

そろそろ始めませんか、@hanachin_ となく。