common-lisp 你好,名字
示例
这是一个稍微高级的示例,显示了普通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>
这突出显示了CommonLispformat函数的一些高级功能,以及一些可选参数和关键字参数(例如:number)之类的功能。这也提供了一个常见的REPL交互式开发示例。