Reading stuff can be actually done in a few ways. The number one cause of confusion is that "read" is a primitive to read a single lisp form (or s-expression) which is a single string (with double quotes), a list, number, character literal (like #\a or #\newline) or whatever the interactive top-level accepts as one thing. So typing into top-level
Code: Select all
> (begin
(display "What's your name? ")
(if (symbol? (read))
(display "That's a symbol!\n")))
What's your name? Mystran
That's a symbol!
>
Notice that you can write a top-level loop as:
Code: Select all
(defun my-read-eval-print-loop ()
(print (eval (read)))
(my-read-eval-print-loop))
So "read" is not simply a reader, put a whole parser. The reason stuff is converted to lower-case is that symbols are (in most implementations) printed as lower-case, why they are truly case-insensitive.
Yup, it's a parser
If you want to read a whole line into a string, you can use "readline":
Code: Select all
> (begin (read-char) (read-line))
foo bar duh
"foo bar duh"
>
The reasons for the extra (read-char) is simply that MzScheme at least seems to think that I want to start reading the line before the evaluation-triggering first newline, so I have to eat-that first. If you run stuff from a file or something, you can ignore this problem. It's just because the newline after parsed form is not part of the parsed form.
Code: Select all
> (display "foo\n")(display "bar\n")
foo
> bar
>
So it gives a prompt even if there already is more stuff..
Ok, this might be confusing, but play around with the above, and you should get it quite fast.
There are other ways to read stuff, and if you absolutely can't do what you need to do with the built-ins, you can always parse stuff yourself using "read-char" and "peek-char" which return the next character in the input-stream, while "read-char" also removes it from the stream and "peek-char" does not.
There are also "format" functions around, which pretty much act like printf/scanf but unfortunately they aren't part of R5RS, so one needs to get those as extra libraries.
I'd also like to point to two books in the net,
Teach yourself Scheme in Fixnum Days which is intended as a scheme tutorial for folks that already know how to program, and
Structure and Interpretation of Computer Programs which starts from programming basics, using Scheme, and continues through evaluation models to interpretation, compilation, including stuff like basics of garbage-collection. SICP is definitely a book that everyone should read, if they want to learn basics of programming language implementation.
Both books are completely available in the above URLs.