在Scheme中编写递归枚举函数

ami*_*dfv 2 lisp algorithm recursion scheme enumeration

我正在写一个递归枚举函数,我在某个地方遇到了一个简单的错误.

这是应该发生的事情:

(enum 1 0.5 2.5)  
> (1.0 1.5 2.0 2.5)
Run Code Online (Sandbox Code Playgroud)

这是代码:

(define enum
   (lambda (start step stop)
      (if (not (<= stop start))
          (cons start (enum (+ start step) step  stop))
          ('(stop))
          )))
Run Code Online (Sandbox Code Playgroud)

编辑:
我得到的错误(来自Impromptu(http://impromptu.moso.com.au/))是:

> (print (enum 0 0.5 2.5))  
:ERROR: position:(0) in function "enum"  
illegal function  
Trace: enum  
Run Code Online (Sandbox Code Playgroud)

tem*_*def 5

我相信你的问题就在于此

('(stop))
Run Code Online (Sandbox Code Playgroud)

我认为你有正确的想法,你想要在结束后停止执行递归,但这不是这样做的方法.因为你把它放在双括号中,这被解释为"评估'停止',然后尝试将其作为一个函数调用." 但是,stop不是函数,因此是错误.

要解决此问题,如果要将返回值设为包含just的列表stop,请使用以下list函数:

(define enum
   (lambda (start step stop)
      (if (not (<= stop start))
          (cons start (enum (+ start step) step  stop))
          (list stop)
          )))
Run Code Online (Sandbox Code Playgroud)

请注意,这里只有一组括号list stop.

希望这可以帮助!