Why are you making a special shape?

In Clojure, why do special form instead of a function implemented like this?

 (defn do [& exprs] (last exprs)) 
+6
source share
1 answer

The main semantics of sequential execution

fn (well, fn* ) actually reuses the do logic, like other forms with bodies - let* , letfn* , try . Since there are several of them, it makes sense to repeat the use of the basic logic of “evaluate expressions in sequence”; and since this in itself is useful for user code outside the context of a more complex special form, it makes sense to expose it as do .

Instead of fn* , a special special form was created, a sequential evaluation of expressions would include a function call - the overhead of this would not be acceptable for this type of low-level object.

(On the other hand, replacing other special forms with macros wrapping their bodies in an implicit do would not have incurred any overhead. However, in the context of the Clojure compiler, since it costs to be a huge victory - at least that’s pretty DRY already.)

Top level do

In addition, the top level do handled specifically by the compiler in that

 (do (foo) (bar)) 

equivalent at the upper level (and only at the upper level) to

 (foo) (bar) 

- that is, to individual expressions written out separately. This allows macros to output (equivalent to code) multiple top-level expressions.

It would be impossible to interpret the top-level calls of any special function in this way, but using a special form for this purpose is much cleaner.

+6
source

All Articles