Philip Fominykh on 3 Feb 2011 08:58:22 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: let substitution in clojure?


dan are you making internet do your homework for you again? --philip

On Thu, Feb 3, 2011 at 06:02, Dan Mead <d.w.mead@gmail.com> wrote:
Hi guys, I'm having trouble figuring out the proper way to do some
macro stuff for my project.

I've got a pattern matcher that works as you'd expect. I can take
patterns which are quoted s-expressions and match them to actual
values. This returns a set of bindings, which I want to use to rewrite
a corresponding _expression_.

it's working with a regular dumb function that crawls down a quoted
_expression_ and does replacement with the bindings list. This is pretty
bad as it runs in theta(n) time, and is really brittle when it comes
to scoping issues.


So, I'm trying to get a macro that will splice the bindings list as a
vector and the target _expression_ into a let statement. That way
scoping should be less of an issue and it should run in constant time
(or whatever let run in etc etc)


so, to match some pattern to a set of values we can do

user=> (match '(x y z) '(1 2 3))
((x 1) (y 2) (z 3))

If that call to the matcher is wrapped in some stuff to convert to a
vector, we can do

user=> (domatch '(x y z) '(1 2 3))
[x 1 y 2 z 3]

and clojure agrees that it's a vector

user=> (vector? (domatch '(x y z) '(1 2 3)))
true

if i have a macro like this

(defmacro sub [binds expr]
Â`(let ~binds ~expr))

which works fine, if you give it a literal vector

user=> (sub [x 1 y 1] (+ x y))
2

then why doesn't it work with my call to the matcher?

user=> (sub (domatch '(x y z) '(1 2 3)) (+ x y z))
java.lang.IllegalArgumentException: let requires a vector for its
binding (NO_SOURCE_FILE:0)


I'm afraid I've lost the plot here. Any insight you guys could give
would be helpful

Here is my newbie and entirely too verbose code.

http://taz.cs.wcupa.edu/~dmead/patternmatch.clj

Dan

sorry if you get this post twice