Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation order of function arguments in Clojure

Tags:

clojure

Does Clojure specify the order of evaluation of function arguments?

I.e. When I call a function in Clojure:

(my-func (fun1 arg1) (fun2 arg2) (fun3 arg3))

Is the order of evaluation of fun1, fun2, and fun3 defined?

I understand that Java defines the order of evaluation of function arguments as left to right, but I can imagine a functional language like Clojure being more relaxed.

like image 744
pauldoo Avatar asked Jan 27 '11 10:01

pauldoo


2 Answers

Order of evaluation is left to right. See http://clojure.org/evaluation

Both the operator and the operands (if any) are evaluated, from left to right.

like image 57
Heiko Rupp Avatar answered Sep 19 '22 18:09

Heiko Rupp


Left to right, but inside out as well (fun1 (fun2 arg2)).

I think that in general for a functional language, you would want to avoid the situation where (fun1 arg1) influences (fun2 arg2) or (fun3 arg3). (fun1 arg1) ideally would return a value without altering the state of existing data structures in your program.

Therefore, depending on how close you keep to a functional programming style, the left-to-right order of evaluation is unimportant.

see http://clojure.org/state

like image 29
bOR_ Avatar answered Sep 16 '22 18:09

bOR_