Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why LET doesn't work with VECTOR?

Tags:

clojure

Instead of

(let [x 1] (my-expression))

I'm trying to use:

(let (vector x 1) (my-expression))

Don't ask why, I just like normal brackets more. But Clojure says:

let requires a vector for its binding in ...

What's wrong?

like image 776
yegor256 Avatar asked Aug 23 '26 13:08

yegor256


2 Answers

The let special form binding form is required to be a vector literal not just an expression that would evaluate to a vector.

Why? Roughly stated, the expression must be compiled before it can be evaluated. At compile-time (vector x 1) will not have been evaluated to a vector, it will just be a list. Indeed if it were to be evaluated, the arguments of vector would be evaluated, meaning x would have to be resolved. But, you don't want x to be resolved, you want it bound.

like image 198
A. Webb Avatar answered Aug 26 '26 22:08

A. Webb


let requires a vector for it's bindings (at compile time) so trying to put a vector functional call in its place won't work (as that will only produce a vector at runtime).

However you can make your own let with a bit of macro-fu:

(defmacro mylet [bindings & exprs]
  `(let ~(vec bindings) ~@exprs))

(mylet (x 1) (inc x))
=> 2
like image 22
mikera Avatar answered Aug 27 '26 00:08

mikera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!