Anonymous function consuming another anonymous function - clojure koan

I am working on clojure koans, and one of the questions in the functions needs an additional explanation for me to “get it” and have a moment of aha. I can write a function that satisfies the question. But I do not quite understand why all the bits work.

Clojure> (= 25 ((fn [a b] (b a)) 5 (fn [n] (* n n))))
true

Question 1. I do not understand why this causes an error:

Clojure> (= 25 ((fn [b a] (b a)) 5 (fn [n] (* n n))))
java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

Thus, the only change to the above is to switch the order of b and a. In my brain, I read "a function that takes a and b" or "b and a", but how they are used depends on the subsequent statement in parens. Why is the question in this question?

Questions 2.

Clojure> (= 25 ((fn [a] (5 a)) (fn [n] (* n n))))
java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

Why, when I substitute the value of b for int, do I present an error?

Quentions 3.

((fn [a b] (b a)) 5 (fn [n] (* n n))))

(b a) b 5, . , , ?

+3
1
  • :

    (b a)
    

    b -, b . 5 b, 5 . , 5 :

    ((fn [argument function] (argument function)) ;; problem!!
     5 
     (fn [n] (* n n)))
    

    lisps: s-:

    (f x y z)
    

    f, x, y z f x, y z.

  • . 1 - '5' . :

    ((fn [function] (5 function)) ;; problem!!
     (fn [n] (* n n)))
    

    :

       ((fn [a] (a 5))  ;; 'a' and '5' flipped
        (fn [n] (* n n)))
    

    .

  • this is not a problem: aequals 5, b- function c (b a). With descriptive names, this makes sense:

    ((fn [argument function] (function argument)) 
     5
     (fn [n] (* n n)))
    
+6
source

All Articles