gpt4 book ai didi

function - LISP - 无法使用可选参数调用函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:41 25 4
gpt4 key购买 nike

我在 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

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

最佳答案

(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)))

你说:不能用可选参数调用函数

当然可以。错误消息确实说明了其他内容。您可以调用带有可选参数的函数。错误在函数内部。

错误说:错误:未定义的函数 N 用参数 () 调用。

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

现在问问自己:

  • 函数调用是什么样的?

  • 答案:左括号、函数、可能的一些参数、右括号

  • (n) 是什么样子的?

  • 回答:好像是函数调用。

  • 这是你想要的吗?

  • 当然不是。

  • 你想要什么?

  • 变量值。

  • 那看起来像什么?

  • 只是 n

  • 还有其他错误吗?

  • 嗯。

  • 第三行的表格呢?

  • 这看起来也不对。

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

关于function - LISP - 无法使用可选参数调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com