実践Common Lispを読んでみる2

実践Common Lisp

実践Common Lisp

今日は32ページの途中まで。マクロが出てくる手前になる。

  • mapcarすごい。
  • lambda出てきた。

みたいな感じ。
以下、ソースコード

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

http://dl.getdropbox.com/u/228440/veleno-samples/lisp-samples/sample.lisp