pauldoo
pauldoo

Reputation: 18615

Evaluation order of function arguments in 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.

Upvotes: 12

Views: 1594

Answers (3)

Wojciech Winogrodzki
Wojciech Winogrodzki

Reputation: 412

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

my-func has arity of 3. The result of (fun1 arg1) will be passed as the first argument to my-func. Etc. Then my-func will be executed.

Therefore the order of evaluation of arguments to my-func does not matter at all.

If you have side-effects, then, well, hmmmm... But this answer does not change. The order does not matter.

Cheers -

Upvotes: 1

bOR_
bOR_

Reputation: 381

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

Upvotes: 10

Heiko Rupp
Heiko Rupp

Reputation: 30934

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.

Upvotes: 13

Related Questions