tags:

views:

80

answers:

3

I know you can use (read) to get a user-inputted expression, but (read) will only get the first expression, evaluating anything afterwards. I was wondering if there was any way I could read an entire line of user input, perhaps turning said line into a list?

(let ((input (read-user-line)))
   ;; user could type "cons 2 3" without quotes
   ;; input could contain '(cons 2 3)
   (apply (car input) (cdr input)))

Thanks!

+1  A: 

Some Scheme's have a read-line function that reads a line and returns it as a string.

(But to get from that to something that you can apply is a different story.)

Eli Barzilay
+2  A: 

If your Scheme is an R6RS implementation, you can use GET-LINE. If that same Scheme implements SRFI-13 as well, you can use STRING-TOKENIZE to turn it into a list.

One Scheme that qualifies is Ypsilon:

(import (srfi srfi-13))

(let ((input (get-line (current-input-port))))
  (for-each (lambda (x) (display x) (newline))
            (string-tokenize input)))
$ ypsilon try.scm
the quick brown fox jumps over the lazy dog.
the
quick
brown
fox
jumps
over
the
lazy
dog.

Otherwise you are on your own with whatever non-standard extensions your implementation provides.

Cirno de Bergerac
A: 

You can use read-line to read it as a string.

THen you just split it by whitespace into a list of strings

Then you use read on those using with-input-from-string

And then you use apply.

You can't just apply strings, you can only apply scheme data, strings are scheme data yeah, but not procedures.

Lajla