一 、quoteide
lisp 使用s-expr表示数据和代码,一般会将第一项做为函数,而将后续元素当作参数传给第一项进行计算。能够经过quote来进行其余解析,quote可用(‘)表示:函数
1 ( + 1 1) 2 2 3 4 ‘(+ 1 1) 5 (+ 1 1)
二 、反引号(·) oop
(·)按键(~)为同一个键盘按键。做用和quote差很少,但可使其中部分字符串保留原解析,须要使用,放在须要保留原解析的字符串以前。this
`(+ 1 ,(+ 0 1))
(+ 1 1)
3、 #’idea
该操做符表示对函数的引用spa
4、控制结构code
doorm
(do ((x 1 (+ x 1)) (y 1 (* y 2))) ((> x 5) y) (print y) (print 'working))
condblog
(cond ((evenp a) a) ;if a is even return a ((> a 7) (/ a 2)) ;else if a is bigger than 7 return a/2 ((< a 5) (- a 1)) ;else if a is smaller than 5 return a-1 (t 17)) ;else return 17 2
case字符串
(setq x 'd) (case x (a 5) ((d e) 7) ((b f) 3) (otherwise 9)) 7
loop
(setq a 4) (loop (setq a (+ a 1)) (when (> a 6) (return a))) 7
if
(if t 6 5) 6 (if nil 6 5) 5
5、block
Common Lisp 的构造区块(block)的基本操做符:progn 、block 以及 tagbody 。
progn主体中的表达式会顺序求值并返回最后一个表达式的值。
(progn (format t "a") (format t "b") (+ 11 12)) ab 23
block至关于带有特别出口的progn。
(block head (format t "Here we go.") (return-from head 'idea) (format t "We'll never see this.")) Here we go. IDEA
tagbody大多数迭代操做符都隐含一个tagbody。
(tagbody (setf x 0) top (setf x (+ x 1)) (format t "~A " x) (if (< x 10) (go top))) 1 2 3 4 5 6 7 8 9 10 NIL
return宏把传入的参数当作封闭区块nil的返回值。
(block nil (return 27)) 27
在一个block(明肯定义的或隐含的),不论return-from或者return都不会运行。
6、函数
可经过 symbol-function 给函数配置某个名字:
(setf (symbol-function 'add2) #'(lambda (x) (+ x 2)))
新的全局函数能够这样定义,用起来和 defun 所定义的函数同样:
(add2 0)
2
经过 defun 或 symbol-function 搭配 setf 定义的函数是全局函数。你能够像存取全局变量那样,在任何地方存取它们。定义局部函数也是有可能的,局部函数和局部变量同样,只在某些上下文内能够访问。
局部函数可使用 labels 来定义,它是一种像是给函数使用的 let 。它的第一个实参是一个新局部函数的定义列表,而不是一个变量规格说明的列表。列表中的元素为以下形式: