当前位置: 首页 > 编程笔记 >

common-lisp 休息参数

姚飞昂
2023-03-14
本文向大家介绍common-lisp 休息参数,包括了common-lisp 休息参数的使用技巧和注意事项,需要的朋友参考一下

示例

可以&REST在所需参数之后的关键字中给定一个单个剩余参数。如果存在这样的参数,则该函数可以使用多个参数,这些参数将被分组到其余参数的列表中。请注意,该变量CALL-ARGUMENTS-LIMIT确定可在函数调用中使用的最大参数数目,因此,参数数目限制为特定于实现的最小值50或更多的参数。

(defun foobar (x y &rest rest)
  (format t "X (~s) and Y (~s) are required.~@
             The function was also given following arguments: ~s~%"
          x y rest))

(foobar 10 20)
; X (10) and Y (20) are required.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 30 40 50 60 70 80)
; X (10) and Y (20) are required.
; The function was also given following arguments: (30 40 50 60 70 80)
;=> NIL

休息和关键字参数一起

其余参数可以在关键字参数之前。在这种情况下,它将包含用户给出的属性列表。关键字值仍将绑定到相应的关键字参数。

(defun foobar (x y &rest rest &key (z 10 zp))
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
             The function was also given following arguments: ~s~%"
          x y z zp rest))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z (10) is a keyword argument. It wasn't given.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 :z 30)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30)
;=> NIL

&ALLOW-OTHER-KEYS可以在lambda列表的末尾添加关键字,以允许用户提供未定义为参数的关键字参数。他们将进入其余列表。

(defun foobar (x y &rest rest &key (z 10 zp) &allow-other-keys)
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
             The function was also given following arguments: ~s~%"
          x y z zp rest))

(foobar 10 20 :z 30 :q 40)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30 :Q 40)
;=> NIL
           

 类似资料:
  • Common Lisp Koans(lisp-koans)是一个语言学习练习程序,类似 ruby koans,python koans 等等。Common Lisp Koans 主要是帮助学习一些 lisp 规范特性和改进,可以学习到大量的 Common Lisp 语言特性。 终端,在文件 'contemplate.lsp' 执行 lisp 解析器: sbcl --script contempla

  • Steel Bank Common Lisp (SBCL) 源自于 CMUCL, 是一种高性能的Common Lisp编译器。它是开源/免费软件,采用自由许可。除了ANSI Common Lisp的编译器和运行系统,它提供了一个交互的运行环境,包括一个调试器,统计分析器,一个代码覆盖工具,以及许多其他的扩展。 SBCL 可运行于许多 POSIX 平台上,Windows 上现为试验阶段。 

  • 本文向大家介绍common-lisp True 和 False,包括了common-lisp True 和 False的使用技巧和注意事项,需要的朋友参考一下 示例 特殊符号T表示Common Lisp中的值true,而特殊符号NIL表示false: 在标准中,它们被称为“常量变量”(sic!),因为它们是无法修改其值的变量。因此,您不能将它们的名称用于普通变量,如以下不正确的示例所示: 实际上,

  • 本文向大家介绍common-lisp 有界环,包括了common-lisp 有界环的使用技巧和注意事项,需要的朋友参考一下 示例 我们可以使用重复操作多次repeat。            

  • 本文向大家介绍common-lisp 照应宏,包括了common-lisp 照应宏的使用技巧和注意事项,需要的朋友参考一下 示例 照应宏是一种引入变量(通常为IT)的宏,该变量捕获用户提供的表单的结果。一个常见的示例是Anaphoric If,它与regular一样IF,但也定义了变量IT以引用测试表单的结果。            

  • 本文向大家介绍common-lisp 高阶函数,包括了common-lisp 高阶函数的使用技巧和注意事项,需要的朋友参考一下 示例 Common Lisp包含许多高阶函数,这些函数是传递给参数的函数并调用它们。也许最根本的是funcall和apply: 还有许多其他高阶函数,例如,将函数多次应用于列表的元素。