Lisp 警告:xx 既未声明也未绑定,它将被视为已声明为 SPECIAL

发布于 2024-08-03 12:51:09 字数 503 浏览 5 评论 0原文

我是 Lisp 新手,正在编写一些简单的程序来更熟悉它。我正在做的事情之一是编写阶乘方法的递归和迭代版本。然而,我遇到了一个问题,似乎无法解决。

我在以下位置看到了类似的错误 ​​Lisp:CHAR 既未声明也未绑定 但实际上并没有达成解决方案,除了OP意识到他犯了一个“打字错误”。在 REPL 中我可以使用 setf 函数并且它工作得很好。我还在 emacs 中使用 LispBox。我将不胜感激任何建议!

(defun it-fact(num)
  (setf result 1)
  (dotimes (i num)
    (setf result (* result (+ i 1)))
  )
)

IT-FACT 中的警告: RESULT 既不被声明也不被约束, 它将被视为如同被宣布为特殊的。

I am new to lisp and am writing a few simple programs to get more familiar with it. One of the things I am doing is writing a recursive and iterative version of a factorial method. However, I have come across a problem and can't seem to solve it.

I saw a similar error at
Lisp: CHAR is neither declared nor bound
but a solution wasn't actually reached, other than the OP realized he made a "typing mistake". In the REPL I can use the setf function and it works fine. I am also using LispBox with emacs. I would appreciate any suggestions!

(defun it-fact(num)
  (setf result 1)
  (dotimes (i num)
    (setf result (* result (+ i 1)))
  )
)

WARNING in IT-FACT :
RESULT is neither declared nor bound,
it will be treated as if it were declared SPECIAL.

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

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

发布评论

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

评论(3

偏爱自由 2024-08-10 12:51:09

Lisp 风格有一些错误或不太好的地方:

(defun it-fact(num)                      ; style: use a space before (
  (setf result 1)                        ; bad: variable RESULT is not introduced
  (dotimes (i num)
    (setf result (* result (+ i 1)))     ; bad: extra addition in each iteration
  )                                      ; style: parentheses on a single line
)                                        ; bad: no useful return value

一个可能的版本:

(defun it-fact (num)
  (let ((result 1))                      ; local variable introduced with LET
    (loop for i from 1 upto num          ; I starts with 1, no extra addition
      do (setf result (* result i)))
    result))                             ; result gets returned from the LET

There are a few things wrong or not so good Lisp style:

(defun it-fact(num)                      ; style: use a space before (
  (setf result 1)                        ; bad: variable RESULT is not introduced
  (dotimes (i num)
    (setf result (* result (+ i 1)))     ; bad: extra addition in each iteration
  )                                      ; style: parentheses on a single line
)                                        ; bad: no useful return value

A possible version:

(defun it-fact (num)
  (let ((result 1))                      ; local variable introduced with LET
    (loop for i from 1 upto num          ; I starts with 1, no extra addition
      do (setf result (* result i)))
    result))                             ; result gets returned from the LET
情场扛把子 2024-08-10 12:51:09

在 Lisp 中,局部变量必须使用 LET 或其他创建局部变量的形式显式声明。
这与 Python 或 JavaScript 等不同,在 Python 或 JavaScript 中,对变量的赋值会在当前词法范围内创建变量。

您的示例可以这样重写:

(defun it-fact(num)
  (let ((result 1))
    (dotimes (i num)
      (setf result (* result (+ i 1))))))

偏离主题的注释:将右括号放在单独的行上是没有意义的。

In Lisp, local variables must be explicitly declared with LET or other forms that create local variables.
That differs from e.g. Python or JavaScript where assignment to variable creates the variable in current lexical scope.

Your example can be rewritten like this:

(defun it-fact(num)
  (let ((result 1))
    (dotimes (i num)
      (setf result (* result (+ i 1))))))

An off-topic comment: there is no point in putting closing parentheses on separate lines.

℉服软 2024-08-10 12:51:09

在开始使用之前,您需要绑定变量“result”(例如使用“let”):

(defun it-fact(num)
  (let ((result 1))
    (dotimes (i num)
      (setf result (* result (+ i 1))))))

有关更多详细信息,您可能需要阅读 这个 ...

You need to bind the variable 'result' - using 'let', for example - before starting to use it:

(defun it-fact(num)
  (let ((result 1))
    (dotimes (i num)
      (setf result (* result (+ i 1))))))

For futher details you might want to read this ...

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