An important concept of Haskell -- function Table of Contents
- 1. Haskell basic knowledge
- 2 Haskell important data structure-list
- 3 Haskell Common Data Structures
- 4 Haskell type
- 5 important Haskell concepts-Functions
- 5.1 Function Definition
- 5.2 High-Order Functions)
- 5.2.1 curried Functions
- 5.2.2 anonymous Functions
- 5.2.3 functions with functions as parameters
- 5.2.4 function combination
1 Haskell basic knowledge 2 Haskell important data structures-list3 Haskell common data structures 4 Haskell type 5 Haskell important concepts-Functions
Haskell's programming method is called functional programming, so the concept of function plays a very important role in Haskell.
5.1 Function Definition
- Simplest Function Definition
doubleMe x = x + x
ghci>doubleMe 36ghci>1 + (doubleMe 3)7ghci>doubleMe 3.46.8
In the above function definition, we did not provide the parameter type and return type of the function, so it can be used for integers or floating point numbers. If we want to only use floating point numbers, we can define them as follows:
doubleMeFloat :: Float -> FloatdoubleMeFloat x = x + x
ghci>doubleMeFloat 2.44.8ghci>doubleMeFloat 24.0
Float-> float indicates that a parameter of the float type (the first float) is accepted, and the result of a float type (the second float) is returned.
- Pattern Matching and recrusion
doubleList :: (Num a) => [a] -> [a]doubleList [] = []doubleList (x:xs) = (x+x):(doubleList xs)
ghci>doubleList [1,2,3][2,4,6]ghci>doubleList [1.1,2.2,3.3][2.2,4.4,6.6]
The above Code uses the type parameter A when defining the function type, so that the function can be applied to multiple types. In addition, because the function definition contains (x + x), addition is used, therefore, type A must be an instance of num.
5.2 high-order functions 5.2.1 curried Functions
Strictly speaking, all functions in Haskell have only one parameter. This is another confusing place, because we have used many functions, not just one parameter. For example
ghci>:t maxmax :: Ord a => a -> a -> aghci>max 3 44
Let's do some experiments to observe some phenomena. First, define our own Max functions.
maxInteger :: Integer -> Integer -> IntegermaxInteger x y | x >= y = x | otherwise = y
ghci>:t maxInteger maxInteger :: Integer -> Integer -> Integerghci>maxInteger 3 44
Then we can get another function in the following way.
ghci>let maxInteger' = (maxInteger 3)ghci>:t maxInteger'maxInteger' :: Integer -> Integerghci>maxInteger' 44ghci>maxInteger' 55ghci>maxInteger' 33ghci>maxInteger' 23ghci>maxInteger' 13
We only gave maxinteger a parameter 3, returned the result, and then obtained the maxinteger function '. This function receives a parameter. The function compares this parameter with 3 and then outputs a large number. This means that maxinteger receives an integer and returns a function. Let's take a look at the maxinteger type:
maxInteger :: Integer -> Integer -> Integer
Maxinteger can be interpreted as receiving two integers, and an integer is returned. It can also be interpreted as receiving an integer to return a function. The returned function type is integer-> integer, that is, maxinteger. From the experiment, the second explanation is more correct. This also shows that the Haskell functions we proposed at the beginning all have only one parameter. If you have learned lambda calculus, you will not be unfamiliar with this 1.1. Based on this, we can define the function as follows:
max3 = max 3
ghci>max3 13ghci>max3 23ghci>max3 33ghci>max3 44ghci>max3 55
This function does not explicitly provide parameters during definition, but can receive parameters during use.
5.2.2 anonymous Functions
Sometimes we need a function, but this function is only used temporarily, and we don't even need to give it a name. For example, the following function:
ghci>:t mapmap :: (a -> b) -> [a] -> [b]
Map receives a function, a list, and returns another list. If the function received here is only temporarily used, we can do this:
ghci>map (\x -> x + 1) [1,2,3][2,3,4]
Here (\ x-> x + 1) is an anonymous function. In addition, if an anonymous function has two parameters
ghci>(\x y -> x + y) 1 23
The definition of this anonymous function is actually the definition of the function in lambda calculus.
5.2.3 functions with functions as parameters
- Map
The function acts on every element of the list, puts the result of each operation to another list, and returns the result list"
map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs
ghci>map (+3) [1,2,3][4,5,6]ghci>map (\x -> x+3) [1,2,3][4,5,6]
- Filter
Receives a conditional function, a list, and extracts all elements in the list that meet the conditions.
filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) | p x = x : filter p xs | otherwise = filter p xs
ghci>filter (>=3) [5,2,1,4,3,7][5,4,3,7]
- Foldl
What do you do if you want to add all the elements in a list? Do I use pattern matching and Recursion? Foldl provides a solution.
foldl :: (a -> b -> a) -> a -> [b] -> a
First, let's test it and have an intuitive impression.
ghci>foldl (\acc x -> acc + x) 0 [1,2,3]6ghci>foldl (\acc x -> acc + x) 0 [1,2,3,4]10ghci>foldl (\acc x -> acc + x) 0 [5,8,4]17
Foldl receives a function, a value, a list, and returns a value. The procedure is as follows: first, the second parameter is the initial value of ACC. Each time an element is extracted from the third parameter, the ACC value and this element are used as the parameters of the first parameter (a function). The result returned by the first parameter (a function) is used as the new value of ACC in subsequent operations. Until the third parameter is. Take the third example as an example: the initial value of ACC is 0, and (\ acc x-> ACC + x) 0 5 returns result 5, which is a new value of ACC. The new ACC value is 5 (\ acc x-> ACC + x) 5 8. Expected result 13 is displayed as the new ACC value.
The new ACC value is 13 (\ acc x-> ACC + x) 13 4. Expected result 17 is displayed. The new ACC value is 17. List: [5, 8, 4] Each element has been used, and the program ends. The final value of ACC is 17.
5.2.4 function combination
In mathematics, I actually learned how to combine functions. For example, F = x + 1G = x * 3 makes H = f o G. H is a new function at this time, if a parameter 2 is passed to H, the first parameter is g, 2*3 to get 6, and then 6 is used as the parameter of F, 6 + 1 = 7. Haskell also provides this combination method.
(.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x)
f' :: Integer -> Integerf' x = x + 1g' :: Integer -> Integerg' x = x * 3
ghci>g' 26ghci>f' 67ghci>let h = f' . g'ghci>h 27