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".

Sign up to request clarification or add additional context in comments.

2 Comments

So the specific answer for the expression in question is (= 25 ( (fn [f] (f 5)) (fn [n] (* n n)))), is that correct?
I think so, that evaluates to true in my repl.
5

or you can use the short-hand notation:

#(%1 5)

1 Comment

Even shorter: #(% 5)
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 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.