我正在尝试创建一个读取2个整数的lisp代码,并输出两者之间的所有数字。我目前的代码是
(defun range (x y)
(if (< x y)
x
(1+ (range(x y))))
代码编译并运行但只输出“1”。
答案 0 :(得分:2)
不确定你想要的是什么,但我能想出的最接近的是:
(defun range (x y)
(when (< x y)
(print x)
(range (1+ x) y)))
测试
CL-USER> (range 3 7)
3
4
5
6
NIL
注意
when
(或cond
或progn
...)1+
用于递增参数,而不是完整的表达式;将其视为传统语言中的循环变量另外,请标记您的问题common-lisp
以获得更好的曝光度。
编辑
证明原始代码确实在某些CLISP实例上运行:
Welcome to GNU CLISP 2.49 (2010-07-07) <http://clisp.cons.org/>
Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2010
Type :h and hit Enter for context help.
[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]>