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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -