Lisp学习1

--------------------2015-3-10--------------------

笔记:

Lisp定义函数形式:(defun name varlist &rest body)

变量名字中带星号(*)是全局变量

Emacs快捷键:
    C-x C-f 创建一个新文件
    C-c C-q 匹配当前的所有开括号
    C-c C-c 编译LISP
    C-c C-z | C-x b 切换到REPL
    C-x C-s 保存
    在REPL中按,并输入quit | sayoonara 回车,这将退出Lisp并且关闭所有SLIME创建的缓冲区,包括REPL缓冲区。


练习:

(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*))

(defun dump-db ()
	(dolist (cd *db*)
		(format t "~{~a: ~10t~a~%~}~%" cd)))

(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 "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)))))

(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)))

(defun where (&key title artist rating (ripped 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))))

(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*)))

(defun delete-rows (selector-fn)
	(setf *db* (remove-if selector-fn *db*)))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值