joslarki
2018-6-18 15:27:08

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?


samth
2018-6-18 15:27:39

@joslarki I think you just want read


samth
2018-6-18 15:29:16

Is there a reason that read by itself doesn’t work?


joslarki
2018-6-18 15:34:24

No read does work, I wasn’t wrapping the numbers I input in parentheses. Thanks!


samth
2018-6-18 15:36:41

read works just on numbers too


greg
2018-6-18 15:47:42

@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).


greg
2018-6-18 15:48:10

You might first want to (file-stream-buffer-mode (current-input-port) 'none) — input might have line buffering by default.


greg
2018-6-18 15:48:28

In which case you won’t see any values until you hit ENTER.


greg
2018-6-18 15:49:28

(current-input-port ~= stdin, current-output-port ~= stdout and current-error-port ~= stderr)


joslarki
2018-6-18 16:57:34

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?


joslarki
2018-6-18 17:04:44

I think I’ve found a solution:

(define (read-list) (let ([x (read)]) (if (eof-object? x) (list) (cons x (read-list)))))


greg
2018-6-18 17:06:43

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.


greg
2018-6-18 17:07:48

I think it’s great when learning Racket to work it out the way you did.


joslarki
2018-6-18 17:08:23

Design recipe :sunglasses:


joslarki
2018-6-18 17:08:51

Thanks for the help Sam and Greg!


andreiformiga
2018-6-18 17:10:21

I like port->list


joslarki
2018-6-18 17:55:01

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?


greg
2018-6-18 17:57:59

Maybe the buffering I mentioned above? Does it work if you type ENTER first?


joslarki
2018-6-18 17:59:59

It is throwing an error:

file-stream-buffer-mode: cannot set buffer mode on port port: #<input-port:interactions from an unsaved editor>


greg
2018-6-18 18:00:52

Ah OK that won’t work in DrRacket with the port it sets up to be current-input-port.


joslarki
2018-6-18 18:01:07

I tried ENTER both at the beginning of the stream and end, neither worked.

Ahhh okay


greg
2018-6-18 18:01:10

(It would work running your program with command-line racket or Racket.exe)


joslarki
2018-6-18 18:01:44

To the terminal! batman scene change music


greg
2018-6-18 18:03:30

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.


greg
2018-6-18 18:04:21

(Using your definition of read-list from above, and doing (read-list) to run it.)


joslarki
2018-6-18 18:05:24

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!


greg
2018-6-18 18:06:03

Oh, you meant you were typing “eof” in the input?


joslarki
2018-6-18 18:06:21

Yes, try that with just a regular call to (read) , it should stop the reader


greg
2018-6-18 18:07:21

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…


greg
2018-6-18 18:07:41

Try typing some other letters, say, “abc”, and see what read-list returns for that.


joslarki
2018-6-18 18:08:12

Ahh it’s just quoting it?


greg
2018-6-18 18:08:36

Bingo.


greg
2018-6-18 18:08:49

read is returning a plain data s-expression.


greg
2018-6-18 18:09:07

The “eof” is 'eof, i.e. a symbol.


greg
2018-6-18 18:09:16

The symbol has no meaning.


greg
2018-6-18 18:09:49

When your Racket program is read, expanded, and evaluated, then it might attach some meaning to a symbol such as eof or abc.


greg
2018-6-18 18:10:34

“Oh, the environment has a binding for 'eof. It is defined to be this object. So 'eof is also an identifier.”, says Racket.


greg
2018-6-18 18:11:46

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.


greg
2018-6-18 18:11:57

Well, it would work differently.


greg
2018-6-18 18:12:07

I’ll shut up now I think I’m over-explaining this. :slightly_smiling_face:


greg
2018-6-18 18:17:50

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).


greg
2018-6-18 18:21:35

joslarki
2018-6-18 19:19:53

I work for c311 at IU and am definitely going to use that when explaining symbol lookup. :smile:


joslarki
2018-6-18 19:22:36

Thanks for your time!


greg
2018-6-18 20:54:12

@joslarki Um, “I work for c311 at IU” sounds like it means I was telling you a lot you already know. Sorry. :smile:


joslarki
2018-6-18 21:04:59

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.


greg
2018-6-19 03:20:12

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.