方案:映射 let 和 set!到列表中
我正在尝试映射 let 和 set! on 列出了类似这样的内容:
(map (lambda (x) (let ((x #f)))) <list>)
and
(map set! <list1> <list2>)
但是,当然,两者都不起作用。
有办法做到这一点吗?任何建议表示赞赏。
谢谢。
真正的问题是我正在尝试找到一种模式匹配 letrec 的方法。我需要能够模式匹配:
(letrec ((var val) ...) expr0 expr1 ...)
并使用 match-lambda 将其转换为仅使用 let 和 set 的等效调用!这是我试图模仿的模板:
(letrec ((var val) ...) expr0 expr1 ...)
==>
(let ((var #f) ...)
(let ((temp val) ...)
(set! var temp) ...
(let () expr0 expr1 ...)))
问题是将其转换为 match-lambda 接受的语法。这是我正在做的事情,假设有一种方法可以完成最初的问题:
(match-rewriter (`(letrec((,<var> ,<val>) ...) ,<expr> ...)
`((map (λ (x) (let ((x #f)))) ,<var>)
(let ((temp ,<val>)))
(map set! ,<var> temp)
(let () ,@<expr>))))
任何建议都会受到赞赏。
谢谢。
I am trying to map let and set! onto lists something like this:
(map (lambda (x) (let ((x #f)))) <list>)
and
(map set! <list1> <list2>)
But, of course, neither is working.
Is there a way to do this? Any advice is appreciated.
Thanks.
The real problem is that I am trying to find a way to pattern match letrec. I need to be able to pattern match:
(letrec ((var val) ...) expr0 expr1 ...)
and convert it -- using match-lambda -- to an equivalent call using only let and set! This is the template I am trying to emulate:
(letrec ((var val) ...) expr0 expr1 ...)
==>
(let ((var #f) ...)
(let ((temp val) ...)
(set! var temp) ...
(let () expr0 expr1 ...)))
The problem is translating this into syntax that match-lambda accepts. Here is what I was working on, assuming there was a way to accomplish the original question:
(match-rewriter (`(letrec((,<var> ,<val>) ...) ,<expr> ...)
`((map (λ (x) (let ((x #f)))) ,<var>)
(let ((temp ,<val>)))
(map set! ,<var> temp)
(let () ,@<expr>))))
Any advice is appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能那样做。如果不使用
eval
,变量名通常不允许是动态的(基本上任何不是符号文字的东西)。这是设计使然。如果您的变量名确实是文字,并且您只是想要一种同时绑定多个变量的方法,则可以使用
let-values
(SRFI 11) 或扩展let
(SRFI 71)。编辑以匹配OP的编辑:你想要做的听起来像
letrec
定义在这里给出。但是,该宏使用syntax-case
,而不是match-lambda
等。不过,您也许可以使用它作为起点。You cannot do that. Short of using
eval
, variable names are generally not allowed to be dynamic (basically anything that isn't a symbol literal). This is by design.If your variable names really are literals, and you just want a way to bind multiple variables at once, you can use
let-values
(SRFI 11) or extendedlet
(SRFI 71).Edit to match OP's edit: What you want to do sounds like the
letrec
definition given here. However, that macro usessyntax-case
, notmatch-lambda
or the like. You may be able to use it as a starting point, though.这是您想要的代码:
这里有几个区别。一是
let
是嵌套的。其次,我们正在构建代码,而不是尝试运行它。This is the code you want:
There are several differences here. One is that the
let
s are nested. Second, we're constructing code, not trying to run it.