2013年5月15日星期三

笔记:Common Lisp the Language, 2nd Edition

chapter 1  - chapter 2
 
用 | 包围含特殊符号的字符串,省的搞出一堆 \ 转义符

|A(B)|, A|(|B|)|, A\(B\) 三者都表示同一个字符串 A(B)

8进制 #o105
16进制 #x105
2进制 #b1011

函数fn  #'fn 

反引号 ` 与单引号 ' 功能类似,都是后面的内容进入数据模式;但是可以用逗号,将里面部分list内容切换到代码模式

一些键值 #\Backspace #\Tab

'(a b . (c d)) 相当于 '(a b c d)

vector, array, list

一维array称为vector

vector 存取 是O(1)量级,而 list 存取是 O(n)量级

往list头部加个元素是O(1)量级,往array头部加个元素是O(n)量级

一个vector例子 #(a b c)

bit-vector  #*10110

结构体

#S(structure-name 
        slot-name-1 slot-value-1 
        slot-name-2 slot-value-2 
                      ...)
 
 
mapcar 例子
 
> (mapcar #'(lambda (x y) (sqrt (* x y))) '(1 3) '(4 3))
(2 3)
 
chapter 3 - chapter 7


类型转换

(coerce '(a b c) 'vector) => #(a b c)

(coerce "a" 'character) => #\a
(coerce '(a b c) 'vector) => #(a b c) 
 
 
defvar 与 defparameter 的差别的是,
 
在运行期间,如果多次load file,defparameter定义的值会重新初始化,defvar则不会

批量赋值
 
(setf place1 newvalue1 
      place2 newvalue2) 
      ... 
      placen newvaluen) 
let 与 let* 的差别,非常清晰:http://www.lispworks.com/documentation/HyperSpec/Body/s_let_l.htm
 
注意,let是并行执行,而let*是串行执行 
 
从某个块返回
(block loser 
   (catch 'stuff 
      (mapcar #'(lambda (x) (if (numberp x) 
                                (hairyfun x) 
                                (return-from loser nil))) 
              items))) 
 
dolist, dotimes, do 的用法:successful lisp - Chapter 5 - Introducing Iteration
 
mapc、mapl 类似于 mapcar、maplist,但是返回的是后面的首个参数
mapcan、mapcon 类似于 mapcar、maplist,但是会把结果flatten
 
具体可以看 successful lisp - Chapter 12 - Mapping Instead of Iteration
 
多个返回值
(defun polar (x y) 
  (values (sqrt (+ (* x x) (* y y))) (atan y x))) 

(multiple-value-bind (r theta) (polar 3.0 4.0) 
  (vector r theta)) 
   => #(5.0 0.9272952) 
(nth-value 1 (floor number divisor))


 

没有评论:

发表评论