任何Common Lisp(内置)函数是否返回超过2个值?我知道许多人返回2,但我想不到一个返回3。
(我在这里看到一条关于返回超过2个值的评论,并试图想到CL这样做的情况,但不能。)
答案 0 :(得分:27)
是的,存在这样的功能。以下是COMMON-LISP包中的完整函数列表,它们返回三个值,如SBCL源代码中所声明的那样:
COMPILE required: 3, optional: 0, rest?: NIL
INTEGER-DECODE-FLOAT required: 3, optional: 0, rest?: NIL
COMPILE-FILE required: 3, optional: 0, rest?: NIL
GET-PROPERTIES required: 3, optional: 0, rest?: NIL
FUNCTION-LAMBDA-EXPRESSION required: 3, optional: 0, rest?: NIL
DECODE-FLOAT required: 3, optional: 0, rest?: NIL
RENAME-FILE required: 3, optional: 0, rest?: NIL
此外,以下函数返回大于3的常量数值:
DECODE-UNIVERSAL-TIME required: 9, optional: 0, rest?: NIL
GET-DECODED-TIME required: 9, optional: 0, rest?: NIL
这些函数返回可变数量的值,因此可能超过三个:
NO-APPLICABLE-METHOD required: 0, optional: 0, rest?: T
NO-NEXT-METHOD required: 0, optional: 0, rest?: T
VALUES required: 0, optional: 0, rest?: T
(I've omitted some functions from this list where SBCL does not declare
a values type explicitly. get-setf-expansion is one of them.)
列的说明:required
是这些函数的最小返回值数,optional
是SBCL认为可能返回或可能不返回的固定数量的返回值,rest?
表示预期可变数量的值。 (只有macroexpand
和macroexpand-1
实际上使用& optional,不要问我原因。)
只是为了好玩,这是我用来提出这些表的源代码:
(do-external-symbols (sym :common-lisp)
(when (fboundp sym)
(multiple-value-bind (required optional rest)
(let ((fun-type (sb-int:info :function :type sym)))
(etypecase fun-type
(sb-kernel:fun-type
(let ((returns
(sb-kernel:fun-type-returns fun-type)))
(etypecase returns
(sb-kernel:values-type
(values (length (sb-kernel:values-type-required returns))
(length (sb-kernel:values-type-optional returns))
(sb-kernel:values-type-rest returns)))
(sb-kernel:named-type
(if (sb-kernel:named-type-name returns)
(values 1 0 t)
(values 0 0 nil))))))
(t
(values 0 0 t))))
(format t
"~A~40Trequired: ~D, optional: ~D, rest?: ~A~%"
sym
required optional rest))))
答案 1 :(得分:7)
decode-universal-time返回九个值。
答案 2 :(得分:4)
有一个get-setf-expansion函数。它返回5个值。
答案 3 :(得分:2)