tmountain a écrit :
> Hi guys. I'm in the process of learning Clojure and loving it so far.
> I've been dabbling in Common Lisp for years, but Clojure has so many
> nice features that it's rapidly becoming my language of choice.
> Recently, I've been trying to get my head wrapped around macros and
> that an `or macro' would be a good candidate since you want to defer
> evaluation of arguments until absolutely necessary. I hacked this up,
> and it seems to work, but I wanted to see if I could have done
> anything better.
>
> (defmacro my-or [& args]
> (let [head (first args)
> tail (rest args)]
> (if (nil? tail)
> head
> `(if ~head
> ~head
> (my-or ~...@tail)))))
>
You could use multiples arities to replace the (if (nil?... and shorten
your macro:
(defmacro my-or
([head] head)
([head & tail]
`(if ~head
~head
(my-or ~...@tail))))
There's still a problem: ~head appears twice which means the code substituted
to head can be called twice if it returns true.
(defmacro my-or
([head] head)
([head & tail]
`(let [x# ~head]
(if x#
x#
(my-or ~...@tail)))))
hth,
Christophe
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---