2

Let's say I have a let form and I want to generate the varlist dynamically from another function. Something like this:

(let ((x 1)
      (y 2)
      (the-dyn-gen))
  body...
  )

I have some local vars already in the varlist namely x and y. But I want to call a function that I called the-dyn-gen which returns a list like this '((a 3) (b 4)) now I need to make these sub lists (a 3) & (b 3) part of the varlist. Is this possible?

5

Looks like a macro could do that:

(defmacro mylet (defs fun body)
  (list 'let (append defs (funcall fun)) body))

;; example uses
(defun the-dyn-gen () 
  '((a 3) (b 3)))

(mylet ((x 1) (y 2)) the-dyn-gen (+ a b x y))

(mylet ((x 1)) the-dyn-gen (+ a b x))

(mylet nil the-dyn-gen (+ a b))

Disclaimer: while this seems to work, macros can be tricky.

Note that macroexpand is a nice function to test a macro. Here

(macroexpand '(mylet ((x 1) (y 2)) the-dyn-gen (+ a b x y)))

results in

(let ((x 1) (y 2) (a 3) (b 3)) (+ a b x y))

Note: related question with progv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.