On Friday, January 2, 2015 8:13:41 PM UTC-5, novato wrote:
>
> I choose clojure as my first programming language after some research. I 
> am learning by doing Clojure Koans <http://clojurekoans.com/> exercises. 
> I reached to the functions "lesson" and I have a doubt on the following 
> code:
> ((fn [x] (* 5 x)) 5)
>
> Why is that the last 5 is outside the fn parentheses? Why this doesn't 
> work? 
> (fn [x] (* 5 x) 5)
>
> Thanks in advice for the clarification and excuse my poor english.
>

The (fn ...) form evaluates to a function. In the first case, there is a 
(fn ...) form that evaluates to a function that quintuples its argument, 
inside a form that calls this function with 5 and should itself evaulate to 
25. The second line will evaluate to a function that quintuples its 
argument, throws this result away, and then returns 5, without calling that 
function. (constantly 5) would be equivalent, and more efficient (at least 
prior to JIT optimization).

Everything inside the (fn ...) parentheses is part of the function 
definition.

It might help to break the first one into its components. This is 
equivalent:

(let [f (fn [x] (* 5 x))]
  (f 5))

First we create (fn [x] (* 5 x)) and call it f. Then we call f with 5. 
Replacing the symbol f with what f gets bound to by the let should produce 
the same results; and such a replacement produces the first line of code 
from your post.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to