gpt4 book ai didi

common-lisp - common-lisp中有关可选参数的错误

转载 作者:行者123 更新时间:2023-12-04 16:31:41 33 4
gpt4 key购买 nike

SBCL 64位,1.1.7

如果我想创建一个包并使用package:CL中的一些符号,我将创建一个像这样的包:

(defpackage :foo
(:import-from :cl
:defun :defmacro :in-package
:null :car :cdr :cons :if
:eq))

但是,在此程序包中,如果我定义了带有可选参数的函数,并且在不提供可选参数的情况下调用了该函数,则总是会收到错误消息:
(defun test (&optional a))

(test)

invalid number of arguments: 0
[Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]

Restarts:
0: [RETRY] Retry SLIME interactive evaluation request.
1: [*ABORT] Return to SLIME's top level.
2: [REMOVE-FD-HANDLER] Remove #<SB-IMPL::HANDLER INPUT on descriptor 10: #<CLOSURE (COMMON-LISP:LABELS SWANK-BACKEND::RUN :IN SWANK-BACKEND:ADD-FD-HANDLER) {100490B95B}>>
3: [ABORT] Exit debugger, returning to top level.

定义宏会得到相同的错误,但具有更多信息:
(defmacro test (&rest body))

(test)

error while parsing arguments to DEFMACRO TEST:
invalid number of elements in
()
to satisfy lambda list
(&REST BODY):
exactly 2 expected, but 0 found
[Condition of type SB-KERNEL::ARG-COUNT-ERROR]

我认为可能是因为缺少:CL中的某些符号,所以如何解决此问题?谢谢。

最佳答案

我相信这将为这个问题提供一些启发;)

CL-USER> (defpackage :foo
(:import-from :cl
:defun :defmacro :in-package
:null :car :cdr :cons :if
:eq))
#<PACKAGE "FOO">
CL-USER> (in-package :foo)
#<COMMON-LISP:PACKAGE "FOO">
FOO> (defun bar (&optional baz))
; in: DEFUN BAR
; (SB-INT:NAMED-LAMBDA FOO::BAR
; (FOO::&OPTIONAL FOO::BAZ)
; (BLOCK FOO::BAR))
;
; caught COMMON-LISP:STYLE-WARNING:
; suspicious variable in lambda list: &OPTIONAL.
;
; caught COMMON-LISP:STYLE-WARNING:
; suspicious variable in lambda list: &OPTIONAL.
;
; caught COMMON-LISP:STYLE-WARNING:
; The variable &OPTIONAL is defined but never used.
;
; caught COMMON-LISP:STYLE-WARNING:
; The variable BAZ is defined but never used.
;
; compilation unit finished
; caught 4 STYLE-WARNING conditions
BAR
FOO> (in-package :cl)
#<PACKAGE "COMMON-LISP">
CL> (defpackage :foo
(:import-from :cl
:defun :defmacro :in-package :&optional
:null :car :cdr :cons :if
:eq))
Select a symbol to be made accessible in package FOO:
1. COMMON-LISP:&OPTIONAL
2. FOO::&OPTIONAL

Enter an integer (between 1 and 2): 1

#<PACKAGE "FOO">
CL> (in-package :foo)
#<COMMON-LISP:PACKAGE "FOO">
FOO> (defun bar (&optional baz))
; in: DEFUN BAR
; (SB-INT:NAMED-LAMBDA FOO::BAR
; (&OPTIONAL FOO::BAZ)
; (BLOCK FOO::BAR))
;
; caught COMMON-LISP:STYLE-WARNING:
; The variable BAZ is defined but never used.
;
; compilation unit finished
; caught 1 STYLE-WARNING condition
COMMON-LISP:STYLE-WARNING: redefining FOO::BAR in DEFUN
BAR
FOO>
&optional&rest等只是与其他符号一样的符号,您也需要导入这些符号。但是,也许这不是从 cl包中导入的最佳方法……除非您确定那是您所需要的。以防万一:您可以用 :use整个软件包,而不是逐个符号 :import-from

关于common-lisp - common-lisp中有关可选参数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16837360/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com