我有一个家庭作业,我们需要用newLISP写一些函数。我遇到了一个问题,所以我举了一个问题的例子,看看是否有人可以帮助我。
问题是在递归函数结束后,它会返回一个ERR: invalid function :错误。无论我调用的function是什么,都会发生这种情况。
举个例子,我做了一个递归函数,递减一个数字,直到我们达到0。代码如下:
(define (decrement num)
(if (> num 0)
(
(println num)
(decrement (- num 1))
)
(
(println "done")
)
)
)每当我运行这个函数时,从数字10开始,输出如下所示:
> (decrement 10)
10
9
8
7
6
5
4
3
2
1
done
ERR: invalid function : ((println "done"))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement (- num 1))
called from user function (decrement 10)我不明白为什么这会返回一个无效的函数错误。我对newLISP知之甚少,所以这可能是一个简单的问题。
谢谢!
发布于 2020-09-28 15:20:13
在Lisp中,您不能使用任意的圆括号将事物组合在一起。所以你应该这样做:
(define (decrement num)
(if (> num 0)
(begin
(println num)
(decrement (- num 1))
)
(println "done")
)
)https://stackoverflow.com/questions/64095835
复制相似问题