Racket 中的真实标准输入

Real standard input in Racket

Racket 似乎无法从 STDIN 读取字符串。

$ racket
Welcome to Racket v6.4.
-> (define (s) (read-line))         

好的,s 是调用 read-line 的别名。

-> (printf "You entered: ~a\n" s) 
You entered: 

失败:打印了字符串,但 Racket 没有等待按键/STDIN/EOF/EOL。

-> (define n (read))
a
-> n
'a

失败:调用 read 并等待 EOF / EOL,然后分配给 n,但 n 被分配 符号 'a 不是 字符串文字 a.

-> (read-line)
""

失败:调用 read-line 不等待 STDIN,只是 return 空字符串。

-> (read-string 5)
asdasdasdasd
"\nasda"
; sdasdasd: undefined;
;  cannot reference undefined identifier
; [,bt for context]

失败:只读取了 STDIN 的 5 个字节,显然 eval 剩下的部分......?

-> (read-string 500000)
asdasd
asdasdaas
a
asdasd
asdasd


asdasd

失败:在恰好读取 500000 个字节之前不会 return,并且在 EOL 时不会 return。 有点像 Python 的 input(),当找到 EOL 时,return 是一个字符串,或者 Factorreadln 也是如此,如何我可以从 current-input-port 中读取原始数据吗?

这只是 Racket 的 REPL 输入处理的限制。如果你写一个独立的程序,它会工作得很好。

这是解释问题的邮件列表中的引述:

A known limitation. The REPL implemented by plain `racket' does not separate the input stream for REPL expressions from the program's input stream.

更多详情:https://groups.google.com/d/msg/racket-users/0TTsA9-3HDs/9_mMWsgKFOMJ