series

  • Basic grammar for notes in the Haskell Guide to Fun Learning
  • Notes in the Haskell Guide to Fun Learning
  • Function of notes in the Haskell Guide to Fun Learning
  • Haskell’s Guide to Fun Learning notes of higher order functions
  • Module of notes in the Haskell Guide to Fun Learning
  • Custom types of notes in the Haskell Guide to Fun Learning
  • I/O notes from the Haskell Guide to Fun Learning

A function that satisfies any of the following conditions is a higher-order function

  1. Accept functions as arguments
  2. Take the function as the return value

“The Nature of Computing” is a better book, and I feel like it’s not quite there.

Currie,

It is sufficient to make the function take only one argument. If you want to take two arguments, take one and return a function that takes the other.

multThree: :Int -> Int -> Int -> Int -- Equivalent to Int -> (Int -> (Int -> Int))
multThree x y z = x * y * z 
Copy the code

Section

divideByTen: : (Floating a) => a -> a 
divideByTen = (/10) That's truncation
isUpperAlphanum: :Char -> Bool
isUpperAlphanum = (`elem` ['A'..' Z']) That's truncation
Copy the code

One thing to note is that (-4) does not represent truncation and must be replaced with (‘ subtract ‘4)

Function as argument

applyTwice :: (a -> a) -> a -> a Note that the parentheses cannot be omitted
applyTwice f x = f (f x)
Copy the code

Note that the parentheses cannot be omitted because -> defaults to right combination.

When writing a function (especially a higher-order function), if you are not sure about the type of the function, you can not write the type declaration of the function first, and then use it after the definition :t Check the result derived by Haskell.

The toolkit

ghci> map (++ "!") ["BIFF"." BANG"." POW"]
["BIFF!"." BANG!"." POW!"]
ghci> filter even [1.. 10] 
[2.4.6.8.10]
ghci> let listOfFuns = map (*) [0..] 
ghci> (listOfFuns !! 4) 5 
20
Copy the code

lambda

Lambda is a one-time anonymous function. This is written as \param1 param2 -> returnValue, enclosed in parentheses if necessary.

How to declare a lambda if the argument is a binary:

ghci> map (\(a, b) -> a + b) [(1.2), (3.5)] Note that (a,b) is a pattern match, not two parameters
[3.8]
Copy the code

The following example shows that Haskell functions are currified by default

addThree: :Int -> Int -> Int -> Int 
addThree x y z = x + y + z 

addThree': :Int -> Int -> Int -> Int 
addThree' = \x -> \y -> \z -> x + y + z

These two functions are equivalent

flip1 :: (a -> b -> c) -> b -> a -> c 
flip1 f y x = f x y

flip2 :: (a -> b -> c) -> b -> a -> c 
flip2 f = \y x -> f x y

These two functions are equivalent
Copy the code

Fold folding (similar to Reduce)

foldl (\acc x -> acc + x) init list
foldr (\x acc -> x : acc) init list
foldl1 step list The first element is init
foldr1 step list The first element is init
Copy the code

Another way to think about folding: folding is to apply a function continuously to all the elements in a list. Such as:

foldr f 0 [1.2.3.4] 
-- in fact
f 1 (f 2 (f 3 (f 4 0)))

foldl g 0 [1.2.3.4]
-- in fact
g (g (g (g 0 1) 2) 3) 4
Copy the code

Infinite lists can be handled with FOLdr when the binary function f does not always need to evaluate the second argument. Fordl does not.

Scan is similar to fold, except that scan records the return values of each step in a list.

The $function applies the symbol

After that are the arguments to the function, similar to JS’s.call.

sum $ filter (> 10) $ map (*2) [2.. 10] - 80
sum (filter (> 10) (map (*2) [2.. 10])) If you don't use $, you add a lot of parentheses

ghci> map ($ 3) [(4+), (10*), (^2), sqrt]
[7. 0.30. 0.9. 0.1. 7320508075688772]
Copy the code

Composition of functions

Mathematical definition :(f·g)(x) = f(g(x)), its purpose is to facilitate the generation of new functions.

map (negate . sum . tail) [[1.. 5], [3.. 6], [1.. 7]] 
- / - 14, 15, 27
Copy the code

If the function to be combined has more than one argument, it can only be combined as a function with one argument (Currization).

sum . replicate 5 $ max 6.7 8.9 --44.5
- equivalent to the
sum . (replicate 5) $ max 6.7 8.9 - 44.5
Copy the code

Tip: If you want to use composition to eliminate a lot of parentheses, find the innermost function and argument first, write it down, and add one in frontEarlier, such as

replicate 2 (product (map (*3) (zipWith max [1.2] [4.5)))Change the form -
replicate 2 . product . map (*3) $ zipWith max [1.2] [4.5]
Copy the code

Point – free style

If you see something in the function definition

fn x = fn2 p1 x

So you can just simplify it to

fn = fn2 p1

This is also discussed in The Nature of Computing.

The nice thing about this is that it’s simple.