Original address :Haskell Study - Higher order function

Higher order function (higher-order function) A function that can operate a function , That is, the function can be used as a parameter , It can also be used as a return result . With these two characteristics ,haskell It can achieve many magical effects .

currying (Currying)

stay haskell All arithmetic operators in are functions ( Including the relationship between size and equal to, etc ), And their shortcuts can omit operands ( Parameters ).

(+) 1 2 -- (+) Is a function that requires two operands 
> 3 (+1) 2 -- (+1) Is a function that requires left operands
> 3 (3*) 3 -- (3*) It's a function that needs a right-hand operand
> 6 map (*2) [1,2,3] -- map All the elements *2 The operation of
> [2,4,6] filter (>3) [2,3,4,5] -- Filter >3 The elements of
> [4,5]

haskell All functions in are prefix mode by default , That is to say : Function name Parameters 1 Parameters 2 ... . But almost all functions with two parameters have infix patterns , Just wrap the function name in inverted quotation marks : Parameters 1 ` Function name ` Parameters 2. Because infix functions are more readable in some cases , More in line with people's understanding habits .

5 `div` 3 -- Mod 
> 1 9 `mod` 7 -- modulus
> 2 'f' `elem` ['a' .. 'z'] -- Does it include 'f'
> True

Essentially ,Haskell All functions of have only one parameter , So what happened to our function with multiple parameters ? That's because all functions with multiple arguments are Curried functions. In fact, from the above example of arithmetic operation function , We can probably guess . And then use an example to verify :

moreThen4 = max 4 -- The minimum is 4 Function of
:t max -- Functions that require two comparable arguments 
max :: Ord a => a -> a -> a :t moreThen4 -- A function that needs a comparable number
> moreThen4 :: (Ord a, Num a) => a -> a

By looking at the type of function, you can see that , Two parameters max Functions can actually be written as (max x) y .moreThen4 In fact, that is max Function is called with incomplete arguments , And create a new return function , The function is in the form of a single parameter .

This sum JavaScript In the use Closure Return function to implement currying It's the same thing . But in functional languages , Functions are first-class citizens , It's just as natural as eating and sleeping .

We have a strange looking description of function types Num a => a -> a -> a , It's understandable . It means that the function takes a numeric parameter a after , Will return a need a Function of type parameter (Num a) => a -> a , The last function takes another parameter a after , Eventually it will be sent back a Result of type .

After removing the redundant parameters, the function is more concise :

sum' xs = foldl (+) 0 xs
sum' = foldl (+) 0 -- Get rid of xs after maxNum x = foldr max 0 x
maxNum = foldr max 0 -- Get rid of x after

Lambda expression

lambda It's nothing new , As early as .NET 4.0 Time C# It has already introduced lambda,JavaScript Also in the ES6 Introduction in .

Write anonymous functions , So you don't have to create named functions . Because anonymous functions come from lambda From calculation , So anonymous functions are often called lambda function .

stay Haskell in , Anonymous functions are marked with backslashes \ Start , Followed by the parameters of the function ( You can include patterns ), And the function body is defined in -> After the symbol .lambda The definition of a function can only have one statement , Cannot set multiple modes for one parameter at the same time , Such as [] and (x:xs).

plusOne = \x -> x+1
checkZero = \x -> if x > 0 then " Greater than 0"
else if x<0 then " Less than 0"
else " be equal to 0"

Collapse function

Traversing lists is a very common requirement , Using folding function instead of explicit recursion for traversal is obviously easier to understand and implement . among foldl It's a combination of left and right ,foldr It's right combination , Generally, the right folding efficiency is relatively high , meanwhile foldr It can also be used for infinite lists , So try to use foldr.

Collapse function call format : fold Processing function Initial value ( Cumulative value ) The list that needs to be collapsed

In addition, it also provides and foldl/foldr alike foldl1/foldr1, They use the first item in the list as the initial value by default , So you can omit the initial value .

map' :: Foldable t1 => (t2 -> a) -> t1 t2 -> [a]
map' f = foldr (\x acc -> f x:acc) [] filter' :: Foldable t => (a -> Bool) -> t a -> [a]
filter' f = foldr (\x acc -> if f x then x:acc else acc) [] elem' :: (Foldable t, Eq a) => a -> t a -> Bool
elem' y = foldl (\acc x -> if y==x then True else acc) False and' :: Foldable t => t Bool -> Bool
and' = foldr1 (\x y->if not y then False else if not x then False else True) -- perform
map' (*2) [1,2]
> [2,4] filter (>2) [1,2,3,4]
> [3,4] elem' 1 [1,2,3]
> True and' [True,False,True]
> False

And foldl and foldr alike scanl and scanr, They record all the states of the accumulated value to a List.

Also have scanl1 and scanr1.

scanl (+) 0 [3,5,2,1]
> [0,3,8,10,11] scanr (+) 0 [3,5,2,1]
> [11,8,3,1,0]

also foldl' and foldl1' It's a strict version of their respective implementations . In use fold Deal with larger List when , Stack overflow is a common problem . And the culprit is fold The inertia of : In execution fold when , The value of the accumulator is not immediately updated , It's about being a " When necessary, it will achieve the desired results " Commitment . Every time I go through the accumulator , This behavior is repeated once . And all of this stuff " promise " It's going to end up filling your stack . Strict fold There would be no such problem , They don't do " promise ", Instead, we calculate the intermediate value directly and continue to execute it . If you use inertia fold Overflow errors are often encountered during the process , We should use their strict version instead .

Function combination

$) It's called function callers , It has the lowest priority .

 f $ g x => f (g x)
-- take >2 The list length of 
length (filter (>2) [1,2,3,4])
length $ filter (>2) [1,2,3,4] -- Lower the priority, remove the brackets
> 2

