(defunyay ()
"Insert “Yay!” at cursor position." (interactive)
(insert"Yay!"))
以下是一个定义的模板
1
2
3
4
5
6
7
8
9
10
11
(defunmyCommand ()
"One sentence summary of what this command do.
More detailed documentation here." (interactive)
(let (localVar1localVar2…)
; do something here …; …; last expression is returned )
)
(carlist) ;first element(nthnlist) ;nth element(car (lastlist)) ;last element(cdrlist) ;2nd to last elements.(nthcdrnlist) ;nth to last elements.(butlastlistn) ;without the last n elements.
向前追加 list
1
(consxlist) ;Return a new list, with x added to front. (prepend)
向后追加 list
1
(appendlist1list2) ;Return a new list, join two lists
修改 list 变量
1
2
3
4
5
(pushlist) ;Add a element to the front variable. Returns the new list.(poplist) ;Remove first element from the variable. Returns the removed element.(nbutlastlistn) ;Remove last n elements from the variable. Returns the new value of the variable.(setcarlistx) ;replaces the first element in list with x. Returns x.(setcdrlistx) ;replaces the rest of elements in list with x. Returns x.
(setqxx'(a1b2))
;; set value to a existing key(setqxx (plist-putxx'b3))
;; must use setq, because plist-put works by return value(plist-getxx'b) ; 3;; set value to new key(setqxx (plist-putxx'd3))
(plist-getxx'd) ; 3
检测是否存在
1
2
3
4
(setqxx'(a1b2))
;; check if a key exist(plist-memberxx'b)
Symbol Property List
设置
1
2
3
(put'ff'xx5)
(setplist'ff'(a1b2))
访问
1
2
3
(get'ff'xx) ; nil(symbol-plist'ff )
Map
mapcar
(mapcar FUNCTION SEQUENCE)
它会将函数 FUNCTION 应用到 SEQUENCE 中的每一个元素, 然后返回一个 list 的结果.
function cell 或 value cell 可能为空, 这时就被称为 void . 当你获取一个 cell 的 value 是 void 时, 会产生一个 lisp 错误. (一个空emtpy cell, 不同于有一个 value 为 nil 的 cell).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
;; get symbol's name cell value(symbol-name'sin) ; "sin";; get symbol's value cell value(symbol-value'sin) ; void-variable error;; because the value cell of the symbol sin is void;; or, we just say that sin isn't a variable;; get symbol's function cell value(symbol-function'sin) ;#<subr sin>;; the value is a primitive function (written in C), and has print form of #<subr sin>;; get symbol's property list cell value(symbol-plist'sin) ; (side-effect-free t)
quoting symbol
一个 symbol , 典型情况下是用于计算来获取它的 value 的. 但你可以通过 quoting symbol 来停止这样子. 例如 (quote x) . 你可以想象 quote 是拥有 evaluation (仅仅持有它, 而不要去计算它).
函数 set 与 setq 几乎是一样的, 最主要的不同是 set 并不会自动将第一个参数进行 quote, 而 setq 会.
例如
1
2
3
(setqf'1+)
(setqf'cos)
(setqf'sqrt)
我们用 f 来包装, 因为我们不确定到底使用哪个函数, 直到运行时才能知道.
然后
1
2
3
4
5
6
7
8
;; here's our data(setqmylist'(123))
;; normally, when using mapcar, we want first arg quoted(mapcar'1+mylist) ; (2 3 4);; here, we don't want first arg quoted(mapcarfmylist) ; (1.0 1.4142135623730951 1.7320508075688772)
检测个值是否是 symbol
1
2
3
4
5
6
(setqx1123 )
(symbolpx1) ; nil;; nil, because x1 is evaluated, and that value is 123, not a symbol(symbolp'x1) ; t
获取 symbol 的四个 cell 值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
;; get symbol's name cell value(symbol-name'sin) ; "sin";; get symbol's value cell value(symbol-value'sin) ; void-variable error;; because the value cell of the symbol sin is void;; or, we just say that sin isn't a variable;; get symbol's function cell value(symbol-function'sin) ;#<subr sin>;; the value is a primitive function (written in C), and has print form of #<subr sin>;; get symbol's property list cell value(symbol-plist'sin) ; (side-effect-free t)
设置 symbol 的四个 cell 值
名字是不能修改的.
设置 value cell
1
2
3
4
5
;; set a symbol's value cell(setqy"yes yes")
;; get it(symbol-value'y) ; "yes yes"
检测 value cell 是否为空
1
2
3
4
5
(boundp'h) ; nil(setqh4)
(boundp'h) ; t
设置 function cell
1
2
3
4
5
6
;; a function that returns 4(defunz () 4)
;; Note: return value of defun is not defined;; get a symbol's function cell value(symbol-function'z) ; (lambda nil 4)(boundp 'h) ; nil
检测是否是 function
1
2
3
4
5
6
7
(fboundp'f) ; nil;; define a function that return 3(defunf () 3)
;; now the fuction cell is filled(fboundp'f) ; t
设置 property cell
参见上面的 Property List
综合例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
(setqx"hello world")
(defunx (ab)
"a + b" (+ab)
)
(x34);7(messagex);"hello world"(symbol-name'x);"x"(symbol-value'x);"hello world"(symbol-function'x); (lambda (a b) "a + b" (+ a b))(symbol-plist'x); (group-documentation "The X Window system.")