This is a note to introduce the powerful Haskell library functions, and also to feel the similarities and differences with our ordinary JS operations:

id

Given any value, return the given value;

Prelude> id "myId"
"myId"

Prelude> :t id
id :: a -> a
Copy the code

const

Given two elements, return only the first;

Prelude> const 3 4
3

Prelude> :t const
const :: a -> b -> a
Copy the code

flip

Will return to flip;

Prelude> flip const 3 4
4

Prelude> :t flip
flip :: (a -> b -> c) -> b -> a -> c
Copy the code

error

Throw an exception;

Prelude> error "something error"
*** Exception: something error

Prelude> :t error
error :: [Char] -> a
Copy the code

undefined

Temporarily define functions that are not implemented, so that the compiler does not report errors;

Prelude> ac = undefined
Prelude> bc = ac+1

Prelude> :t undefined
undefined :: a
Copy the code

min/max

Compare size;

Prelude> min 5 6
5
Copy the code

!!!!!

Take the NTH +1 number in the list;

Prelude > [1, 2, 3]!!!!! 2, 3,Copy the code

reverse

Rewind the list elements;

Prelude > reverse [1, 2, 3], [3, 2, 1]Copy the code

head/last

Take the first or last element of the list;

Prelude> head "Hello"
'H'
Prelude> last "Hello"
'o'
Copy the code

init/tail

Remove the last or first element from the list;

Prelude > init,2,3,4,5,6,7,8,9 [1] [1.. 10] Prelude > tail [1.. 10],3,4,5,6,7,8,9,10 [2]Copy the code

map

Map to get a new list;

Prelude> map (+1) [1,2,3,4]
[2,3,4,5]
Copy the code

More complex mappings can be achieved with the help of the λ anonymous function:

Prelude > map (\ x - > x ^ 2 + 1) [1, 2, 3, 4],5,10,17 [2]Copy the code

filter

Filter function;

Prelude> filter (>=7) [9,6,4,2,10,3,15]
[9,10,15]
Copy the code

Two functions derived from the filter function to judge odd (odd) and even (even) :

Prelude> odd 4
False

Prelude> even 4
True
Copy the code

take/drop

The take function retrieves several elements of a list consecutively from scratch;

Prelude> take 3 [1,2,3,4,5]
[1,2,3]
Copy the code

Drop, as opposed to take, drops the first few elements of a list;

Prelude> drop 3 [1,2,3,4,5]
[4,5]
Copy the code

span/break

The SPAN function divides a list into tuples of two lists, from left to right, based on a condition, stopping when the first element that does not meet the condition is encountered;

Prelude> span odd [1,3,5,6,9]
([1,3,5],[6,9])
Copy the code

The break function is the opposite of the SPAN function. It stops from left to right according to a condition.

Prelude> break odd [1,3,5,6,9]
([],[1,3,5,6,9])
Copy the code

TakeWhile/dropWhile

The previous take and drop functions get or remove the first few elements in the list by giving an integer, while takeWhile and dropWhile require a condition to judge, and stop fetching or removing if the condition is not true.

Prelude > takeWhile (> 5),7,8,2,3,4 [6] [June] Prelude > dropWhile (> 5),7,8,2,3,4 [6] [4] 2Copy the code

splitAt

This function can split a list in any position;

Prelude> splitAt 5 "HelloWorld!" ("Hello","World!" )Copy the code

repeat/replicate

The repeat function can repeat an element in the list numerous times;

Replicate is a replication function. You can copy an element a given number of times.

Prelude> repeat True
[True,True,True,True,......

Prelude> replicate 5 True
[True,True,True,True,True]
Copy the code

In fact, we can implement a replicate function with the take and repeat functions:

Prelude> replicate1 n a = take n (repeat a)
Prelude> replicate1 5 False
[False,False,False,False,False]
Copy the code

any/all

Query a list for elements that match a given condition.

Prelude> any even [1,3,5]
False

Prelude> all odd [1,3,5]
True
Copy the code

and/or

And concatenates all Boolean values in a list with ampersand;

Or will put all the Boolean value with | | link;

Prelude> and [True,True,False]
False

Prelude> or [True,True,False]
True
Copy the code

elem/notElem

The elem function determines whether an element exists in a list;

Prelude> elem 1 [4,5,1]
True
Copy the code

NotElem is the negation of elem;

Prelude> notElem 1 [4,5,1]
False
Copy the code

iterate

Iterative function;

Prelude > iterate (* 2) 1 (1,2,4,8,16,32,64,128,256,512,1024,2048...Copy the code

until

The function until iteratively generates data until a given condition is met;

Prelude> until(>500)(*2)1
512
Copy the code

zip

The zip function can combine two lists into a single tuple list;

Prelude > zip [True, False, True, False],4,5,6,7 [2] [(True, 2), (False, 4), (True, 5), (False, 6)]Copy the code

unzip

Unzip, the reverse of zip, is a function that splits a list of binary elements into two list elements.

Prelude > unzip [(True, 2), (False, 4), (True, 5), (False, 6)] ([True, False, True, False], [2,4,5,6])Copy the code

concat

The concat function concatenates lists within a list;

Prelude > concat [[1, 2], [3, 4]] [1, 2, 3, 4]Copy the code

concatMap

The map function evaluates [a] to a result of type [[b], and uses concat to get a result of type [b].

Prelude > map (3) replicate [1, 2, 3] [,1,1 [1], [2,2,2], [3 filling]]Copy the code