(.) Function composition operator , It can combine functions , And generate new functions , Then pass it to other functions . Of course we can use lambda Realization , But most of the time , The use of function combinations is undoubtedly clearer .

(f . g) x => f(g x)
-- Verify that the string is a number 
not ( and ( map isDigit $ "12as"))
not . and . map isDigit $ "12as" -- Use a combination to remove parentheses
> True

These two operators are the artifact of eliminating brackets , Have they , The readability of the code is greatly improved .

Let's reuse haskell Powerful pattern matching capabilities , Change the direction of the function , The effect after transformation is similar to unix/linux The pipe , Rewrite the above two expressions . Now even ($) (.) I don't need any more , It's a blast , Is there any

Haskell Study - More articles on higher-order functions

  1. Python Study —— Higher order function filter and sorted

    filter filter Function as the name implies , Screening , By calling the function to filter the subitems in the sequence that satisfy the function Let's talk about it with examples : Filter all even numbers in a sequence , Keep odd The other is as follows , Filter out all spaces and empty characters in a sequence You can tell ...

  2. Python Study --- Learning higher-order functions

    Higher order function Higher order function : The function name can be passed as input , Function names can also be returned as return values Function names can be reassigned , Because it's a variable in itself     A function itself is an object ,    The variable name of the function f It points to the function itself , Plus ...

  3. python Study —— Higher order function

    Recursive function Inside the function , You can call other functions . If a function calls itself internally , This function is a recursive function . The advantage of using recursive functions is that the logic is simple and clear , The drawback is that too deep a call can cause a stack overflow . But for the tail recursion optimization language, we can use tail recursion to prevent ...

  4. Haskell Higher order function

    Haskell functions can take functions as parameters and return functions as return values. A function ...

  5. JavaScript Learning notes ( Ten )—— Of higher order functions map,reduce,filter,sort

    I'm learning from master Liao Xuefeng JavaScript tutorial , There are some points that need attention , So list them as study notes , Remind yourself of ! If you need to , Welcome to my blog https://www.liaoxuefeng.com/ ...

  6. Python Xiaobai's way of learning ( fourteen )—【 Scope 】【 Anonymous functions 】【 Programming methodology 】【 Higher order function 】

    Come on, inner play Before there is a specific scope , I mentioned it in my previous study notes I started to think it was my own words I didn't expect that this word already existed ( Hand cover your face ) What an ignorant little hotpot ( He who does not know is innocent ) What I find myself best at , Just give ...

  7. Python Learning notes 8 : File operations ( To continue ), File encoding and decoding , function , recursive , Introduction to functional programming , Higher order function

    File operations ( To continue ) Get the file handle location ,f.tell(), from 0 Start , Count by number of characters f.read(5), Read 5 Characters Returns a file handle to a location ,f.seek(0) Changing the encoding of a file during editing ,f.detech() ...

  8. python Learning higher order functions in functional programming

    Basic concepts Functional programming , It's a very abstract programming paradigm , Functions written in pure functional programming languages have no variables . therefore , Any function , Just type ok , This kind of function whose output is determined is called pure function , We say this function has no side effects . And allow the use of ...

  9. python Study 8— Higher order functions and built-in functions of functions

    python Study 8— Higher order functions and built-in functions of functions 1. Higher order function a. map() function Perform the operation specified by the first input parameter on the second input parameter .map() The return value of the function is an iterator , You can only iterate once , After iteration, it will be interpreted ...

Random recommendation

  1. digit DP GYM 100827 E Hill Number

    Topic link The question : Judgment is less than n In the number of , The number of digits that go up and down from high to low analysis : Simple digits DP, Save the first digit , Pay attention to the treatment of critical points , It's all routine. . #include <bits/stdc++. ...

  2. hdoj 2087 Cut the flower cloth

    Cut the flower cloth Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. RDIFramework.NET ━ .NET Framework of rapid information system development V3.3 New version released

    1.RDIFramework.NET The framework is introduced RDIFramework.NET, be based on .NET Rapid information system development . Integration Framework , It provides strong support for enterprises or individuals to develop their systems rapidly , Developers don't need to develop the basic functions and ...

  4. HTML5 API Share

    Facebook - http://developers.facebook.com/ Renren open platform - http://dev.renren.com/ 51.com Open platform - http://deve ...

  5. PHP Array encyclopedia

    One . The basic function of array operation Key names and values of arrays array_values($arr); Get the value of the array array_keys($arr); Get the key name of the array array_flip($arr); The values in the array are interchanged with the key names ( Such as ...

  6. IDEA+Maven:cannot download sources

    hold IDEA Of maven Switch to maven2

  7. Protocol Buffers Study the tutorial

    In the process of looking at the company code recently , See a lot of proto Postfix file , What kind of thing is this ? Asked the big guy , It turned out to be Protocol Buffers! What's this thing for ? I didn't know until I checked the information , It's an open source component pushed by Google boss again , This thing can completely replace ...

  8. platform + Plug in software design ideas and based on COM Prototype implementation of

    introduction : We are used to developing software on our own , Everyone uses their own style of programming , But as the project gets bigger or time is tight , Just a few people , A dozen people , Even hundreds of people collaborate on software development and Design , At this time a more thorny ...

  9. E470 There's no sound problem with the playback

    Download the sound card driver from the official website . And hotkey drive , Just install it ok 了

  10. The image processing ( The 21st ) Face cartoon animation generation based on data driven -Siggraph Asia 2014

    http://blog.csdn.net/garfielder007/article/details/50582018 in real life , We often evaluate a person , Beautiful or not . Is it a handsome girl , But how to use five ...