开发者

Getting a line of user input in Scheme?

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!


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