Re: Why is this macro misbehaving?



"jmckitrick" <j_mckitrick@xxxxxxxxx> writes:

I'd like to take a string like this: "1/2/3"
and bind it like this:

(let ((item "1") (arg1 "2") (arg2 "3"))
body)

Here's the macro:

(defmacro with-url ((&rest vars) seq &body body)
(let ((subseqs (split-sequence #\/ seq :remove-empty-subseqs t)))
`(let ,(loop
for var in vars
for subseq in subseqs collect
`(,var ,subseq))
,@body)))

The splitting must be done at runtime, not macroexpansion time.

(with-url (item arg1 arg2) (request-unhandled-part request)
(format t "arg1: ~A~%" arg1))

In this call, your macro gets the list (REQUEST-UNHANDLED-PART
REQUEST) as the SEQ argument. Trying to use split-sequence on it will
not work.

You need to move the splitting work into runtime; that's when the
value (rather than the form that produces the value) is available.

At that point, though, there's very little to be gained over simply
using DESTRUCTURING-BIND.

Zach
.



Relevant Pages

  • Why is this macro misbehaving?
    ... (defmacro with-url ((&rest vars) ... for subseq in subseqs collect ... using the macro declares all 'args' after 'item' to be ...
    (comp.lang.lisp)
  • Re: defmacro
    ... (defmacro doloop (vars &body body) ... To write a macro, ...
    (comp.lang.lisp)
  • 2 macros and passing gensyms around
    ... when (listp var-and-column-name) ... (defmacro bind-vars ((&rest vars) ... data &body body) ... The name would be created in the with-bindable-columns macro and used in the ...
    (comp.lang.lisp)
  • Newbie doubt with macros ` , ,@
    ... It is a newbie question about how parameters and vars are treated ... It is not possible to write commas inside a comma, ... I want to make the first, i.e., run the the inner macro before ... nil) ). ...
    (comp.lang.lisp)
  • Re: Newbie doubt with macros ` , ,@
    ... It is not possible to write commas inside a comma, ... macro and what should be a function. ... (defmacro let-samevars (definition &rest vars) ...
    (comp.lang.lisp)

Loading