在有一些限制的情况下反转Scheme中的列表
我正在尝试反转Scheme中的列表,但我只能使用define、car、cdr、list?,<强>空?,<强>如果,<强>条件,<强>缺点,<强>显示,< strong>开始,以及任何运算符。我不能使用追加、循环(必须是纯递归)、lambda 和任何其他我没有提到的函数。我也不允许使用辅助函数。一切都应该在一个功能中。
现在已经过去几个小时了,我已经尝试了多种解决方案,但仍然无法解决这个问题。这是我得到的最接近的解决方案:
(define (my-reverse2 lis)
(if (null? (cdr lis)) lis
(cons (my-reverse2 (cdr lis)) (cons (car lis) '()))))
这是输出:
> (my-reverse2 '(3 2 1))
Output: (list (list (list 1) 2) 3)
输出应该类似于 (1 2 3)
。我应该如何进行?
I'm trying to reverse a list in Scheme, but I can only use define, car, cdr, list?, null?, if, cond, cons, display, begin, and any operators. I cannot use append, loops (must be pure recursion), lambda and any other functions that I have not mentioned. I'm also not allowed to use helper functions. Everything should be in one function.
It's been hours now and I've tried multiple solutions and I still cannot solve this. This is the closest solution that I got:
(define (my-reverse2 lis)
(if (null? (cdr lis)) lis
(cons (my-reverse2 (cdr lis)) (cons (car lis) '()))))
And this is the output:
> (my-reverse2 '(3 2 1))
Output: (list (list (list 1) 2) 3)
The output should be something like (1 2 3)
. How should I proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是使用累加器值:
您将其称为
(my-reverse '(1 2 3) '())
。如果Scheme中有可选参数,您可以将acc设为可选,默认值为空列表。在我的代码中,我通常会对此进行包装,仅使用单个参数,然后使用空列表调用两个参数版本。The easiest way would be to use an accumulator value:
You call this
(my-reverse '(1 2 3) '())
. If there were optional arguments in Scheme you could make theacc
optional with a default value of the empty list. In my code I would usually have a wrapper around this with a single argument only which then calls the two argument version with the empty list.首先,反转
cdr
并获取其car
——这将是......最后元素,对吗? -- 其cdr
将是相反的core,即没有最后一个和第一个元素的原始列表。然后,将原始
car
cons
到反转的核心上,反转,从而按原始顺序获取列表中除最后一个元素之外的所有元素。那么现在我们可以反转那个,并将最后一个元素cons
到结果上。很简单,对吧?是的,没那么多。所以这里有一个例子:
所有这些需要的是
cdr
,car
,cons
,null?
,cond
和递归。let
/define
会有很大帮助,但并不是绝对必要的。不要忘记处理极端情况。另请参阅:这个我的相关答案。
First, reverse the
cdr
and take itscar
-- which will be ... the last element, right? -- and itscdr
will be the reversed core, i.e. the original list without its last and first element.Then,
cons
the originalcar
onto the reversed core, reversed, thus getting all the elements of the list without the last one, in original order. So then now we can reverse that, andcons
the last element onto the result.Simple, right? Yeah, not so much. So here's an illustration:
All this needs is
cdr
,car
,cons
,null?
,cond
, and recursion.let
/define
will help a lot but is not strictly necessary. Do not forget to take care of the corner cases.See also: this related answer of mine.
可能是有史以来效率最低的
reverse
,但它确实符合您问题中的标准。在本练习结束时,您应该能够相当轻松地纯粹从car
和cdr
角度思考 -好吧,dangit,这基本上就是 @WillNess 写的 :D
If
let
被允许,你可以删除两个(reverse (cdr t))
实例 -如果允许
or
,我们可以折叠两个if -
Probably the most inefficient
reverse
ever written but it does meet the criteria in your question. By the end of this exercise you should be fairly comfortable thinking purely in terms ofcar
andcdr
-Well dangit, this is basically what @WillNess wrote :D
If
let
is allowed, you can remove two instances of(reverse (cdr t))
-If
or
is allowed, we can collapse the twoif
-您可以使用可选参数吗?
测试:
Are you allowed to use optional arguments?
Test: