2014-10-03 62 views
1

我试图写一个简单的递归程序,打印一定范围内的数字的平方:递归函数出错

(defun show-squares (i end) 
    (if (> i end) 
     'done 
     (format t "~A ~A~%" i (* i i)) 
     (show-squares (+ i 1) end))) 

我得到的错误:*** - SYSTEM::READ-EVAL-PRINT: variable SHOW-SQUARES has no value 错在这里?

+3

'if'只需要三个参数:测试时,则 - 部分和其他部分。你已经给了它四个。 – 2014-10-03 16:45:07

+1

另请参阅[您是否可以使用If语句执行多个语句?](http://stackoverflow.com/q/2852249/1281433)和[表单中的连续调用/评估?](http://stackoverflow.com/q/2878240/1281433) – 2014-10-03 16:47:14

+0

由错误判断:函数的值与其他值相同,并且看起来您的lisp不知道SHOW-SQUARES是什么;我想你可能在调用函数之前忘记了解释/编译代码。 – miercoledi 2014-10-03 19:02:33

回答

3

我怀疑问题的一部分是严重形成if语句。

“如果” 需要2个或3个参数,而你传递4.

3

你可能错过了一些progn

(defun show-squares (i end) 
    (if (> i end) 
     'done 
     (progn 
      (format t "~A ~A~%" i (* i i)) 
      (show-squares (+ i 1) end)))) 
+1

是的,我试图在没有'progn'的情况下重写程序。这实际上是Paul Graham的Ansi Common Lisp的一个例子。问题出在我构造括号的方式上。 '如果'收到4个参数。它现在工作正常:'(format t“〜A \t〜A〜%”i(* i i)(show-squares(+ i 1)end))))' – Omid 2014-10-03 23:07:21