任何Common Lisp(内置)函数是否返回超过2个值?我知道许多人返回2,但我想不到一个返回3.
(我在这里看到关于返回超过2个值的评论,并试图想到CL做到这一点的情况,但不能.)
是的,存在这样的功能.以下是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
此外,以下函数返回大于三的常量值:
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
实际使用和选购,不要问我为什么.)
只是为了好玩,这里是我用来提出这些表的源代码:
(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))))
decode-universal-time返回九个值.