[Lisp] このエラーに若干ハマった「ERROR: syntax-error: the form can appear only in the toplevel」

相変わらず「プログラミングGauche」読んでます。
9.1集合の部分を読んでいて、リストから見つかった要素を1つ削除する手続きを定義したのですがエラーになってしまいました。(写経したのに..)
問題のコードは以下の通り

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! /usr/bin/env gosh
;; coding: utf8
 
(define *inventory* (list 'potion 'potion 'dagger 'cokkie 'dagger))
 
(define (sub-member item list)
  (cond ((null? #?=list) #f)
        ((equal? item (car list)) list)
        (else (sub-member item (cdr list)))))
 
(define (delete-1 elt lis . options)
  (let-optional* options ((cmp-fn equal?))
    (define (loop lis)
      (cond [(null? lis) '()]
            [(cmp-fn elt (car lis)) (cdr lis)]
            [else (cons (car lis) (loop (cdr lis)))]))
    (loop lis)))
 
(define (main args)
  (print (delete-1 'potion *inventory*))
  0)

実行しようとしたら以下のエラーが... :(

1
2
3
4
5
6
7
8
9
10
:!gosh /private/tmp/test.scm
*** ERROR: syntax-error: the form can appear only in the toplevel: (define (loop lis) (cond ((null? lis) (quote ())) (
(cmp-fn elt (car lis)) (cdr lis)) (else (cons (car lis) (loop (cdr lis))))))
    While compiling "/private/tmp/test.scm" at line 11: (define (delete-1 elt lis . options) (let-optional* options ((
cmp-fn equal?)) (define (loop lis) (con ...
    While loading "/private/tmp/test.scm" at line 17
Stack Trace:
_______________________________________
 
シェルが値を返しました 70

原因

let-optionals*のスペルが間違っていました...
括弧の位置などはあっていたので、condとcons間違えていないかとか調べましたが、原因はlet-optionalsがスペルミスしていました。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#! /usr/bin/env gosh
;; coding: utf8
 
(define *inventory* (list 'potion 'potion 'dagger 'cokkie 'dagger))
 
(define (sub-member item list)
  (cond ((null? #?=list) #f)
        ((equal? item (car list)) list)
        (else (sub-member item (cdr list)))))
 
(define (delete-1 elt lis . options)
  (let-optionals* options ((cmp-fn equal?))
    (define (loop lis)
      (cond [(null? lis) '()]
            [(cmp-fn elt (car lis)) (cdr lis)]
            [else (cons (car lis) (loop (cdr lis)))]))
    (loop lis)))
 
(define (main args)
  (print (delete-1 'potion *inventory*))
  0)

初めての言語の場合、エラーの時の勘所を抑えるまで単純なエラーでもハマってしまいます。
その辺りは場数を踏むしかないのでしょうか。

0 件のコメント :

コメントを投稿