在Scheme中编写递归枚举函数
我正在编写一个递归枚举函数,但在某个地方遇到了一个简单的错误。
这是应该发生的事情:
(enum 1 0.5 2.5)
> (1.0 1.5 2.0 2.5)
这是代码:
(define enum
(lambda (start step stop)
(if (not (<= stop start))
(cons start (enum (+ start step) step stop))
('(stop))
)))
编辑:
我得到的错误(来自即兴(http://impromptu.moso.com.au/))是:
> (print (enum 0 0.5 2.5))
:ERROR: position:(0) in function "enum"
illegal function
Trace: enum
I'm writing a recursive enumeration function, and I'm having a simple error somewhere.
Here's what should happen:
(enum 1 0.5 2.5)
> (1.0 1.5 2.0 2.5)
Here's the code:
(define enum
(lambda (start step stop)
(if (not (<= stop start))
(cons start (enum (+ start step) step stop))
('(stop))
)))
Edit:
The error I'm getting (from Impromptu (http://impromptu.moso.com.au/)) is:
> (print (enum 0 0.5 2.5))
:ERROR: position:(0) in function "enum"
illegal function
Trace: enum
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信你的问题就在
我认为你有正确的想法,你想在到达终点后停止执行递归,但这不是这样做的方法。因为您已将其放在双括号中,所以这被解释为“评估‘stop’,然后尝试将其作为函数调用。”但是,
stop
不是函数,因此会出现错误。要解决此问题,如果您想让返回值仅包含
stop
的列表,请使用list
函数:请注意,
周围只有一组括号在此列出停止
。希望这有帮助!
I believe that your problem is in the line
I think you have the right idea that you want to stop executing the recursion once you've hit the end, but this isn't the way to do it. Because you have put this in double parentheses, this is interpreted as "evaluate 'stop', then try calling it as a function." However,
stop
isn't a function, hence the error.To fix this, if you want to make the return value a list containing just
stop
, use thelist
function:Note that there is only one set of parentheses around
list stop
here.Hope this helps!