I am working on a complicated macro and have run into a roadblock.
(defmacro for-each-hashtable-band (body vars on &optional counter name)
`(block o
(with-hash-table-iterator (next-entry ,on)
(destructuring-bind
,(apply #'append vars)
(let ((current-band (list ,@(mapcar #'not (apply #'append vars)))))
(for (i 1 ,(length (apply #'append vars)) 2)
(multiple-value-bind
(succ k v) (next-entry)
(if succ
(progn
(setf (nth i current-band) k)
(setf (nth (+ 1 i) current-band) v))
(return-from o nil))))
current-band)
,@body))))
im getting "Evaluation aborted on #<UNDEFINED-FUNCTION NEXT-ENTRY {100229C693}>" i dont understand why next-entry appears to be invisible to the macro i have created.
I've tried stripping down this to a small replicable example but i couldnt find a minimal scenario without the macro i created where next-entry would be invisible besides this scenario no matter what I tried, i've always managed to find a way to call next-entry in my other examples so im stumped as to why i cannot get it working here
I've tested the for macro ive created and it seems to generally work in most cases but for some reason it cannot see this next-entry variable. How do i make it visible?
In your code there are multiple places where the macro generates bindings in a way that is subject to variable capture (pdf).
A simplified example of such a capture is:
Here above, the
xvariable is introduced, but if the code is expanded in a context wherexis already bound, thenbodywill not be able to reference that formerxbinding and will always seexbound to zero (you generally don't want this behavior).Write a function instead
Instead of writing a big macro for this, let's first try understanding what you want to achieve and write instead a higher-order function, ie. a function that calls user-provided functions.
If I understand correctly, your function iterates over a hash-table by bands of entries. I assume
varsholds a list of(key value)pairs of symbols, for example((k1 v1) (k2 v2)). Then,bodyworks on all the key/value pairs in the band.In the following code, the function
map-each-hashtable-bandaccepts a function, a hash-table, and instead ofvarsit accepts a size, the width of the band (the number of pairs).Notice how in your code, you only have one loop, which builds a band using the hash-table iterator. But then, since the macro is named
for-each-hashtable-band, I assume you also want to loop over all the bands. The macrowith-hash-table-iteratorprovides an iterator but does not loop itself. That's why here I have two loops.For example:
NB. Iterating over a hash-table happens in an arbitrary order, there is no guarantee that you'll see the entries in any particular kind of order, this is implementation-dependant.
With my current version of SBCL this prints the following:
Wrap the function in a macro
The previous function might not be exactly the behavior you want, so you need to adapt to your needs, but once it does what you want, you can wrap a macro around it.
For example:
This prints:
Macro-only solution, for completeness
If you want to have only one, single macro, you can start by inlining the body of the above function in the macro, you don't need to use
applyanymore, but instead you need to establish bindings around the body, usingdestructuring-bindas you did. A first draft would be to simply as follows, but notice that this is not a proper solution:In order to be free of variable capture problems with macros, each temporary variable you introduce must be named after a symbol that cannot exist in any context you expand your code. So instead we first unquote all the variables, making the macro definition fail to compile:
When compiling the macro, the macro is supposed to inject symbols into the code, but here we have a compilation error that says undefined variables:
So now, those variables should be fresh symbols:
This above is a bit verbose, but you could simplify that. Here is what the previous
for-each-hashtable-bandexample expands into with this new macro:Each time you expand it, the
#:gXXXXvariables are different, and cannot possibly shadow existing bindings, so for example, thebodycan use variables named likecurrent-bandorvaluewithout breaking the expanded code.