【问题标题】:Real standard input in RacketRacket 中的真实标准输入
【发布时间】:2016-04-05 14:00:42
【问题描述】:

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,只返回空字符串。

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

失败:只读取了 5 个字节的 STDIN,显然是 evals 的其余部分...?

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


asdasd

失败:在准确读取 500000 个字节之前不会返回,并且不会在 EOL 时返回。 有点像 Python 的input(),它在找到EOL 时返回一个字符串,或者Factorreadln,它的作用相同,我如何从current-input-port 读取原始数据?

【问题讨论】:

    标签: input racket


    【解决方案1】:

    这只是 Racket 的 REPL 输入处理的限制。如果你编写一个独立的程序,它就可以正常工作。

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

    一个已知的限制。由普通“球拍”实现的 REPL 不 将 REPL 表达式的输入流与程序的 输入流。

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

    【讨论】:

    • 很高兴知道,谢谢,虽然这有点烦人。是否有解决方法,还是会修复?
    • (define (my-read-line) (let ([contents (read-line)]) (if (string=? contents "") (read-line) contents))) 是我想我现在要使用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    相关资源
    最近更新 更多