1

Racket を使用して Practical Common Lisp を使用しています。ユーザーに入力を求め、「はい/y」「いいえ/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)])))

1つのバグを除いて、私が望むものとほぼ同じです。通話中

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キーを2回押す必要があり、バインドされていない識別子エラーが発生します。誰が何が悪いのか知っていますか?

PS ラケット V 7.0 を使用しています。Emacs でラケット モードを使用して実行します。

4

0 に答える 0