我正在使用球拍练习“实用Lisp”。我正在尝试编写一个函数来提示用户输入,如果他们没有输入“是/是”或“否/ n”,则提示他们。
(define (y-or-n? (text ""))
(if (string=? text "") (display "[y/n]: ") (display (~a text "[y/n]: " #:separator " ")))
(let ([resp (string-downcase (read-line))] [input text])
(cond
[(or (equal? resp "y") (equal? resp "yes")) #t]
[(or (equal? resp "n") (equal? resp "no")) #t]
[else (displayln "please enter yes/y no/n ") (y-or-n? input)])))
除了一个错误外,它几乎完全符合我的要求。打电话
pcl.rkt> (y-or-n? "ripped?")
ripped? [y/n]: incorrect-answer
please enter yes/y no/n
ripped? [y/n]: y
y
#t
; y: undefined;
; cannot reference an identifier before its definition
; in module: "/home/user/Documents/Programming/Lisp/Racket/pcl.rkt"
; internal name: y
; Context:
; /usr/share/racket/collects/racket/repl.rkt:11:26
pcl.rkt>
我必须按两次Enter键才能接受答案,这给了我一个未绑定的标识符错误。有人知道怎么了吗?
P.S我正在使用Racket V 7.0。在Emacs中使用球拍模式运行。