将字符串转换为Scheme中的代码

发布于 2024-07-09 03:54:05 字数 215 浏览 6 评论 0原文

如何将字符串转换为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长伴 2024-07-16 03:54:05

Scheme 有过程 read 用于从输入端口读取 s-表达式,您可以使用 string->input-port 将字符串转换为输入流。 因此,您可以从字符串中读取Scheme对象,但

(read (string->input-port "(1 (0) 1 (0) 0)"))

我没有安装Scheme,所以我只读取它来自参考,并没有实际测试它。

Scheme has procedure read for reading s-expressions from input port and you can convert a string to input stream with string->input-port. So, you can read a Scheme object from a string with

(read (string->input-port "(1 (0) 1 (0) 0)"))

I don't have Scheme installed, so I only read it from reference and didn't actually test it.

妥活 2024-07-16 03:54:05

来自 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 optional name-v argument is used as the name for the returned port; the default is 'string.

神也荒唐 2024-07-16 03:54:05

来自 comp.lang.scheme 上的类似问题您可以将字符串保存到文件中,然后从中读取。

这可能类似于这个示例代码:

(let ((my-port (open-output-file "Foo")))
  (display "(1 (0) 1 (0) 0)" my-port)
  (close-output-port my-port))

(let* ((my-port (open-input-file "Foo"))
       (answer (read my-port)))
  (close-input-port my-port)
  answer)

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:

(let ((my-port (open-output-file "Foo")))
  (display "(1 (0) 1 (0) 0)" my-port)
  (close-output-port my-port))

(let* ((my-port (open-input-file "Foo"))
       (answer (read my-port)))
  (close-input-port my-port)
  answer)
清旖 2024-07-16 03:54:05

许多方案都有 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 executes thunk in a context where str 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文