Great! Thanks all for your explanations.
On Wednesday, January 27, 2016 at 4:33:40 AM UTC-8, gianluca torta wrote:
>
> Hi Dan,
>
>
> And, if I understand correctly, what was really happening with the macro
>> bar was that in ('+ 2 'u), '+ was looking itself up in 2, not finding
>> itself, and so
Hi Dan,
And, if I understand correctly, what was really happening with the macro
> bar was that in ('+ 2 'u), '+ was looking itself up in 2, not finding
> itself, and so returning the default value 'u. This was the expansion of
> the macro, and so at run time, it was bound to 10.
>
>
exactly,
A macro emits a form for the compiler to compile. And it understands its
inputs as forms -- unevaluated forms.
- Your plus-two emits a list with + in function position.
- foo, when it is computing the form to emit, applies + to 2 and some form
x. So 5 works but u does not, even after (def u
Thanks for your reply. I guess I was forgetting that at macroexpansion time
(if I'm using the word correctly), we don't have access to the binding
between u and 10. The same error I message I noted above does appear for (+
2 'u).
And, if I understand correctly, what was really happening with th
> I know that this macro will add 2 to its argument:
This isn't what that macro does. That macro takes a piece of syntax, and
sticks that syntax in the hole in the expression (+ 2 HOLE). The
macro doesn't evaluate the expression, it just generates it. It is the
evaluation of *that* expression that
you can also use macroexpand-1 to see how your macro expands
for example, having defined your macro as suggested by Gary and James
above, you can write something like:
(macroexpand-1 '(if-zero (- 4 2 2) (+ 1 1) (+ 2 2)))
and see that it correctly to:
(if (clojure.core/= (- 4 2 2) 0) (+ 1 1) (+ 2
Macros differ from functions in two ways:
1. They're executed at compile-time, rather than run-time.
2. Their arguments are unevaluated.
Your if-zero function probably doesn't act the way you expect it to,
because you're expecting test-expr to be evaluated.
If all of the arguments to your macro
Yes, you need to revisit the fundamentals of macros.
Macros are functions that take in data (the forms), and return data
(possibly altered forms).
In your example, you're checking if the expression itself, a list or some
kind of value or something is equal to the value 0, at compile-time, then
yo
That works - thanks.
Still, it seems intuitive to build the code as I had originally done.
It's too bad code has to get complicated so quickly :)
Thank again for the help.
On Jan 3, 9:19 pm, Robert Marianski wrote:
> On Tue, Jan 03, 2012 at 07:22:22PM -0800, Trevor wrote:
> > hmmm macro quest
On Tue, Jan 03, 2012 at 07:22:22PM -0800, Trevor wrote:
> hmmm macro question:
>
> ; here's a macro that assembles many puts. It serves no purpose to me
> (and doesn't even make sense in its current form). It's just something
> I hit playing around and learning macros.
>
> (defmacro doto-putt
On Jan 3, 7:22 pm, Trevor wrote:
> hmmm macro question:
>
> ; here's a macro that assembles many puts. It serves no purpose to me
> (and doesn't even make sense in its current form). It's just something
> I hit playing around and learning macros.
>
> (defmacro doto-putter [x y xs]
> `(doto (
The first solution looks good. I had no idea that wrapping a defn
inside of let's would leave the function exposed. Interesting. Is it
considered idiomatic though?
On May 30, 2:16 pm, Erik Söhnel wrote:
> Hi,
>
> Not really mechanisms, but two Idioms will help you with your problem:
> Either w
Hi,
Not really mechanisms, but two Idioms will help you with your problem:
Either wrap the defn in a let:
(let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
S (gen-vector [3 4 5])]
(letfn [(prims-fro
Thanks a lot.
This year starts well (I learned something :-)
Regards
Poul
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe
On Jan 1, 2009, at 5:45 AM, synphonix wrote:
>
> Hello and happy new year,
>
> I've started this year with playing around with clojure macros and
> wrote a macro that
> behaves in a way I don't understand:
>
> (defmacro foo
> ([x] `(list ~x ~x))
> ([x n] (if (<= n 0)
> `(foo ~x)
>
Hello,
just a follow up: I discovered that I sent the macro def twice and
than applied the macro. If the first time the defmacro is evaluated
then the resulting macro works as I expected. But when I send the same
defmacro a second time to the interpreter, the macro behaves as
described below.
Can
16 matches
Mail list logo