方案函数中的右括号放置不当
我有以下方案函数:
(define get-ivars
(λ (ivars num)
(cond ((null? ivars) '())
(else
(append (list (car ivars) `(nth args ,num)) (list (get-ivars (cdr ivars) (+ num 1))))))))
在特定实例中返回以下内容:
(x (nth args 1) (y (nth args 2) ()))
问题是,我需要它返回:
((x (nth args1)) (y (nth args 2)) ())
- 末尾的两个右括号应该在 (nth 语句之后。
我如何将其变为 工作正常吗?
get-ivars 调用者
(define gen-classes
(λ (classes)
(cond ((null? classes) '())
(else
(let* ((class (car classes)))
(eval
`(define ,(cadr class)
(λ (args)
(let (
,(get-ivars (cdr (cadddr class)) 1)
)
(eval
(let* ,(cdar (cddddr class))
(λ (method . args)
,(get-methods (cdadr (cddddr class)))
))))))))))))
I have the following scheme function:
(define get-ivars
(λ (ivars num)
(cond ((null? ivars) '())
(else
(append (list (car ivars) `(nth args ,num)) (list (get-ivars (cdr ivars) (+ num 1))))))))
That returns the following in a specific instance:
(x (nth args 1) (y (nth args 2) ()))
The problem is, I need it to return:
((x (nth args1)) (y (nth args 2)) ())
-the two closing parenthesis at the end should be after the (nth statements.
How would I go about getting this to work properly?
get-ivars caller:
(define gen-classes
(λ (classes)
(cond ((null? classes) '())
(else
(let* ((class (car classes)))
(eval
`(define ,(cadr class)
(λ (args)
(let (
,(get-ivars (cdr (cadddr class)) 1)
)
(eval
(let* ,(cdar (cddddr class))
(λ (method . args)
,(get-methods (cdadr (cddddr class)))
))))))))))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
else
子句中的第二个(list ...)
才是让你陷入困境的原因。它使每个连续的调用嵌套得越来越深。递归自然会创建列表;你不需要再把它包裹起来。尝试:
关于
get-ivars
调用者代码,对get-ivars
的未引用调用周围的括号是给您在评论中提到的麻烦的原因。有了它们,这段代码:给你这个:
正如你所看到的,它为你在 let 中的赋值周围提供了一组额外的括号。
所以你想这样做:
get-ivars
返回一个列表列表,这正是你想要的let
中的分配,所以你不需要包裹或(就像我之前那样)拼接它。只需单独使用取消引号,结果是:哪个应该可以解决问题。
顺便说一句,我发现当我在玩这个时,放弃
eval
很有帮助;然后可以目视检查结果以确保其语法正确。That second
(list ...)
in yourelse
clause is what's screwing you up. It's nesting each successive call deeper and deeper. The recursion will naturally create the list; you don't need to wrap it again.Try:
Regarding the
get-ivars
caller code, the parentheses surrounding the unquoted call toget-ivars
are what's giving you the trouble you mention in the comments. With them, this code:Gives you this:
Which, as you can see, gives you an extra set of parentheses around the assignments in the let.
So you want to do this:
get-ivars
is returning a list of lists, which is exactly what you want for the assignments in thelet
, so you don't need to wrap or (as I had it earlier) splice it. Just use the unquote on its own, and the result is:Which should do the trick.
Incidentally, I found it helpful to leave off the
eval
when I was playing around with this; one can then visually inspect the result to make sure its syntax is okay.我没有尝试过,但我认为这会起作用:
I haven't tried this, but I think this would work: