newLISP 无效函数

newLISP Invalid Function

我有一个家庭作业,我们需要用 newLISP 编写一些函数。我 运行 遇到了一个问题,所以我举了一个问题的例子,看看是否有人可以帮助我。

问题是递归函数结束后,returns出现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知之甚少,所以这可能是一个简单的问题。

谢谢!

在 Lisp 中,您不会使用任意括号将事物组合在一起。所以你应该这样做:

(define (decrement num)
   (if (> num 0)
      (begin
        (println num)
        (decrement (- num 1))
      )
      (println "done")
   ) 
 )