budu wrote:
> Hi, I've been having a great time using Clojure in the last few month.
> But this week I've come accross something that is blocking me. I'm
> trying to write a very simple pattern matching macro to use in other
> macros for matching forms. I've based my macro on one posted in this
> group by James Reeves a few months ago. It's working great but, as it
> uses eval, it does not evaluate in the lexical scope.
>
> (defmacro match [value & clauses]
> (when (and clauses (= 0 (rem (count clauses) 2)))
> (let [m (gensym 'm)]
> `(if-let ~m (match-forms '~(first clauses) ~value)
> (eval (list 'let ~m '~(second clauses)))
> (match ~value ~@(rrest clauses))))))
>
> user=> (match '(1 2 3) (a b c) (list c b a))
> (3 2 1)
> user=> (let [z 4] (match '(1 2 3) (a b c) (list z c b a)))
> java.lang.Exception: Unable to resolve symbol: z in this context
I am not sure of your detailed requirements but maybe the
pattern matching in 'let' can help. Based on your
above example.
user=> (let [[a b c] [1 2 3]] (list c b a))
(3 2 1)
user=> (let [[a b c] [1 2 3] z 4] (list z c b a))
(4 3 2 1)
user=> (let [z 4 [a b c] [1 2 3]] (list z c b a))
(4 3 2 1)
user=>
Actually 'let' in Clojure has some really cool pattern matching
features keyword matching, :keys, :as and :or clauses. Its different
from the 'let' you see in CL. Have a look at:
http://clojure.org/special_forms
Parth
>
> How can I get around this problem? I'm personnaly not very fond of
> using eval so is there another special form better suited for this? Or
> maybe it would be better to find another way to resolve this problem,
> but I can't think of anything right now.
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
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 from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---