Clojure let vs Common Lisp let

In Common Lisp, let uses a list for bindings, i.e.

 (let ((var1 1) (var2 2)) ...) 

While Clojure uses a vector instead:

 (let [a 1 b 2] ...) 

Is there any specific reason, besides readability, for Clojure to use a vector?

+6
source share
2 answers

You can find Rich Hickey's argument in Simple Easy Easy - slide 14, about 26 minutes:

Simple Made Easy - slide 14

+8
source

The rich string on this was

β€œSince we talked about syntax, let's look at the classic Lisp. It seems to be the simplest syntax, all this in brackets is a list of characters, numbers and a few other things. Which could be simpler? But in fact it is not the easiest, since to achieve such homogeneity, there must be a significant overload of the meaning of lists, which can be functional calls, grouping of constructs or data literals, etc. And a definition that requires using context, increasing the cognitive load when scanning code to evaluate it Clojure adds some more composite data literals to lists and uses them for syntax, which means that lists are almost always called things, vectors are used for grouping, and maps have their own literals. significantly reduce cognitive load. "

One of the things that he thought was overloaded with standard syntax is access time. Therefore, the vector syntax in the arguments is associated with a constant access time when you used them. He said:

It seems strange, though, since it is valid for only one form ... as soon as it is stored in a variable or transmitted in any way, the information is "lost". For instance...

 (defn test [a] (nth a 0)) ;;<- what is the access time of and element of a? 

I personally prefer hard syntax changes, such as parentheses, which need to be reserved when the programmer needs to switch mental models, for example. for embedded languages.

 ;; Example showing a possible syntax for an embedded prolog. {size [],0} {size([H|T],N) :- size(T,N1), N is N1+1} (size '(1 2 3 4) 'n) ;; here we are back to lisp code 

Such a concept is syntactically constant. At run time, you do not β€œbypass” the structure. Before executing the runtime (read / macro / compile time) is another matter, therefore, when possible, it is often better to store things as lists.

[edit] original source seems to have gone, but here is another interview entry: https://gist.github.com/rduplain/c474a80d173e6ae78980b91bc92f43d1#file-code-quarterly-rich-hickey-2011-md

+2
source

Source: https://habr.com/ru/post/984489/


All Articles