I’m trying to read several inputs at once in STDIN, and I think read/recursive
is what I’m looking for. However, it returns a placeholder/eof, and I want it to become a list I can reference after reading. any tips?
@joslarki I think you just want read
Is there a reason that read
by itself doesn’t work?
No read
does work, I wasn’t wrapping the numbers I input in parentheses. Thanks!
read
works just on numbers too
@joslarki If you want to read
multiple times, accumulating to a list, you could probably do something like (for/list ([v (in-port read)]) v)
.
You might first want to (file-stream-buffer-mode (current-input-port) 'none)
— input might have line buffering by default.
In which case you won’t see any values until you hit ENTER.
(current-input-port
~= stdin, current-output-port
~= stdout and current-error-port
~= stderr)
I’m trying to read a sequence of numbers that are not parenthesized when they’re input, and I want them to be wrapped in parens so I can pass the instance variable of the read values to a function like filter. Is there a way to do this?
I think I’ve found a solution:
(define (read-list)
(let ([x (read)])
(if (eof-object? x) (list) (cons x (read-list)))))
That’s great you worked it out like that! The (for/list ([v (in-port read)]) v)
I mentioned above, will expand to code very similar to your definition of read-list
.
I think it’s great when learning Racket to work it out the way you did.
Design recipe :sunglasses:
Thanks for the help Sam and Greg!
I like port->list
is there a reason entering eof
into the input reader won’t stop the reader even if I have an eof-object?
predicate to catch it?
Maybe the buffering I mentioned above? Does it work if you type ENTER first?
It is throwing an error:
file-stream-buffer-mode: cannot set buffer mode on port
port: #<input-port:interactions from an unsaved editor>
Ah OK that won’t work in DrRacket with the port it sets up to be current-input-port
.
I tried ENTER both at the beginning of the stream and end, neither worked.
Ahhh okay
(It would work running your program with command-line racket
or Racket.exe
)
To the terminal! batman scene change music
FWIW I just tried now with DrRacket 6.10 on macOS and it worked for me. In the Interactions pane prompt I typed 1 2 3
then clicked the yellow EOF button.
(Using your definition of read-list
from above, and doing (read-list)
to run it.)
I think when it takes eof as an input in read-list it might be getting changed somehow to not be an eof object, but a symbol/datum
clicking the yellow EOF button worked for me as well though!
Oh, you meant you were typing “eof” in the input?
Yes, try that with just a regular call to (read)
, it should stop the reader
So, with will work is the DrR “EOF” yellow button, or, (on Unixy systems) typing CTRL+D. As to why typing “eof” did not work…
Try typing some other letters, say, “abc”, and see what read-list
returns for that.
Ahh it’s just quoting it?
Bingo.
read
is returning a plain data s-expression.
The “eof” is 'eof
, i.e. a symbol.
The symbol has no meaning.
When your Racket program is read, expanded, and evaluated, then it might attach some meaning to a symbol such as eof
or abc
.
“Oh, the environment has a binding for 'eof
. It is defined to be this object. So 'eof
is also an identifier.”, says Racket.
For fun, you could try changing your read-list
to check for (if (eq? x 'eof) (list) (cons x (read-list)))
and now it would work.
Well, it would work differently.
I’ll shut up now I think I’m over-explaining this. :slightly_smiling_face:
OK, one other thing I’ll mention. With quoted expressions like '(1 foo "bar")
, keep in mind that that’s really a list
with the quote distributed across all of the items. Try entering (list '1 'foo '"bar")
in the REPL. Racket will print '(1 foo "bar")
. Quoting can be confusing early on (at least it was for me).
@lexi.lambda wrote a great answer about this https://stackoverflow.com/questions/34984552/what-is-the-difference-between-quote-and-list
I work for c311 at IU and am definitely going to use that when explaining symbol lookup. :smile:
Thanks for your time!
@joslarki Um, “I work for c311 at IU” sounds like it means I was telling you a lot you already know. Sorry. :smile:
I didn’t mean to sound ungrateful! I couldn’t explain how I knew that it was being quoted in a way that is as understandable as the description you gave. This is the beginners channel as well, and I think it could be useful to more than just myself.
Oh, no, I didn’t mean to say you sounded ungrateful. Instead, I get embarrassed/cringey when I realize I’m motor-mouthing old news to someone.