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
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,
stopisn’t a function, hence the error.To fix this, if you want to make the return value a list containing just
stop, use thelistfunction:Note that there is only one set of parentheses around
list stophere.Hope this helps!