Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp 未使用的词汇变量_Lisp_Common Lisp - Fatal编程技术网

Lisp 未使用的词汇变量

Lisp 未使用的词汇变量,lisp,common-lisp,Lisp,Common Lisp,刚开始学习lisp。我不知道我为什么会犯这些错误,甚至不知道它们是什么意思。我只是想用Gregory Leibniz级数对pi的近似值进行编码,下面是代码 (defun gl (n) (defparameter x 0) ;init variable to hold our runnning sum (loop for y from 0 to n ;number of i

刚开始学习lisp。我不知道我为什么会犯这些错误,甚至不知道它们是什么意思。我只是想用Gregory Leibniz级数对pi的近似值进行编码,下面是代码

(defun gl (n)
           (defparameter x 0)                        ;init variable to hold our runnning sum
           (loop for y from 0 to n                   ;number of iterations, starting from 0 to desired n
              (if (= y 0)                                      ;if n is 0 then we just want 4
                 (defparameter w 4))
              (if (> y 0)                                      ;else, 4*(-1^y)/((2 * y)+1)
                 (defparameter w (* 4 (/ (exp -1 y) (+ (* 2 y) 1)))))
              (+ x w))                                         ;add to our running sum
           (write x))                                        ;once loop is over, print x.
我尝试过使用setq、defvar、let等代替defparameter,但仍然得到“未声明的自由变量X”

我还得到错误“Unused lexical variable N”,即使我在循环中使用它,这也很奇怪


我如何解决这个问题?为什么会发生?谢谢

以下是Emacs自动缩进后的代码:

(defun gl (n)
  (defparameter x 0)
  (loop for y from 0 to n
       (if (= y 0)
           (defparameter w 4))
       (if (> y 0)
           (defparameter w (* 4 (/ (exp -1 y) (+ (* 2 y) 1)))))
       (+ x w))
  (write x))
使用SBCL编译以下代码时会出现一个错误和两个警告

一条警告说,
x
未定义。 您不应该从函数内部调用
defparameter
,因为它们用于声明动态变量并在全局范围内设置其值。更喜欢使用let绑定,或者,因为您已经在使用循环,所以使用
with
子句。如果要修改绑定,请使用

错误来自
循环
的宏扩展,其格式不正确。对于SBCL,这意味着对于函数编译的其余部分,代码被视为死代码;这就解释了为什么不使用
n
,这就是第二个警告的内容

还有各种修复需要完成:

  • 使用函数
    EXPT
    ,而不是
    EXP
  • 调用
    (+xw)
    只计算一个值,而不修改
    x
    ,结果是无用的
  • 喜欢使用
    if
    作为表达式,就像其他语言中的三元运算符一样,在您的情况下,代码可以简化
  • 可以使用函数
    1+
    添加一个(这是函数的名称,不是添加常量的特殊语法)
  • 很少需要
    write
    操作,尤其是在计算数学公式时;只要返回值,REPL就会自动打印它
使代码正常工作的小更正:

(defun gl (n)
  (let ((x 0))
    (loop
       for y from 0 to n
       for w = (if (= y 0)
                   4
                   (* 4 (/ (expt -1 y) (+ (* 2 y) 1))))
       do (setf x (+ x w)))
    (write x)))
我个人会去掉
x
w
,使用
SUM
循环子句

(defun gl (n)
  (loop
     for y from 0 to n
     sum (if (zerop y)
             4
             (* 4 (/ (expt -1 y)
                     (1+ (* 2 y)))))))

不要在
defun
中使用
defparameter
。您应该查看Hyperspec或一些介绍书(请参阅信息部分获得建议),了解使用
let
或内部
循环定义局部变量的惯用方法。哇!我没有线索,我做了很多错误的LOL。我想我习惯于用Python和C++进行手持操作。谢谢你的帮助@MIT主要是你熟悉它的问题,你可能很容易赶上CL,这在某些方面比C++和Python(例如更少的角落情况)更简单。我们可以编译一个
def foo(x):。。。在Python3中返回y
,但没有警告
x
未使用,或者警告
y
未定义。弹出一个定义非工作编译函数的
.pyc
文件。