Lisp概述
C-x
3 水平切分窗口
C-x C-f 输入文件名,创建或打开文件
C-c C-c编译Lisp原码
C-c C-z切换至*REPL*
C-c b 切换至*REPL*
C-x o 切换至其他窗口
输入逗号后,再输入quit可以退出Lisp环境
在函数调用出错时,会进入调试窗口,点击q会退出调试器,再次进入*REPL*
(load "hello.lisp")或者(load (compile-file "hello.lisp"))或者C-c C-l加载
(list 1 2 3) 使用list函数生成列表
(list :a 1 :b 2 :c 3)属性表 (getf (list :a 1 :b 2 :c 3) :a)获取属性表a的值
(defun make-cd(title artist rating ripped) (list :title title :artist artist :rating rating :ripped ripped))定义函数
(defvar *db* nil) 定义全局变量
(defun add-record(cd) (push cd *db*)) 使用push为*db*添加新项
(add-record(make-cd "Rose" "Kathy Mattea" 7 t)) 在数据库中添加歌曲
*db*显示数据库内容
(defun dump-db() (dolist(cd *db*) (format t "~{~a: ~10t~a~%~}~%" cd))) 使用dolist循环*db*所有元素,美化输出一个参数循环项内容换行输出下一项对齐到第10列 该函数等价于(defun dump-db() (format t "~{~{~a: ~10t~a~%~}~%~}" *db*))
(defun prompt-read(prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (read-line *query-io*))
(defun prompt-for-cd() (make-cd (prompt-read "Title") (prompt-read "Artist") (or(parse-integer(prompt-read "Rating") :junk-allowed t) 0) (y-or-n-p prompt-read "Ripped [y/n]: ")))
(defun add-cds() (loop (add-record(prompt-for-cd)) (if (not (y-or-n-p "Another? [y/n]: ")) (return)))) 批量输入歌曲数据
(defun save-db(filename) (with-open-file(out filename :direction :output :if-exists :supersede) (with-standard-io-syntax(print *db* out))))
(defun load-db(filename) (with-open-file(in filename) (with-standard-io-syntax(setf *db* (read in)))))保存文件与读取文件
(remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10)) 把evenp当成函数看
(remove-if-not #'(lambda (x) (=0 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)) 把lambda当成匿名函数的定义方式
(remove-if-not #'(lambda (cd) (equal (getf cd :artist) "Dixie Chicks")) *db*)
(defun select-by-artist(artist) (remove-if-not #'(lambda (cd) (equal (getf cd :artist) artist)) *db*))
(defun select(selector-fn) (remove-if-not selector-fn *db))
(defun artist-selector(artist) #'(lambda (cd) (equal (getf cd :artist) artist)))
(select (artist-selector "Dixie Chicks"))
(defun foo (a b c) (list a b c))
(defun foo (&key a b c) (list a b c))
(defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p))
(defun where (&key title artist rating (rapped nil ripped-p)) #'(lambda (cd) (and (if title (equal (getf cd :title) title) t) \
(if artist (equal getf(cd :artist) artist) t) (if rating (equal (getf cd :rating) rating) t) if(ripped-p (equal (getf cd :ripped) ripped) t))))
(select (where :rating 10 :ripped nil))
(defun update(selector-fn &key title artist rating (ripped nil ripped-p)) (setf *db* (mapcar #'(lambda(row) \
(when (funcall selector-fn row) (if title (setf (getf row :title) title)) (if artist (setf(getf row :artist) artist))\
(if rating (setf (getf row :rating) rating)) (if ripped-p (setf (getf row :ripped) ripped))) row) *db*)))
(update (where :artist "Dixie Chicks") :rating 11)
(defun delete-rows (selector-fn) (setf *db* (remove-if selector-fn *db*)))
(defmacro backwards (expr) (reverse expr))
(backwards ("hello,world" t format)) 传给reverse函数后求值再替换
’(1 2 (+ 1 2)) ---> (1 2 (+ 1 2)) 反引号保持原样
’(1 2 ,(+ 1 2)) ---> (1 2 3) 反引号内逗号求值
’(and ,(list 1 2 3))--->(and (1 2 3))
’(and ,@(list 1 2 3))--->(and 1 2 3)
’(and ,@(list 1 2 3) 4)--->(and 1 2 3 4)
(defun make-comparison-expr (field value) (list equal (list getf cd field) value))
(defun make-comparison-expr (field value) (list ’equal (list ’getf ’cd field) value))
(defun make-comparison-expr (field value) ’(equal (getf cd ,field) ,value))
(defun make-comparisons-list (fields) (loop while fields collecting (make-comparison-expr (pop fields) (pop fields))))
(defmacro where (&rest clauses) ’#'(lambda (cd) (and ,@(make-comparisons-list clauses))))
(macroexpand-1 '(where :title "Give Us a Break" :ripped t)) 返回宏调用的展开式
#'(lambda (cd) (and (equal (getf cd :title) "Give Us a Break") (equal (getf cd :ripped) t)))
C-x C-f 输入文件名,创建或打开文件
C-c C-c编译Lisp原码
C-c C-z切换至*REPL*
C-c b 切换至*REPL*
C-x o 切换至其他窗口
输入逗号后,再输入quit可以退出Lisp环境
在函数调用出错时,会进入调试窗口,点击q会退出调试器,再次进入*REPL*
(load "hello.lisp")或者(load (compile-file "hello.lisp"))或者C-c C-l加载
(list 1 2 3) 使用list函数生成列表
(list :a 1 :b 2 :c 3)属性表 (getf (list :a 1 :b 2 :c 3) :a)获取属性表a的值
(defun make-cd(title artist rating ripped) (list :title title :artist artist :rating rating :ripped ripped))定义函数
(defvar *db* nil) 定义全局变量
(defun add-record(cd) (push cd *db*)) 使用push为*db*添加新项
(add-record(make-cd "Rose" "Kathy Mattea" 7 t)) 在数据库中添加歌曲
*db*显示数据库内容
(defun dump-db() (dolist(cd *db*) (format t "~{~a: ~10t~a~%~}~%" cd))) 使用dolist循环*db*所有元素,美化输出一个参数循环项内容换行输出下一项对齐到第10列 该函数等价于(defun dump-db() (format t "~{~{~a: ~10t~a~%~}~%~}" *db*))
(defun prompt-read(prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (read-line *query-io*))
(defun prompt-for-cd() (make-cd (prompt-read "Title") (prompt-read "Artist") (or(parse-integer(prompt-read "Rating") :junk-allowed t) 0) (y-or-n-p prompt-read "Ripped [y/n]: ")))
(defun add-cds() (loop (add-record(prompt-for-cd)) (if (not (y-or-n-p "Another? [y/n]: ")) (return)))) 批量输入歌曲数据
(defun save-db(filename) (with-open-file(out filename :direction :output :if-exists :supersede) (with-standard-io-syntax(print *db* out))))
(defun load-db(filename) (with-open-file(in filename) (with-standard-io-syntax(setf *db* (read in)))))保存文件与读取文件
(remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10)) 把evenp当成函数看
(remove-if-not #'(lambda (x) (=0 (mod x 2))) '(1 2 3 4 5 6 7 8 9 10)) 把lambda当成匿名函数的定义方式
(remove-if-not #'(lambda (cd) (equal (getf cd :artist) "Dixie Chicks")) *db*)
(defun select-by-artist(artist) (remove-if-not #'(lambda (cd) (equal (getf cd :artist) artist)) *db*))
(defun select(selector-fn) (remove-if-not selector-fn *db))
(defun artist-selector(artist) #'(lambda (cd) (equal (getf cd :artist) artist)))
(select (artist-selector "Dixie Chicks"))
(defun foo (a b c) (list a b c))
(defun foo (&key a b c) (list a b c))
(defun foo (&key a (b 20) (c 30 c-p)) (list a b c c-p))
(defun where (&key title artist rating (rapped nil ripped-p)) #'(lambda (cd) (and (if title (equal (getf cd :title) title) t) \
(if artist (equal getf(cd :artist) artist) t) (if rating (equal (getf cd :rating) rating) t) if(ripped-p (equal (getf cd :ripped) ripped) t))))
(select (where :rating 10 :ripped nil))
(defun update(selector-fn &key title artist rating (ripped nil ripped-p)) (setf *db* (mapcar #'(lambda(row) \
(when (funcall selector-fn row) (if title (setf (getf row :title) title)) (if artist (setf(getf row :artist) artist))\
(if rating (setf (getf row :rating) rating)) (if ripped-p (setf (getf row :ripped) ripped))) row) *db*)))
(update (where :artist "Dixie Chicks") :rating 11)
(defun delete-rows (selector-fn) (setf *db* (remove-if selector-fn *db*)))
(defmacro backwards (expr) (reverse expr))
(backwards ("hello,world" t format)) 传给reverse函数后求值再替换
’(1 2 (+ 1 2)) ---> (1 2 (+ 1 2)) 反引号保持原样
’(1 2 ,(+ 1 2)) ---> (1 2 3) 反引号内逗号求值
’(and ,(list 1 2 3))--->(and (1 2 3))
’(and ,@(list 1 2 3))--->(and 1 2 3)
’(and ,@(list 1 2 3) 4)--->(and 1 2 3 4)
(defun make-comparison-expr (field value) (list equal (list getf cd field) value))
(defun make-comparison-expr (field value) (list ’equal (list ’getf ’cd field) value))
(defun make-comparison-expr (field value) ’(equal (getf cd ,field) ,value))
(defun make-comparisons-list (fields) (loop while fields collecting (make-comparison-expr (pop fields) (pop fields))))
(defmacro where (&rest clauses) ’#'(lambda (cd) (and ,@(make-comparisons-list clauses))))
(macroexpand-1 '(where :title "Give Us a Break" :ripped t)) 返回宏调用的展开式
#'(lambda (cd) (and (equal (getf cd :title) "Give Us a Break") (equal (getf cd :ripped) t)))

浙公网安备 33010602011771号