syntax - Scheme: What type is read? -
when read user input console in scheme, type need use if i'm converting type want?
for example (string->number "20") converts string number, syntax regarding read?
for example
(define input(read) (let ((r read)) (????->number r)))
if @ racket documentation read
see signature : (read [in]) → any
.
so in case if user inputs number return number. explicitly check number because can't sure user won't input else!
an example :
(define (read-number) (let ((inpt (read))) (if (number? inpt) inpt (begin (display "please input number!") (newline) (read-number)))))
edit : if want test if inputted number zero, should replace if
-statement conditional.
(cond ((and (number? inpt) (= inpt 0)) ; works because of lazy evalutation ; user inputted 0 ...) ((number? inpt) ; user inputted number other 0 ...) (else ; user did not entered number! (display "please input number!") (newline) (read-number)))
Comments
Post a Comment