I think I'm confused about how Scheme works with user input. I want to just read some values from the console that the user inputs as the script runs. For example, if I wanted to add a user's value to 3, I was under the impression I could use:

(+ 3 (read)) 

I could also enclose it in a display function to see the output. This is what I expected, but when I try to do it, it only waits for the next code in the console. I am trying biwascheme.org and ideone.com as the interpreters.

So, I am hoping to find out the very basic way to force the console to wait for the user input and then put it into the (read) spot. How?

2 Answers

You need to use a runtime that actually supports reading from standard input. Most web-based runtimes probably won't support that (though Ideone seems to let you specify standard input contents ahead of time (not interactively), see example).

You should install Racket. The DrRacket program will actually give you a nice prompt whenever read is called.

1

I have found that repl.it's online programming environment for biwascheme does support reading user input using the (read) function, although it doesn't seem to be documented on the biwascheme reference.

As an example, in a Scheme repl on repl.it, type this on the left-hand side:

(define (user-says-y) (eq? (read) 'y)) (define (take-user-input) (if (user-says-y) (print "entered y!") (print "did not enter y!"))) 

Save, run and then call the function from the terminal on the right-hand side:

> (take-user-input) 

If you enter y you'll see the entered y! message, if you enter n you'll see the did not enter y! message.

Magic.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy