将字符串转换为Scheme中的代码
如何将字符串转换为PLT方案中相应的代码(不包含string->input-port
方法)? 例如,我想将此字符串:转换
"(1 (0) 1 (0) 0)"
为此列表:
'(1 (0) 1 (0) 0)
是否可以在不打开文件的情况下执行此操作?
How do I convert a string into the corresponding code in PLT Scheme (which does not contain the string->input-port
method)? For example, I want to convert this string:
"(1 (0) 1 (0) 0)"
into this list:
'(1 (0) 1 (0) 0)
Is it possible to do this without opening a file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Scheme 有过程
read
用于从输入端口读取 s-表达式,您可以使用string->input-port
将字符串转换为输入流。 因此,您可以从字符串中读取Scheme对象,但我没有安装Scheme,所以我只读取它来自参考,并没有实际测试它。
Scheme has procedure
read
for reading s-expressions from input port and you can convert a string to input stream withstring->input-port
. So, you can read a Scheme object from a string withI don't have Scheme installed, so I only read it from reference and didn't actually test it.
来自 PLT 方案手册:
(open-input-string string [name-v])
创建一个输入端口,用于从 string 的 UTF-8 编码(参见第 1.2.3 节)读取字节。 可选的 name-v 参数用作返回端口的名称; 默认为'string
。From PLT Scheme manual:
(open-input-string string [name-v])
creates an input port that reads bytes from the UTF-8 encoding (see section 1.2.3) of string. The optionalname-v
argument is used as the name for the returned port; the default is'string
.来自 comp.lang.scheme 上的类似问题您可以将字符串保存到文件中,然后从中读取。
这可能类似于这个示例代码:
From this similar question on comp.lang.scheme you can save the string to a file then read from it.
That might go something like this example code:
许多方案都有
with-input-from-string str thunk
,它在str
是标准输入端口的上下文中执行thunk
。 例如,在策略方案中:(with-input-from-string "(foo bar)"
(lambda () (read)))
计算结果为:
(foo bar)
lambda 是必需的,因为
thunk
应该是一个不带参数的过程。Many schemes have
with-input-from-string str thunk
that executesthunk
in a context wherestr
is the standard input port. For example in gambit scheme:(with-input-from-string "(foo bar)"
(lambda () (read)))
evaluates to:
(foo bar)
The lambda is necessary because a
thunk
should be a procedure taking no arguments.