#!/usr/local/bin/sbcl --script (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*)) (add-record (make-cd "Roses" "Kathy Mattea" 7 t)) (add-record (make-cd "Fly" "Dixie Chicks" 8 t)) (add-record (make-cd "Home" "Dixie Chicks" 9 t)) (defun dump-db () (dolist (cd *db*) (format t "~{~a:~10t~a~%~}~%" cd))) ;(dump-db) ;select (format t "~a~%" "select") (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))) (format t "~a~%" (select (artist-selector "Dixie Chicks"))) ;defmacro (format t "~a~%" '(1 2 3)) (format t "~a~%" (reverse '(1 2 3))) (format t "~a~%" "hello, world") (defmacro backwards (expr) (reverse expr)) (backwards ("hello, world" "~a~%" t format)) (defun make-comparison-expr (field value) (list 'equal (list 'getf 'cd field) value)) ;(make-comparison-expr :rating 10) ;(make-comparison-expr :title "Give Us a Break") (format t "~a~%" `(1 2 3)) (format t "~a~%" '(1 2 3)) (format t "~a~%" `(1 2 (+ 1 2))) (format t "~a~%" `(1 2 ,(+ 1 2))) (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)))) `(and ,(list 1 2 3)) `(and ,@(list 1 2 3)) `(and ,@(list 1 2 3) 4) (macroexpand-1 '(where :title "Fly" :ripped t)) ;(dump-db) (format t "~a~%" (select(where :title "Fly" :ripped t)))