What does parallel binding mean in Clojure
clojure, lisp
Solution
Sequential binding
a = 1
b = 2
Here
- `1` is evaluated
- then bound to `a`
- then `2` is evaluated
- then bound to b
Parallel binding
a,b = 1,2
Here,
- `1` and `2` are evaluated, either in a determined order (such as left to right) or not, depending on the language specifications
- the two results are bound to `a` and `b`, respectively.
If the expressions (here `1` and `2`) are independant and side-effect free, it doesn't matter which binding you use, but in parallel you need to be aware of the exact evaluation order.
Now, in your case,
- first `(rest vectorA)`
- then `(first vectorA)` are evaluated (left to right)
- then the results are bound to `vectorA` and `A`, respectively.
which is a parallel binding, as opposed to for example a `let` binding in Clojure which is sequential.
Problem
I see the binding of `recur` is "parallel", however I don't get what that means. I've tested the code below: ``` (defn parallelTest "parallel binding test of recur " [] (loop [vectorA [1 2 3 4 5] A (first vectorA)] (if-not (= A nil) (do (println vectorA) (println A) (recur (rest vectorA) (first vectorA)))) ;Recur! )) (parallelTest) ``` the output is ``` user=> [1 2 3 4 5] 1 (2 3 4 5) 1 (3 4 5) 2 (4 5) 3 (5) 4 () 5 nil ``` so I assume the bindings are happened simultaneously instead of one by one?