views:

85

answers:

4

I have a let statement in which I would like to dynamically destructure a list. The following is my solution:

symList  ;; list of some Strings which will become the vector of Symbols to assign to
valList  ;; list of some values, same length as symList

(let [(map read-string symList) valList]
  ...)

An example value of symList would be ("pt1" "pt2") and an example value of valList would be (1 2)

However, this produces an exception that the first part is an "unsupported binding form". I suspect I am missing something regarding syntax-quoting, or that it's not possible. Any advice would be greatly appreciated.

EDIT: I will only know these values come run time, hence this approach. Secondly, I need to be able to pass the lexical scope later on, hence the use of let.

A: 
(let [valList (map str symList)]
   (somefun valList))

The error you are getting is because your let statement was backwards (let [val SOURCE] STUFF)

Joshua Smith
I thought `let` worked in such a way that it was (symbol value). So in this case, I would want to be able to refer to the symbol pt1 and get 1, pt2 and get 2, etc.
allie
(let [pt1 1 pt2 2] (println pt1)) would print a 1 via stdout.
Joshua Smith
+2  A: 

If symList and valList have the correct value at compilation time, then you could write a macro to do this. If they are only known at runtime, you are going to have to use/write a function to do the destructuring for you and return the result of that destructuring as some data structure.

In the second case, for something simple like this, you can use (zipmap symList valList) to get a map.

Brian
I figured as much, and have already written a function to do that, as the values won't be known till run time. The problem is that I need to then pass the lexical scope elsewhere (with these values bound to their symbols), which can't be done with `def` (I believe - please correct me if I'm wrong). Is there any way to do the destructuring using zipmap and still have it defined in the `let`?
allie
Lexically you can't do that (unless the target code is still code and you want to construct code that you pass to eval). Dynamic scope is doable, though. If symList was a list of vars and the target was written to use those vars, you could use clojure.core/with-bindings to do what you want. There is probably a better solution to the problem you are trying to solve with this approach.
Brian
A: 

maybe matchure can give you what you want

jneira
I checked it out, but I couldn't seem to find anything that would do that, although it is a cool library.
allie
A: 

Although I've been unable to find a way to dynamically destructure a list, for those of you who are interested in creating values that are lexically instead of dynamically scoped, I found that intern to whatever namespace you need works well.

allie