Lisp函数调用错误

发布于 2024-10-03 03:16:48 字数 1322 浏览 8 评论 0原文

我写了一个像这样的 Lisp 函数:

(defun power (base exponent)
  (if (= exponent 0)
      1
    (* base (power (- exponent 1)))))

然而,当我尝试调用它时,我收到一些错误:

CL-USER 2 > (power 2 3)

Error: POWER got 1 arg, wanted at least 2.
  1 (abort) Return to level 0.
  2 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 3 : 1 > (power 2)

Error: POWER got 1 arg, wanted at least 2.
  1 (abort) Return to level 1.
  2 Return to debug level 1.
  3 Return to level 0.
  4 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 4 : 2 > (power 2 3 4)

Error: POWER got 3 args, wanted 2.
  1 (continue) Ignore the extra arg.
  2 (abort) Return to level 2.
  3 Return to debug level 2.
  4 Return to level 1.
  5 Return to debug level 1.
  6 Return to level 0.
  7 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

这里发生了什么?如果我给它两个参数,它会认为我给了它一个。如果我给它三个,它就会认为我给了它三个。如果我给它一个,它会认为我给了它一个......

I've written a Lisp function like this:

(defun power (base exponent)
  (if (= exponent 0)
      1
    (* base (power (- exponent 1)))))

When I try to call it, however, I get some errors:

CL-USER 2 > (power 2 3)

Error: POWER got 1 arg, wanted at least 2.
  1 (abort) Return to level 0.
  2 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 3 : 1 > (power 2)

Error: POWER got 1 arg, wanted at least 2.
  1 (abort) Return to level 1.
  2 Return to debug level 1.
  3 Return to level 0.
  4 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 4 : 2 > (power 2 3 4)

Error: POWER got 3 args, wanted 2.
  1 (continue) Ignore the extra arg.
  2 (abort) Return to level 2.
  3 Return to debug level 2.
  4 Return to level 1.
  5 Return to debug level 1.
  6 Return to level 0.
  7 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

What's going on here? If I give it two arguments, it thinks I gave it one. If I give it three, it thinks I gave it three. If I give it one, it thinks I gave it one...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-10-10 03:16:48

这是只有一个参数的递归调用:

(power (- exponent 1))

它应该是这样的:

(power base (- exponent 1))

It is the recursive call that only has one argument:

(power (- exponent 1))

It should be like this:

(power base (- exponent 1))
琉璃梦幻 2024-10-10 03:16:48

递归调用是你的问题。您忘记将基数作为第一个参数传递。

(* base (power base (- exponent 1)))))

应该是:

(* base (power base (- exponent 1)))))

The recursive call is your problem. You forgot to pass the base in as the first argument.

(* base (power (- exponent 1)))))

should be:

(* base (power base (- exponent 1)))))

放我走吧 2024-10-10 03:16:48

编译您的函数。在 LispWorks 中,使用 c-sh-c 在编辑器中编译定义。

在 REPL 中:

CL-USER 18 > (defun power (base exponent)
               (if (= exponent 0)
                   1
                 (* base (power (- exponent 1)))))
POWER

CL-USER 19 > (compile 'power)
;;;*** Warning in POWER: POWER is called with the
;;;    wrong number of arguments: Got 1 wanted 2

编译器已经告诉您代码有问题。

请注意,LispWorks Listener (REPL) 无法编译。您必须使用函数 COMPILE 来编译在侦听器中输入的定义。或者,您可以将定义键入编辑器窗口并从那里进行编译(通过编译文件、缓冲区或表达式)。 LispWorks 还具有定位发生错误的代码的功能。

Compile your functions. In LispWorks use c-sh-c to compile a definition in the editor.

Here in the REPL:

CL-USER 18 > (defun power (base exponent)
               (if (= exponent 0)
                   1
                 (* base (power (- exponent 1)))))
POWER

CL-USER 19 > (compile 'power)
;;;*** Warning in POWER: POWER is called with the
;;;    wrong number of arguments: Got 1 wanted 2

The Compiler will already tell you that there is a problem with the code.

Note that the LispWorks Listener (the REPL) does not compile. You have to compile the definitions you enter at the Listener with the function COMPILE. Alternatively you can type the definitions into an editor window and compile it from there (by either compiling the file, the buffer or the expression). LispWorks also has features to locate code where an error happens.

明月松间行 2024-10-10 03:16:48

Lisp 附带了函数 expt,所以不需要定义自己的。

(除非这是练习或家庭作业,在这种情况下,您可能想了解更有效的方法,例如 指数通过平方。)

Lisp comes with the function expt, so no need to define your own.

(Unless this is an exercise or homework, in which case you might want to look at more efficient methods, such as exponentiation by squaring.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文