I'm going through Practical Common Lisp using Racket. I'm trying to write a function that prompts the user for an input, and reprompts them if they don't input "yes/y" "no/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)])))
It does almost exactly what I want except for one bug. Calling
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>
I have to hit enter twice for the answer to be accepted and it gives me an unbound identifier error. Does anyone know whats wrong?
P.S I'm using Racket V 7.0. Running using racket-mode in Emacs.