Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does let require a vector?

I never really thought about this until I was explaining some clojure code to a coworker who wasn't familiar with clojure. I was explaining let to him when he asked why you use a vector to declare the bindings rather than a list. I didn't really have an answer for him. But the language does restrict you from using lists:

=> (let (x 1) x)
java.lang.IllegalArgumentException: let requires a vector for its binding (NO_SOURCE_FILE:0)

Why exactly is this?

like image 904
Jason Baker Avatar asked Oct 04 '10 22:10

Jason Baker


People also ask

How are vector fields used in real life?

The daily weather chart (showing wind speeds) is an example of a vector field. The wind direction and strength is given for different points on the Earth's surface.

What are vector fields used for?

Vector fields are often used to model, for example, the speed and direction of a moving fluid throughout space, or the strength and direction of some force, such as the magnetic or gravitational force, as it changes from one point to another point.

Can you use a vector as a queue?

You "can" use a vector over a queue, if the queue lifetime is short or if you know the maximum size of your queue.

What is a vector in Rust?

Vector is a module in Rust that provides the container space to store values. It is a contiguous resizable array type, with heap-allocated contents. It is denoted by Vec<T>. Vectors in Rust have O(1) indexing and push and pop operations in vector also take O(1) complexity.


1 Answers

Mostly readability, I imagine. Whenever bindings are needed in Clojure, a vector is pretty consistently used. A lot of people agree that vectors for bindings make things flow better, and make it easier to discern what the bindings are and what the running code is.

Just for fun:

user=> (defmacro list-let [bindings & body] `(let ~(vec bindings) ~@body))
#'user/list-let
user=> (macroexpand-1 '(list-let (x 0) (println x)))
(clojure.core/let [x 0] (println x))
user=> (list-let (x 0 y 1) (println x y))
0 1
nil
like image 172
Rayne Avatar answered Oct 21 '22 15:10

Rayne