25

I am doing the closure tutorial at http://clojurescriptkoans.com and I am stuck here: http://clojurescriptkoans.com/#functions/9

It looks like this

Higher-order functions take function arguments

(= 25 ( _ (fn [n] (* n n))))

I am supposed to fill in something at the underscore to make the expression true. I have no clue what to do.

3 Answers 3

39

The syntax simply consists of binding the function, and then calling it.

Since this is an exercise, I will show a similar situation rather than showing the exercise's solution:

user> ((fn [f] (f "abc")) (fn [s] (str s s s)))
"abcabcabc"

here I bind the argument of the first function to f, and call f with the argument "abc".

2
  • 1
    So the specific answer for the expression in question is (= 25 ( (fn [f] (f 5)) (fn [n] (* n n)))), is that correct?
    – Ezward
    Commented Feb 16, 2014 at 21:10
  • I think so, that evaluates to true in my repl.
    – noisesmith
    Commented Feb 16, 2014 at 21:35
5

or you can use the short-hand notation:

#(%1 5)
1
  • 10
    Even shorter: #(% 5)
    – Nick
    Commented Nov 19, 2014 at 9:15
0

Higher order functions takes functions as arguments. Defining two functions

user=> (defn multiply [n] (* n n))
#'user/multiply

user=> (defn add [n] (+ n n))
#'user/add

Defining higher order function

user=> (defn highorderfn [fn number] (fn number))
#'user/highorderfn

Calling the higher order function

user=> (highorderfn multiply 5)
25
user=> (highorderfn add 5)
10
1
  • 2
    This is considered here at StackOverflow a "code-only" answer and not appreciated. Would you like to improve it by adding some explanation how and why this helps? Try to also highlight how this givess additional insight to existing older, better explained, upvoted and accepted answers. Please take the tour and consider the recommendations in How to Answer.
    – Yunnosch
    Commented Nov 2, 2018 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.