未定义变量:COMMON-LISP / SBCL

问题描述 投票:0回答:1

在Mac(MacOS Sonoma 14.2.1)上使用SBCL(2.4.1),我有以下功能:

    (defun getEndLoop(n)
        (let ((lpCntThrshld 8192)) ; To be safe and avoid any possible infinite loop.
            (do ((count 0 (+ count 1)) (token n (collatzNext token PrimeList))
                 ; tail implements a mechanism to detect loops in the flow of values.
                 (tail n (if (and (eq stage 0) (eq (mod count 2) 1))
                            (collatzNext tail PrimeList) tail))
                     (stage 0 (if (and (> count 0) (eql token tail)) 1 stage)))

                (progn
                    (if (> count lpCntThrshld) (return-from getEndLoop (list -1 fnID n)))

                    (if (eq stage 1) ;(and (> count 0) (eql token tail))
                                    (progn
                                        (format t "stage=~a" stage)
                                        (return-from getEndLoop NIL)
                                    )
                            ))
            ) ; End do.
        ) ; End getEndLoop-let.
    ) ; End getEndLoop.

当我想运行包含此函数的程序时,我看到以下消息:

    ; caught WARNING:
    ;   undefined variable: COMMON-LISP:PROGN
    ; 
    ; compilation unit finished
    ;   Undefined variable:
    ;     PROGN
    ;   caught 1 WARNING condition
    Unhandled UNBOUND-VARIABLE in thread #<SB-THREAD:THREAD "main thread" RUNNING
                                    {7005550003}>:
      The variable PROGN is unbound.

我使用 progn 有什么问题吗?还是我在错误的环境中使用?

我在其他程序(GNU clisp)中经常使用它,我也在该程序的其他部分使用它,没有任何问题。

有人能发现我哪里出错了吗?

lisp common-lisp sbcl
1个回答
0
投票

rajashekar 是正确的。举例说明:

(defun getEndLoop(n)
  (let ((lpCntThrshld 8192)) 
    (do ((count 0 (+ count 1))
         (token n (collatzNext token PrimeList))
         (tail n (if (and (eq stage 0) (eq (mod count 2) 1))
                     (collatzNext tail PrimeList) tail))
         (stage 0 (if (and (> count 0) (eql token tail)) 1 stage))) ;; <-- end of loop variable definitions

        ;; now DO expects the end test form
        ;; If you don't want one it can be
        ;; (nil)

        ;; but you give what you think is the body
        (progn

例如:

    (do ((count 0 (+ count 1))
         (token n (collatzNext token PrimeList))
         (tail n (if (and (eq stage 0) (eq (mod count 2) 1))
                     (collatzNext tail PrimeList) tail))
         (stage 0 (if (and (> count 0) (eql token tail)) 1 stage)))

        ;; end test form
        ((> count lpCntThrshld)
         (list -1 fnID n))

        (progn
           ;; etc

但是正文是隐式写的

progn
所以你不需要写它。因此,我们可以更好地看到带有缩进的 DO 结构:

    (do ((count 0 (+ count 1))
         (token n (collatzNext token PrimeList))
         (tail n (if (and (eq stage 0) (eq (mod count 2) 1))
                     (collatzNext tail PrimeList) tail))
         (stage 0 (if (and (> count 0) (eql token tail)) 1 stage)))

        ;; end test form
        ((> count lpCntThrshld)
         (list -1 fnID n))

      (do-something)
      (do-something-else))    

你有一对

if
,没有其他分支,它们应该是
when

© www.soinside.com 2019 - 2024. All rights reserved.