SBCL-循环功能中的“非法功能调用”

时间:2019-10-23 02:38:04

标签: lisp common-lisp

这是我正在编程的代码的一般示例:

(defun test (a b)
  (loop for x in '(a b) collect
    ((setf a (+ a b))
    (loop for y in '(a b) 
          (setf a (+ a b))
          (loop for z in '(a b) 
            (setf a (+ a b))))
    (list a b))))

当我在REPL中调用函数(测试)时,它给了我“非法函数调用”,如下所示:

> (test 1 1)

debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread
#<THREAD "main thread" RUNNING {10005E85B3}>:
  Execution of a form compiled with errors.
Form:
  ((SETF A (+ A B))
 (LOOP FOR Y IN '(A B) (SETF A (+ A B)) (LOOP FOR Z IN '(A B) (SETF A
                                                                      (+ A
                                                                         B))))
 (LIST A B))
Compile-time error:
  illegal function call

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(FRED5 #<unused argument> #<unused argument>)
   source: ((SETF A (+ A B))
            (LOOP FOR Y IN '(A B) (SETF A (+ A B)) (LOOP FOR Z IN '(A B) (SETF A
                                                                                 (+
                                                                                  A
                                                                                  B))))
            (LIST A B))
0] 0

>

似乎我的第一个(循环)参数做错了什么,但我找不到。 有人可以帮忙吗? 谢谢!

编辑1:

因此,我进行了@Rainer Joswig和@Xero Smith建议的调整:

(defun test (a b)
  (loop for x in '(a b) collect
    (setf a (+ x 1))
    (loop for y in '(a b) do
          (setf a (+ y 1))
          (loop for z in '(a b) do 
            (setf a (+ z 1))))
    (list a b)))

但是现在,我遇到以下错误:

> (test 1 1)

debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread
#<THREAD "main thread" RUNNING {10005E85B3}>:
  Execution of a form compiled with errors.
Form:
  (LOOP FOR X IN '(A B)
      COLLECT (SETF A (+ X 1)) (LOOP FOR Y IN '(A B)
                                     DO (SETF A (+ Y 1)) (LOOP FOR Z IN '(A B)
                                                               DO (SETF A
                                                                          (+ Z
                                                                             1)))) (LIST
                                                                                    A
                                                                                    B))
Compile-time error:
  during macroexpansion of (LOOP FOR X ...). Use *BREAK-ON-SIGNALS* to intercept.

 (LOOP FOR Y IN '(A B)
       DO (SETF A (+ Y 1)) (LOOP FOR Z IN '(A B)
                                 DO (SETF A
                                            (+ Z
                                               1)))) found where a LOOP keyword or LOOP type keyword expected
current LOOP context: COLLECT (SETF A (+ X 1)) (LOOP FOR Y IN '(A B)
                                                     DO (SETF A
                                                                (+ Y
                                                                   1)) (LOOP FOR Z IN '(A
                                                                                        B)
                                                                             DO (SETF A
                                                                                        (+
                                                                                         Z
                                                                                         1)))).

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

3 个答案:

答案 0 :(得分:4)

FAULTMASK

答案 1 :(得分:3)

您的修复仍然错误。 awk 'BEGIN { srand() } 1; { print "path/to/file" int(rand() * 8) }' file 采用一种形式作为其参数。您现在拥有collect

另一方面,collect (setf ...) (loop ...)子句的确采用多种形式。

也许您正在寻找或关闭此功能:

do

如果要将外部(defun test (a b) (loop for x in '(a b) do (setf a (+ a b)) (loop for y in '(a b) do (setf a (+ a b)) (loop for z in '(a b) do (setf a (+ a b)))) collect (list a b)))) setffor y组合成单个(list a b)子句,则需要collect

progn

答案 2 :(得分:2)

这是您的问题:((setf a (+ a b))替换为(setf a (+ a b))。这是一个错误,因为表达式((setf a (+ a b))不是函数。