So using common lisp, I want to be able to do something of the sorts of:
(defmacro foo (count &rest someExpression)
`(do
((,count 0 (+ ,count 1)))
((= ,count 5) T)
`(eval ,someExpression)
)
)
(foo (print 1) temp)
With the result of it printing 1 5 times. I do not want to simply call (print 1) directly, but by passing the expression through a macro parameter and calling it via the macro. In other words, the macro foo should handle any expression(s) as input and run it. This case does not seem to work.
Edited to clarify an explicit script and intended function.
Starting with your recent version, which is at least a reasonable candidate for a macro unlike the older one:
Well what is the expansion of
(foo (print 1) c)
?Well, that's a disaster: what is that nested backquote doing? Let's just remove it:
That's less disastrous, but the
eval
form is entirely bogus. We can make that 'work' by changing it to be at least syntactically legal:And now
And this will 'work' but it will work purely by coincidence: because
(print 1)
returns1
and the value of1
is1
.and that's a run-time error.
But ... why are you using
eval
?eval
is a terrible, terrible solution to almost any problem you can think of, unless the solution to the problem is called 'code injection attack', and in this case it's not just terrible: it's wrong. So we just remove it.And now
Which looks like the code transformation we want. So, finally:
Which is, finally, fine. And this works:
As with yet another edit to the question it probably is more useful to put the variable name first and allow a bunch of expressions:
This will now allow multiple expressions in the body. And we could go further: we could allow it to specify the number of iterations and the return value`:
And now
Well, the name of this macro is
dotimes
of course.