将Scheme表达式转换为字符串
给定一个表达式 '(lambda (x) x) 我如何将其转换为字符串。我认为符号->字符串可以完成这项工作,但不,它不能不是符号。
例如对于宏到字符串: (to-string (lambda(x) x)) 这应该返回>> “(lambda (x) x)”
任何想法,谢谢
Given an expression '(lambda (x) x) How can I translate this into a string. I thought symbol->string will do the job but no it cant not a symbol.
e.g for a macro to-string:
(to-string (lambda(x) x)) this should return >> "(lambda (x) x)"
Any ideas folks Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
标准方案(至少在 R5RS 意义上)无法做到这一点,因此如果您想要可移植代码,您需要自己遍历结构。乏味,但不太复杂(即使对于点列表)。
但如果您只想要一些工作版本,那么您应该查看您的实现手册并搜索执行此操作的方法。答案几乎总是很简单,例如,在 PLT 方案中,您可以使用
(format "~s" '(lambda ...))
Standard Scheme (at least in the R5RS sense) has no way of doing this, so if you want portable code, you need to walk the structure yourself. Tedious, but not too complicated (even for dotted lists).
But if you just want some working version, then you should look in your implementation's manual and search for the way to do this. The answer will almost always be simple, for example, in PLT Scheme you'd use
(format "~s" '(lambda ...))
表达式
'(lambda (x) x)
是一个带引号的列表。表达式
(lambda (x) x)
是运行时的某种已编译的、不透明的、可执行的内部对象。symbol->string
只是将符号转换为字符串,即字符序列。如果您正在使用列表,则只需遍历该列表并打印出各个组件即可。事实上
(write '(lambda (x) x))
将简单地打印出列表。许多方案都有类似于
(with-output-to-string ... )
的东西,它返回写入标准端口的所有输出的字符串。然而,如果你这样做
(write (lambda (x) x))
,你会得到谁知道什么。转储可执行函数类型时,您将获得实现提供的任何内容。有些可能会打印显示源代码的“反汇编”。其他人可能只是打印#function
或同样无用的东西。简而言之,如果您只想打印出一个列表,有各种机制可以实现。
如果您想打印出已编译函数的源代码,那是一个完全不同的问题,非常依赖于实现,而且很可能是不可能的。
The expression
'(lambda (x) x)
is a quoted list.The expression
(lambda (x) x)
is some kind of compiled, opaque, executable, internal object of the runtime.symbol->string
simply converts a symbol to a string, which is a sequence of characters.If you're working with a list, you can simply walk the list and print the individual components out. In fact
(write '(lambda (x) x))
will simply print the list out.Many schemes have something akin to
(with-output-to-string ... )
that returns a string of all the output written to the standard port.However, if you do
(write (lambda (x) x))
, you'll get who knows what. You'll get whatever the implementation provides when dumping an executable function type. Some may print a "disassembly" showing the source code. Others may simply print#function
or something equally useless.In short, if you just want to print out a list, there are all sorts of mechanisms for that.
If you want to print out the source code of a compiled function, that's a completely different problem, very implementation dependent, and may well be impossible.
如果您使用的是 R6RS 兼容方案,则可以使用
write
函数结合使用字符串输出端口调用
。一些例子:
顺便说一句,通常也可以使用
read
结合开放字符串输入端口
。一些例子:
If you are using an R6RS-compatible scheme, you can use the
write
function combined withcall-with-string-output-port
.Some examples:
By the way, it's usually possible to go in the other direction as well, using
read
combined withopen-string-input-port
.Some examples:
使用
pretty-format
:Use
pretty-format
:你应该走过这些缺点。当一个新的cons开始时写“(”,当它结束时写“)”并使用symbol->string作为conses内的符号。
您可以通过类型分派来扩展它。方案中可能也存在漂亮的印刷品吗?
You should walk through the conses. When a new cons starts write "(", when it ends write ")" and use symbol->string for symbols inside the conses.
You can extend this with type dispatching. May be pretty print exists in scheme too?
我认为这是一个简单的代码,可以满足您的要求。
它不使用特定于实现的东西并且非常简单。
但请注意,它不能正确处理字符串中的特殊字符(例如“a\”b\“c”将变成“a”b“c”)。
This is a simple code that, I think, does what you want.
It uses no implementation specific stuff and is quite straight forward.
Note, however, that it doesn't correctly handle special characters in strings (e.g. "a\"b\"c" will turn to "a"b"c").