Patrick (patrickwonders) wrote in lisp,

Lisp Troubles: Fabricating a Closure...

The short version of my problem is that I want to do this:

(defun generate-setter (buffer pre post)
  (eval `(lambda (index value)
	   (setf (aref buffer ,@pre index ,@post) value))))

Except that I want the (lambda ...) to be a closure around buffer. Here, pre and post are lists generated at runtime (thus not available as lists at compile time) and hence the (defun ...) and (eval ...) instead of (defmacro ...). Alas, the (eval ...) form uses the null lexical environment, so I cannot capture buffer.

The long form with some of the myriad failed attempts I have made is on this blog post. Please, anyone have half a cup of clue that I could borrow? I promise, I'm going to the store for more clues really soon. I'll pay you back.

Edit: Here is exactly what I was looking for... (courtesy of tfb)

(defun generate-setter (buffer pre post)
  (let ((make-closure
           (compile nil `(lambda (buf)
                           (lambda (index value)
                             (setf (aref buf ,@pre index ,@post) value))))))
    (compile nil (funcall make-closure buffer))))
  • Post a new comment


    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.