Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure Higher-order functions take function arguments, but what is the syntax?

Tags:

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.

like image 823
Ezward Avatar asked Feb 16 '14 20:02

Ezward


People also ask

What is the syntax for a function with arguments?

Arguments typically have some restrictions on the expression used for that argument. For example, the input argument to the INDEX function can only be an object name. Depending on the function, one or many data objects can be used for an input argument for one function evaluation.

Can higher-order functions be used as arguments?

A higher order function is a function that takes a function as an argument, or returns a function . Higher order function is in contrast to first order functions, which don't take a function as an argument or return a function as output.

What is a higher-order function give an example?

Higher order functions are functions that operate on other functions, either by taking them as arguments or by returning them. In simple words, A Higher-Order function is a function that receives a function as an argument or returns the function as output. For example, Array. prototype. map , Array.

What are higher-order functions how these functions are defined and used in functional programming?

Higher Order Function In functional programming, a function is defined as a "first order citizen". This means it includes all the properties generally available to any other element, such as the possibility of being affected to a name, returned as a result or passed as a parameter.


2 Answers

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

like image 91
noisesmith Avatar answered Oct 29 '22 08:10

noisesmith


or you can use the short-hand notation:

#(%1 5) 
like image 32
Jeroen Avatar answered Oct 29 '22 07:10

Jeroen