Starting out with Lisp - stuck on a macro
Posted: Mon Jul 21, 2014 12:20 pm
I was following through Practical Common LISP and then at some point I tried to play around a bit, but I seem to be getting stuck. I've been playing around for a bit with the chapter "Making a database" and was trying to convert the "update" mechanism to use a snazzy macro the way the book teaches you to do with "where".
I think I got most of it to work, but now I'm really confused about the last step, because it's suddenly trying to call keyword arguments as a function (which doesn't work)
This is a piece of the book which explains the workings of the "where", if that's helpful: http://www.gigamonkeys.com/book/practic ... abase.html
And this is what I have now:
And now I'm trying to call it like this;
But then I get the message:
My basic idea was that update-row would pop out a lambda function that would (through make-modify-list) build a function that would setf whichever fields you'd pass to updatem. But instead it seems to be trying to call the keyworded field names themselves as functions somewhere? I just can't really figure out where and I'm not seeing a backtrace anymore (not that I could really read those anyway).
I'm hoping anyone could explain to me what is going wrong here. This LISP stuff sounds amazing, but I'm having some trouble trying to understand what is actually happening in some of these functions.
I think I got most of it to work, but now I'm really confused about the last step, because it's suddenly trying to call keyword arguments as a function (which doesn't work)
This is a piece of the book which explains the workings of the "where", if that's helpful: http://www.gigamonkeys.com/book/practic ... abase.html
And this is what I have now:
Code: Select all
(defun make-modify-expr (field value)
`(setf (getf row ,field) ,value))
(defun make-modify-list (fields)
(loop while fields
collecting (make-modify-expr (pop fields) (pop fields))))
(defmacro update-row (selector-fn &rest clauses)
`#'(lambda (row) (when (funcall ,selector-fn row)
(and ,@(make-modify-list clauses)))))
(defmacro updatem (selector-fn &rest clauses)
`(mapcar
(update-row ,selector-fn ,clauses)
*db* ))
Code: Select all
(updatem (where :rating 6) :rating 7)
Code: Select all
;Compiler warnings :
; In an anonymous lambda form: Undefined function :RATING
I'm hoping anyone could explain to me what is going wrong here. This LISP stuff sounds amazing, but I'm having some trouble trying to understand what is actually happening in some of these functions.