【问题标题】:LISP - cannot call function with optional parameterLISP - 无法使用可选参数调用函数
【发布时间】:2012-06-05 20:23:12
【问题描述】:

我在 LISP 中有这个函数,带有常规参数和可选参数 n:

(defun lastplus (x &optional (n 0)) //default value for n is 0
    ( if (listp x) //if x is a list
        (
            (list (length x) (n)) //return list that contains length(x) and n
        )
        (n) //else return n
    )
)

我正在尝试使用侦听器文件中的函数,但它给了我这个错误:

CL-USER 13 : 4 > (lastplus 2 8) 

Error: Undefined function N called with arguments ().

我使用 LispWorks 6.0.1

你知道我为什么会收到这个错误吗?

【问题讨论】:

  • // 是一个有效的令牌。特别是,它没有引入评论。评论用;引入或包含在#||#中。
  • 你是对的。我只在我的问题中添加了这些“//”..
  • 顺便说一句,'CL-USER 13 : 4 >' 表示您处于调试级别。您可能想回到顶层。使用命令 :top .

标签: function lisp optional-parameters


【解决方案1】:
(defun lastplus (x &optional (n 0)) //default value for n is 0
    ( if (listp x) //if x is a list
        (
            (list (length x) (n)) //return list that contains length(x) and n
        )
        (n) //else return n
    )
)

你的格式风格不是 Lispy。

适应 Lisp 格式:

(defun lastplus (x &optional (n 0)) ; default value for n is 0
   (if (listp x) ; if x is a list
        ((list (length x) (n))) ; return list that contains length(x) and n
     (n)))

你说:cannot call function with optional parameter

当然可以。错误消息确实说了别的。您可以使用可选参数调用函数。错误在函数内部。

错误提示:Error: Undefined function N called with arguments ().

所以你正在调用一个名为N 的函数,但它并不存在。没有争论。比如(n)。检查你的代码 - 你能找到(n)吗?

现在问问自己:

  • 函数调用是什么样的?

  • 答案:左括号,函数,可能还有一些参数,右括号

  • (n) 长什么样子?

  • 答案:看起来像函数调用。

  • 这就是你想要的吗?

  • 当然不会。

  • 你想要什么?

  • 变量值。

  • 那是什么样的?

  • 只需n

  • 还有其他错误吗?

  • 嗯。

  • 第三行的表格呢?

  • 这看起来也不对。

  • 这也是错误的。同样的错误。.

【讨论】:

  • 令人印象深刻..你可以成为一名诗歌作家..现在我看到了我的错误..我只需要写 n...谢谢老兄
  • 不幸的是,在我将“(n)”的每次出现都更改为n之后,它仍然给出同样的错误。现在有什么问题?谢谢
  • @זאבי כהן:然后将此新版本的代码添加到您的问题中,我们将再次查看。
  • @זאבי כהן:我在更改的代码中没有发现问题。错误是如何发生的?
猜你喜欢
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
相关资源
最近更新 更多