强制评估模式匹配中的递归调用
我正在使用 match-lambda 来根据更基本的函数重写某些函数。这是一个示例,它采用表示 let* 调用的输入代码的字符串,并将它们作为转换为嵌套一元 let 的字符串返回:
(define let*→nested-unary-lets
(match-lambda
(`(let* (()) ,<exprs>)
`(let () ,<exprs>))
(`(let* ((,<var> ,<val>)) ,<exprs>)
`(let ((,<var> ,<val>)) (let () ,<exprs>)))
(`(let* ((,<var> ,<val>) . ,<clauses>) ,<exprs>)
`(let ((,<var> ,<val>)) (let*→nested-unary-lets '(let* (,@<clauses>) ,<exprs>))))))
这是调用 let*→nested-unary-lets 的示例:
(let*→nested-unary-lets '(let* ((a 1) (b (+ a 1)) (c (+ a b))) (displayln c)))
'(let ((a 1))
(let*→nested-unary-lets
'(let* ((b (+ a 1)) (c (+ a b)))
(displayln c))))
我想知道是否有任何方法强制对 let*→nested-unary-let 的递归调用求值,以便输出字符串仅包含嵌套的 let 并且不需要进一步求值。
谢谢。
I am using match-lambda to rewrite certain functions in terms of more basic ones. Here is an example that takes strings representing input code for let* calls and returns them as strings converted to nested unary lets:
(define let*→nested-unary-lets
(match-lambda
(`(let* (()) ,<exprs>)
`(let () ,<exprs>))
(`(let* ((,<var> ,<val>)) ,<exprs>)
`(let ((,<var> ,<val>)) (let () ,<exprs>)))
(`(let* ((,<var> ,<val>) . ,<clauses>) ,<exprs>)
`(let ((,<var> ,<val>)) (let*→nested-unary-lets '(let* (,@<clauses>) ,<exprs>))))))
Here is an example of a call to let*→nested-unary-lets:
(let*→nested-unary-lets '(let* ((a 1) (b (+ a 1)) (c (+ a b))) (displayln c)))
'(let ((a 1))
(let*→nested-unary-lets
'(let* ((b (+ a 1)) (c (+ a b)))
(displayln c))))
I was wondering if there is any way to force the evaluation of the recursive call to let*→nested-unary-lets so that the output string contains only nested lets and requries no further evaluation.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在递归情况下使用准引用来输出答案,因此只需在递归调用
let*->nested-unary-lets
,) > (与和
之前的一样),因此会立即对其进行评估。准引号中的,
可以拼接任何表达式的结果,而不仅仅是变量。该行:还有一些其他问题:为了使
,@
工作,内部let*
之前的'
需要被<代码>`。这是我建议的版本:这需要将
的匹配更改为。 ,
允许多个表达式。You are already using quasiquoting to output your answer in the recursive case, so just add a comma (
,
) before the recursive call tolet*->nested-unary-lets
(like the ones before<var>
and<val>
) so it is evaluated immediately. The,
in quasiquotes can splice in the result of any expression, not just variables. The line:has some other problems as well: in order for the
,@
to work, the'
before the innerlet*
needs to be`
. Here is the version I suggest:That requires that the match for
<exprs>
be changed to. ,<exprs>
to allow more than one.