方案:顺序执行
我需要做的事情基本上是这样的:
(define test
(λ (ls1 ls2)
(cond
((empty? ls2) null)
(else
(append ls1 (car ls2)) (test ls1 (cdr ls2))) (displayln ls1))))
问题是 else 子句及其后面的函数。我需要执行 else 子句的两个子句,然后需要执行最后一个函数,但我无法获得正确的语法。
我需要 (test '(1 2 3) '(4 5 6)) 来显示 '(1 2 3 4 5 6) 并且它必须使用递归调用。
任何建议表示赞赏。
谢谢。
I need to do something basically like this:
(define test
(λ (ls1 ls2)
(cond
((empty? ls2) null)
(else
(append ls1 (car ls2)) (test ls1 (cdr ls2))) (displayln ls1))))
The issue is the else-clause and the function that follows it. I need both clauses of the else-clause to execute and then I need the last function to execute but I can't get the syntax right.
I need (test '(1 2 3) '(4 5 6)) to result in displaying '(1 2 3 4 5 6) and it has to use the recursive call.
Any advice is appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那里有几个问题。首先,您在列表和原子(不是列表)上进行追加...至少如果
(car l2)
不是列表。其次,您可能认为(append l1 (list (car l2))
修改了l1
。但事实并非如此。结果是一个新列表。要对您的操作进行排序你可以像拉斯曼所说的那样,但你也可以编写以下内容,
这具有完全相同的行为。
There is several problem there. First you make an append on a list and an atom (not a list)... at least if
(car l2)
is not a list. Second you probably think that(append l1 (list (car l2))
modifiesl1
. But this is not the case. The result is a new list.To sequence your operation you can do as larsmans have said. But you can also write the following
This has exactly the same behavior.
如果您非常想递归地解决这个问题,并且不关心返回值,请使用
(顺便说一句,这是非常低效的:O(n × m)内存分配.)
If you desperately want to solve this recursively, and you don't care about the return value, use
(This is very inefficient btw: O(n × m) memory allocations.)