Common Lisp 笔记:
由于lisp看起来有点支离破碎的,所以笔记也很乱...
1.hello world:
(print "hello world")
只需存为文本文件在终端使用clisp hello就行了...
2.打印1+1...
(print (+ 1 1))
3.将11左移一位
(print (ash 11 1))
4.将11右移一位
(print (ash 11 -1))
5.计算2的100次方
(print (expt 2 100))
6.打印一句话:
(print "He yelled \"Stop that thief!\" from the busy street.")
7.输出5个列表:
(print (cons 'chicken 'cat))
(print (cons 'chicken 'nil))
(print (cons 'chicken ()))
(print (cons 'pork '(beef chicken)))
(print (list 'a 'b 'c))
其中cons(list)表示建立列表函数 () 或者 'nil代表空
8.输出列表首个元素:
(print (car '(pork beef chicken)))
(print (car '(beef chicken)))
car表示提取列表首个元素...
9.输出列表其他元素(除了首个):
(print (cdr '(abc balabala beef chicken)))
10.列表中的列表:
(print '(cat (duck bat) ant))
11.这个有点意思,有点分配律的味道...
(print (cdr (car '((peas carrots tomatoes) (pork beef chicken)))))
(print (cdar '((peas carrots tomatoes) (pork beef chicken))))
大概是d或a都拉上一个c和r就行了...
12.复杂点的组合:
(print(cadadr '((peas carrots tomatoes) (pork beef chicken) duck)))
(print (car (cdr (car (cdr'((peas carrots tomatoes) (pork beef chicken) duck))))))
想是好想,就是不好还原回来... 组合最多只能是4层,5层以上就不行了...
13.if语句:
(print (if '(true)
'i-am-true
'i-am-false))
(print (if '()
'i-am-true
'i-am-false))
列表为空时为假...
14.定义并使用求长度的递归函数(好看吗?):
(defun my-length(list)
(if list
(1+ (my-length (cdr list)))
0))
(print (my-length '(1 2 3 )))
15.比较空值:
(print (eq '() nil))
16.像样点的if:
(print (if (= (+ 1 2) 3)
'yup
'nope))
(print (if (= (+ 1 2 ) 4)
'yup
'nope))
比较1+2和3是否相等...
17.判断奇数:
(print (if(oddp 5)
'odd-number
'even-number
))
oddp 是判断奇数操作符
18.使用变量:
(defvar *number* nil)
(print *number*)
(if (oddp 5)
(progn (setf *number* t)
'odd-number)
'even-number)
(print *number*)
defvar 定义变量number
progn 用于获取最后一个表达式的返回值
setf 用于设置变量的值
19.when语句:
(defvar *number* nil)
(print *number*)
(when (oddp 5)
(setf *number* t)
'odd-number)
(print *number*)
当条件成立时...
20.unless语句
(defvar *number* nil)
(print *number*)
(unless (oddp 4)
(setf *number* nil)
'even-number)
(print *number*)
21.if else语句:
(defvar *who* nil)
(defun whatshouldsay(person)
(cond ((eq person 'alice) (setf *who* 'sweethert)
'(Love you))
((eq person 'bob) (setf *who* 'friend)
'(Welcome))
(t (setf *who* 'enemy)
'(I hate you!))))
(print (whatshouldsay 'alice))
(print *who*)
(print (whatshouldsay '()))
(print *who*)
(print (whatshouldsay 'BOB))
(print *who*)
定义变量who和函数whatshouldsay,cond是条件语句,
最后一个t表示默认时候总会是enemy
22.使用case语句重写:
(defvar *who* nil)
(defun whatshouldsay(person)
(case person
((alice) (setf *who* 'sweethert)
'(Love you))
((bob) (setf *who* 'friend)
'(Welcome))
(t (setf *who* 'enemy)
'(I hate you!))))
(print (whatshouldsay 'alice))
(print *who*)
(print (whatshouldsay 'bad))
(print *who*)
(print (whatshouldsay 'BOB))
(print *who*)
23.逻辑与(或)运算符,有短路特性:
(print (and (oddp 5) (oddp 7) (oddp 9)))
(print (or (oddp 5) (oddp 2) (oddp (expt 2 1))))
(print (or (oddp 4) (oddp 2) (oddp (expt 2 1))))
24.python中的in符号:
(print (if (member 1 '(3 4 1 5))
'one-is-in-list
'one-is-not-in-list ))
member相当于in
但是:
(print (member 1 '(2 3 1 6 8)))
会返回(1 6 8)
因为'(2 3 1 6 8)
在构造时:
使用(cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))
所以解析时返回最初发现的序列...
25.find-if语句:
(print (find-if #'oddp '(2 4 5 6)))
发现就返回值
26.比较运算符:
eq用于比较符号
equal用于比较常量
note:
eq can also be used to compare conses (the links created by the cons command). How-
ever, it returns true values only when a cons is compared directly to itself, created by the
same cons call. This means, two unrelated conses that “look” exactly the same can fail
an eq test. Since eq can check a cons cell only against itself, using eq with conses isn’t
really that useful for a beginner. However, an advanced Lisper may want to compare
conses with eq under certain circumstances.
如:
(print (eq '(2 3 1 6 8) (cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))))
返回nil
而
(print (equal '(2 3 1 6 8) (cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))))
返回T
eql和eq类似,但是还能比较字符和数字
equalp可以忽略整型实型大小写比较,如:
(print (equalp "Bob" "bob"))
(print (equalp "Bob" "bob"))
(print (equalp 0.0 0))
都返回T
27.mapcar,类似python的map
The mapcar function is used frequently by Lispers. This function takes
another function and a list, and then applies this function to every member
of a list.
(print (mapcar #'sqrt '(1 2 3 4 5)))
(print (mapcar #'oppd '(1 2 3 4 5)))
(print (mapcar #'car '((foo bar) (baz qux))))
28.append函数,类似python的append:
(print (append '(I have) '(a ) '(book)))
29.apply函数,类似python的apply:
(print (apply #'append '((she has) (a) (book))) )
(print (apply #'append '((this is a list) (this is another list))))
30.push函数:
(defparameter *foo* '(1 2 3))
(push 8 *foo*)
定义一个list,将8加入list
31.prin1函数(不会换行):
(prin1 "hello world!")
32.let定义全局变量:
(let ((a 5)
(b 6)
(+ a b))
33.输入函数read:
(defun hello()
(print "Please input your name:")
(let ((name (read)))
(print "Hello")
(print name)))
(hello)
read以后保存在name中
(defun say-hello()
(princ "Please input your name:")
(let ((name (read-line)))
(princ "Hi,")
(princ name)
)
)
(say-hello)
34.代码模式和数据模式:
'(+ 1 2) 是数据模式,仅表示字串
(+ 1 2) 是代码模式
(defparameter *f* '(+ 1 2))
(eval *f*)
可以求出具体数值
35.匿名函数:
(lambda (n) (/ n/2))
由于lisp看起来有点支离破碎的,所以笔记也很乱...
1.hello world:
(print "hello world")
只需存为文本文件在终端使用clisp hello就行了...
2.打印1+1...
(print (+ 1 1))
3.将11左移一位
(print (ash 11 1))
4.将11右移一位
(print (ash 11 -1))
5.计算2的100次方
(print (expt 2 100))
6.打印一句话:
(print "He yelled \"Stop that thief!\" from the busy street.")
7.输出5个列表:
(print (cons 'chicken 'cat))
(print (cons 'chicken 'nil))
(print (cons 'chicken ()))
(print (cons 'pork '(beef chicken)))
(print (list 'a 'b 'c))
其中cons(list)表示建立列表函数 () 或者 'nil代表空
8.输出列表首个元素:
(print (car '(pork beef chicken)))
(print (car '(beef chicken)))
car表示提取列表首个元素...
9.输出列表其他元素(除了首个):
(print (cdr '(abc balabala beef chicken)))
10.列表中的列表:
(print '(cat (duck bat) ant))
11.这个有点意思,有点分配律的味道...
(print (cdr (car '((peas carrots tomatoes) (pork beef chicken)))))
(print (cdar '((peas carrots tomatoes) (pork beef chicken))))
大概是d或a都拉上一个c和r就行了...
12.复杂点的组合:
(print(cadadr '((peas carrots tomatoes) (pork beef chicken) duck)))
(print (car (cdr (car (cdr'((peas carrots tomatoes) (pork beef chicken) duck))))))
想是好想,就是不好还原回来... 组合最多只能是4层,5层以上就不行了...
13.if语句:
(print (if '(true)
'i-am-true
'i-am-false))
(print (if '()
'i-am-true
'i-am-false))
列表为空时为假...
14.定义并使用求长度的递归函数(好看吗?):
(defun my-length(list)
(if list
(1+ (my-length (cdr list)))
0))
(print (my-length '(1 2 3 )))
15.比较空值:
(print (eq '() nil))
16.像样点的if:
(print (if (= (+ 1 2) 3)
'yup
'nope))
(print (if (= (+ 1 2 ) 4)
'yup
'nope))
比较1+2和3是否相等...
17.判断奇数:
(print (if(oddp 5)
'odd-number
'even-number
))
oddp 是判断奇数操作符
18.使用变量:
(defvar *number* nil)
(print *number*)
(if (oddp 5)
(progn (setf *number* t)
'odd-number)
'even-number)
(print *number*)
defvar 定义变量number
progn 用于获取最后一个表达式的返回值
setf 用于设置变量的值
19.when语句:
(defvar *number* nil)
(print *number*)
(when (oddp 5)
(setf *number* t)
'odd-number)
(print *number*)
当条件成立时...
20.unless语句
(defvar *number* nil)
(print *number*)
(unless (oddp 4)
(setf *number* nil)
'even-number)
(print *number*)
21.if else语句:
(defvar *who* nil)
(defun whatshouldsay(person)
(cond ((eq person 'alice) (setf *who* 'sweethert)
'(Love you))
((eq person 'bob) (setf *who* 'friend)
'(Welcome))
(t (setf *who* 'enemy)
'(I hate you!))))
(print (whatshouldsay 'alice))
(print *who*)
(print (whatshouldsay '()))
(print *who*)
(print (whatshouldsay 'BOB))
(print *who*)
定义变量who和函数whatshouldsay,cond是条件语句,
最后一个t表示默认时候总会是enemy
22.使用case语句重写:
(defvar *who* nil)
(defun whatshouldsay(person)
(case person
((alice) (setf *who* 'sweethert)
'(Love you))
((bob) (setf *who* 'friend)
'(Welcome))
(t (setf *who* 'enemy)
'(I hate you!))))
(print (whatshouldsay 'alice))
(print *who*)
(print (whatshouldsay 'bad))
(print *who*)
(print (whatshouldsay 'BOB))
(print *who*)
23.逻辑与(或)运算符,有短路特性:
(print (and (oddp 5) (oddp 7) (oddp 9)))
(print (or (oddp 5) (oddp 2) (oddp (expt 2 1))))
(print (or (oddp 4) (oddp 2) (oddp (expt 2 1))))
24.python中的in符号:
(print (if (member 1 '(3 4 1 5))
'one-is-in-list
'one-is-not-in-list ))
member相当于in
但是:
(print (member 1 '(2 3 1 6 8)))
会返回(1 6 8)
因为'(2 3 1 6 8)
在构造时:
使用(cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))
所以解析时返回最初发现的序列...
25.find-if语句:
(print (find-if #'oddp '(2 4 5 6)))
发现就返回值
26.比较运算符:
eq用于比较符号
equal用于比较常量
note:
eq can also be used to compare conses (the links created by the cons command). How-
ever, it returns true values only when a cons is compared directly to itself, created by the
same cons call. This means, two unrelated conses that “look” exactly the same can fail
an eq test. Since eq can check a cons cell only against itself, using eq with conses isn’t
really that useful for a beginner. However, an advanced Lisper may want to compare
conses with eq under certain circumstances.
如:
(print (eq '(2 3 1 6 8) (cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))))
返回nil
而
(print (equal '(2 3 1 6 8) (cons 2 (cons 3 (cons 1 (cons 6 (cons 8 nil)))))))
返回T
eql和eq类似,但是还能比较字符和数字
equalp可以忽略整型实型大小写比较,如:
(print (equalp "Bob" "bob"))
(print (equalp "Bob" "bob"))
(print (equalp 0.0 0))
都返回T
27.mapcar,类似python的map
The mapcar function is used frequently by Lispers. This function takes
another function and a list, and then applies this function to every member
of a list.
(print (mapcar #'sqrt '(1 2 3 4 5)))
(print (mapcar #'oppd '(1 2 3 4 5)))
(print (mapcar #'car '((foo bar) (baz qux))))
28.append函数,类似python的append:
(print (append '(I have) '(a ) '(book)))
29.apply函数,类似python的apply:
(print (apply #'append '((she has) (a) (book))) )
(print (apply #'append '((this is a list) (this is another list))))
30.push函数:
(defparameter *foo* '(1 2 3))
(push 8 *foo*)
定义一个list,将8加入list
31.prin1函数(不会换行):
(prin1 "hello world!")
32.let定义全局变量:
(let ((a 5)
(b 6)
(+ a b))
33.输入函数read:
(defun hello()
(print "Please input your name:")
(let ((name (read)))
(print "Hello")
(print name)))
(hello)
read以后保存在name中
(defun say-hello()
(princ "Please input your name:")
(let ((name (read-line)))
(princ "Hi,")
(princ name)
)
)
(say-hello)
34.代码模式和数据模式:
'(+ 1 2) 是数据模式,仅表示字串
(+ 1 2) 是代码模式
(defparameter *f* '(+ 1 2))
(eval *f*)
可以求出具体数值
35.匿名函数:
(lambda (n) (/ n/2))