这是一个稍微高级的示例,显示了普通lisp的其他一些功能。我们从一个简单的Hello, World!功能开始,并在REPL上演示一些交互式开发。请注意,从分号;到行的其余部分的所有文本均为注释。
CL-USER> (defun hello () (format t "Hello, World!~%")) ;We start as before HELLO CL-USER> (hello) Hello, World! NIL CL-USER> (defun hello-name (name) ;A function to say hello to anyone (format t "Hello, ~a~%" name)) ;~a prints the next argument to format HELLO-NAME CL-USER> (hello-name "Jack") Hello, Jack NIL CL-USER> (hello-name "jack") ;doesn't capitalise names Hello, jack NIL CL-USER> (defun hello-name (name) ;format has a feature to convert to title case (format t "Hello, ~:(~a~)~%" name)) ;anything between ~:( and ~) gets it WARNING: redefining COMMON-LISP-USER::HELLO-NAME in DEFUN HELLO-NAME CL-USER> (hello-name "jack") Hello, Jack NIL CL-USER> (defun hello-name (name) (format t "Hello, ~:(~a~)!~%" name)) WARNING: redefining COMMON-LISP-USER::HELLO-NAME in DEFUN HELLO-NAME CL-USER> (hello-name "jack") ;now this works Hello, Jack! NIL CL-USER> (defun hello (&optional (name "world")) ;we can take an optional argument (hello-name name)) ;name defaults to "world" WARNING: redefining COMMON-LISP-USER::HELLO in DEFUN HELLO CL-USER> (hello) Hello, World! NIL CL-USER> (hello "jack") Hello, Jack! NIL CL-USER> (hello "john doe") ;note that this capitalises both names Hello, John Doe! NIL CL-USER> (defun hello-person (name &key (number)) (format t "Hello, ~a ~r" name number)) ;~r prints a number in English HELLO-PERSON CL-USER> (hello-person "Louis" :number 16) ;this doesn't quite work Hello, Louis sixteen NIL CL-USER> (defun hello-person (name &key (number)) (format t "Hello, ~:(~a ~:r~)!" name number)) ;~:r prints an ordinal WARNING: redefining COMMON-LISP-USER::HELLO-PERSON in DEFUN HELLO-PERSON CL-USER> (hello-person "Louis" :number 16) Hello, Louis Sixteenth! NIL CL-USER> (defun hello-person (name &key (number)) (format t "Hello, ~:(~a ~@r~)!" name number)) ;~@r prints Roman numerals WARNING: redefining COMMON-LISP-USER::HELLO-PERSON in DEFUN HELLO-PERSON CL-USER> (hello-person "Louis" :number 16) Hello, Louis Xvi! NIL CL-USER> (defun hello-person (name &key (number)) ;capitalisation was wrong (format t "Hello, ~:(~a~) ~:@r!" name number)) WARNING: redefining COMMON-LISP-USER::HELLO-PERSON in DEFUN HELLO-PERSON CL-USER> (hello-person "Louis" :number 16) ;thats better Hello, Louis XVI! NIL CL-USER> (hello-person "Louis") ;we get an error because NIL is not a number Hello, Louis ; Evaluation aborted on #<SB-FORMAT:FORMAT-ERROR {1006641AB3}>. CL-USER> (defun say-person (name &key (number 1 number-p) (title nil) (roman-number t)) (let ((number (if number-p (typecase number (integer (format nil (if roman-number " ~:@r" " ~:(~:r~)") number)) (otherwise (format nil " ~:(~a~)" number))) "")) ; here we define a variable called number (title (if title (format nil "~:(~a~) " title) ""))) ; and here one called title (format nil "~a~:(~a~)~a" title name number))) ;we use them here SAY-PERSON CL-USER> (say-person "John") ;some examples "John" CL-USER> (say-person "john doe") "John Doe" CL-USER> (say-person "john doe" :number "JR") "John Doe Jr" CL-USER> (say-person "john doe" :number "Junior") "John Doe Junior" CL-USER> (say-person "john doe" :number 1) "John Doe I" CL-USER> (say-person "john doe" :number 1 :roman-number nil) ;this is wrong "John Doe First" CL-USER> (defun say-person (name &key (number 1 number-p) (title nil) (roman-number t)) (let ((number (if number-p (typecase number (integer (format nil (if roman-number " ~:@r" " the ~:(~:r~)") number)) (otherwise (format nil " ~:(~a~)" number))) "")) (title (if title (format nil "~:(~a~) " title) ""))) (format nil "~a~:(~a~)~a" title name number))) WARNING: redefining COMMON-LISP-USER::SAY-PERSON in DEFUN SAY-PERSON CL-USER> (say-person "john doe" :number 1 :roman-number nil) ;thats better "John Doe the First" CL-USER> (say-person "louis" :title "king" :number 16 :roman-number nil) "King Louis the Sixteenth" CL-USER> (say-person "louis" :title "king" :number 16 :roman-number t) "King Louis XVI" CL-USER> (defun hello (&optional (name "World") &rest arguments) ;now we will just (apply #'hello-name name arguments)) ;pass all arguments to hello-name WARNING: redefining COMMON-LISP-USER::HELLO in DEFUN HELLO CL-USER> (defun hello-name (name &rest arguments) ;which will now just use (format t "Hello, ~a!" (apply #'say-person name arguments))) ;say-person WARNING: redefining COMMON-LISP-USER::HELLO-NAME in DEFUN HELLO-NAME CL-USER> (hello "louis" :title "king" :number 16) ;this works now Hello, King Louis XVI! NIL CL-USER>
这突出显示了Common Lispformat函数的一些高级功能,以及一些可选参数和关键字参数(例如:number)之类的功能。这也提供了一个常见的REPL交互式开发示例。
本文向大家介绍common-lisp 你好,世界,包括了common-lisp 你好,世界的使用技巧和注意事项,需要的朋友参考一下 示例 接下来是摘录自Common Lisp的REPL会议,其中摘录了“ Hello,World!”。函数已定义并执行。请参阅本页底部的备注,以更全面地了解REPL。 这定义了名为的零参数的“函数” hello,它将把字符串"Hello, World!"后跟换行符的字符
Common Lisp Koans(lisp-koans)是一个语言学习练习程序,类似 ruby koans,python koans 等等。Common Lisp Koans 主要是帮助学习一些 lisp 规范特性和改进,可以学习到大量的 Common Lisp 语言特性。 终端,在文件 'contemplate.lsp' 执行 lisp 解析器: sbcl --script contempla
本文向大家介绍common-lisp 定义匿名函数,包括了common-lisp 定义匿名函数的使用技巧和注意事项,需要的朋友参考一下 示例 Common Lisp中的函数是一等值。可以使用创建匿名函数lambda。例如,这是3个参数的函数,然后我们使用调用funcall 匿名函数也可以直接使用。Common Lisp为此提供了一种语法。 匿名函数也可以存储为全局函数: 带引号的lambda表达式
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。