Understanding higher-order functions
The higher order function is the important property of Haskell, and it is also the common property of all functional languages, and the higher order function is actually the function of functions, which is the form of a function as a parameter of another function, which is the higher order function.
Let's take a look at an example of a function that takes a function and invokes it twice, expressed in a mathematical expression is F (f (x)), and the function in Haskell is actually very close to the mathematical expression, which is why Haskell is closer to math.
Applytwice:: (A, a), a-a
applytwice f x = f (f x)
(A->a) indicates that the first parameter is a function, the input of the function is a type, the output is a type, the second parameter a means that the second argument is a variable, and the function output is also a variable.
Looking at the second line, F represents the function, the Appletwice function actually implements the F (f (x)) function, and, in practice, F is an abstract concept that can represent any function of a variable, which is very similar to the mathematical f (x), and F (x) is any function that represents an independent variable x. So we can do this at the time of the call.
Applytwice (+3) 10
Representation, (10+3) +3, where F (x) =x+3, so F (f (x)) =x+3+3.
Of course, we can also come up with a more complicated one, where anonymous functions are used.
Applytwice (\x->x^2+3) 10
Here, the parentheses inside the anonymous function, this concept should be no strangers, actually the whole function is F (f (x)) = (x^2+3) ^2+3. map and Filter
Map and filter are also important components of functional programming, and this concept has been accepted by many command-line programming languages, such as Python.
A map takes the elements out of a set of lists, executes the specified functions, and then generates a new set of lists.
For example we have a group of list,[1,2,3,4,5,6], I need to put them all under the square, according to the previous practice, we can write:
[x^2 | x<-[1,2,3,4,5,6]]
If we use the map form, we can write this:
Map (\x->x^2) [1,2,3,4,5,6]
This is the map, which is completely equivalent to the above, but it is more clear.
Fliter is an input parameter that is a constraint and a list that callbacks all the elements in the list that meet the criteria.
For example, we would like to find a list of more than 3 of all the numbers, according to the previous wording, we can do so:
[x | x <-[1,2,3,4,5,6],x>3]
Similarly, if you use filter, you can write this
Filter (>3) [1,2,3,4,5,6]
Map and filter are important ideas and methods of functional programming, which have been borrowed from many languages, so we have to master them carefully. function Combination
function combination, is actually a shorthand function of the way, such as a list XS, we need to call a,b,c three function, then I can write C.B.A Xs, actually represents C (b (a xs)). Anonymous Functions
Anonymous functions We are not specific, we have begun to use the above, his general form is: \ parameters, such as the function body, such as \x---x^2+3 anonymous function No name
This tutorial references the Haskell Fun Guide, It is a study note for this guide, but I have joined some of my own ideas and summaries, first of all, thanks to the author of the Haskell Guide, bonus and the mainland translator Fleurer and Taiwanese translator MnO2, for your previous work.