Writing a macro for docstring for lambda

Discussion of Common Lisp

Writing a macro for docstring for lambda

Postby npolyspace » Sat Nov 12, 2011 8:35 am

I am trying to debug some code I wrote, and I realized that what I need to do is replace many of my lambda functions with lambda functions that have doc strings attached.
But these docs strings need to be generated on the fly. But the docstring that lisp accepts must be a string, not something that evaluates to a string.

What I have come up with so far is:

Code: Select all
(defmacro doclambda (args dexpr &body body)
  (let ((multiquotes (mapcar (lambda(k)(list 'quote k)) body)))
  `(eval `(lambda ,(quote ,args) ,,dexpr ,,@multiquotes))))


which doesn't quite work. The generated lambda doesn't have the right environment. So this seems to work:

Code: Select all
(setf dlam (let* ((v 5) (vstr (write-to-string v)))
(doclambda(x) (concatenate 'string "doc" vstr) (declare (type integer x)) (+ x 2))))
(documentation dlam 'function)
(funcall dlam 3)


But this doesn't:
Code: Select all
(setf dlam (let* ((v 5) (vstr (write-to-string v)))
(doclambda(x) (concatenate 'string "doc" vstr) (declare (type integer x)) (+ x v 2))))
(documentation dlam 'function)
(funcall dlam 3)


I assume that the problem is my use of eval. How can this be rewritten to not use eval?

Richard
npolyspace
 
Posts: 7
Joined: Wed Mar 17, 2010 6:03 pm

Re: Writing a macro for docstring for lambda

Postby platypus » Sat Nov 12, 2011 1:57 pm

The piece of information you need is that documentation is setfable.

Code: Select all
(let ((f (lambda (x) (* 2 x))))
  (setf (documentation f 'function) "double a number")
  (print (documentation f 'function)))


Armed with this info, writing the macro is easy,

Code: Select all
(defmacro doclambda (args doc &body body)
  (let ((f (gensym)))
    `(let ((,f (lambda ,args ,@body)))
       (setf (documentation ,f 'function) ,doc)
       ,f)))
platypus
 
Posts: 1
Joined: Sat Nov 12, 2011 1:49 pm


Return to Common Lisp

Who is online

Users browsing this forum: MSN [Bot] and 3 guests

cron