I am trying to create a lisp code that reads 2 integers and outputs all numbers between the two. my current code is
(defun range (x y)
(if (< x y)
x
(1+ (range(x y))))
the code compiles and runs but only outputs "1".
I am trying to create a lisp code that reads 2 integers and outputs all numbers between the two. my current code is
(defun range (x y)
(if (< x y)
x
(1+ (range(x y))))
the code compiles and runs but only outputs "1".
Not sure what exactly you want but the closest I could come up with is:
(defun range (x y)
(when (< x y)
(print x)
(range (1+ x) y)))
Testing
CL-USER> (range 3 7)
3
4
5
6
NIL
Pay attention to
when
(or cond
or progn
...) if you want to do more than one action after a condition1+
is used to increment a parameter, not the complete expression; think of it as a loop variable in a traditional languageAlso, tag your question common-lisp
for better visibility.
EDIT
proof that the original code does run on some instances of CLISP:
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
[1]> (range 1 5)
*** - EVAL: undefined function RANGE
The following restarts are available:
USE-VALUE :R1 Input a value to be used instead of (FDEFINITION 'RANGE).
RETRY :R2 Retry
STORE-VALUE :R3 Input a new value for (FDEFINITION 'RANGE).
ABORT :R4 Abort main loop
Break 1 [2]>
[3]> (defun range (x y)
(if (< x y)
x
(1+ (range(x y)))))
RANGE
[4]> (range 1 5)
1
[5]>