-
Header:
flip :: (a -> b -> c) -> (b -> a -> c)
-
Description:
flip f
returns thef
function but with its two parameters inverted. It is defined byflip f x y = f y x
-
Examples:
half = flip div 2 half 10 π 5
-
Header:
(.) :: (b -> c) -> (a -> b) -> a -> c
-
Description:
f . g
is the composition of functionsf
ig
. -
Examples:
bigger3 = take 3 . reverse . sort bigger3 [3, 1, 2, 6, 7] π [7, 6, 3]
Β Β ```
-
Header:
($) :: (a -> b) -> a -> b
-
Description:
f $ x
is the same asf x
. Looks like unnecessay, but because of its low priority, it can save us from closing lots of parameters! -
Examples:
tail (tail (tail (tail "Jordi"))) π "i" tail $ tail $ tail $ tail "Jordi" π "i"
-
Header:
map :: (a -> b) -> [a] -> [b]
-
Description:
map f xs
is the list obtained when applyingf
to each element of thexs
list, so thatmap f [x1, x2, ..., xn]
Γ©s[f x1, f x2, ..., f xn]
. -
Examples:
map even [2, 4, 6, 7] π [True, True, True, False] map (*2) [2, 4, 6, 7] π [4, 8, 12, 14]
-
Header:
filter :: (a -> Bool) -> [a] -> [a]
-
Description:
filter p xs
is the sublist of elements fromxs
that satisfy thep
predicate. -
Examples:
filter even [2, 1, 4, 6, 7] π [2, 4, 6]
-
Header:
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
-
Description:
zipWith op xs ys
is the list obtained operating each element ofxs
with each elements ofys
via theop
function, from left to right, as long as there are elements. -
Examples:
zipWith (+) [1, 2, 3] [5, 1, 8, 9] π [6, 3, 11]
-
Header:
all :: (a -> Bool) -> [a] -> Bool
-
Description:
all p xs
tells whether all elements inxs
satisfy thep
predicate. -
Examples:
all even [2, 1, 4, 6, 7] π False all even [2, 4, 6] π True
-
Header:
any :: (a -> Bool) -> [a] -> Bool
-
Description:
any p xs
tells whether some element inxs
satisfies thep
predicate. -
Examples:
any even [2, 1, 4, 6, 7] π True all odd [2, 4, 6] π False
-
Header:
dropWhile :: (a -> Bool) -> [a] -> [a]
-
Description:
dropWhile p xs
Γ©s la subllista dexs
que elimina els primers elements dexs
que compleixen el predicatp
(fins al final o al primer qua no la compleix). -
Examples:
dropWhile even [2, 4, 6, 7, 8] π [7, 8] dropWhile even [2, 4] π []
-
Header:
takeWhile :: (a -> Bool) -> [a] -> [a]
-
Description:
takeWhile p xs
is the sublist ofxs
that contains the first elements ofxs
that satisfy thep
predicate. -
Examples:
takeWhile even [2, 4, 6, 7, 8] π [2, 4, 6] takeWhile even [1, 3] π []
-
Header:
iterate :: (a -> a) -> a -> [a]
-
Description:
iterate f x
returns the infinite list[x, f x, f (f x), f (f (f x)), ...]
. -
Examples:
iterate (*2) 1 π [1, 2, 4, 8, 16, ...]
-
Header:
foldl :: (b -> a -> b) -> b -> [a] -> b
-
Description:
foldl β x0 xs
folds an operator β from the left, so thatfoldl β x0 [x1, x2, ..., xn]
Γ©s(((x0 β x1) β x2) β ...) β xn
. -
Examples:
foldl (+) 0 [3, 2, (-1)] π 4
-
Header:
foldr :: (a -> b -> b) -> b -> [a] -> b
-
Description:
foldr β x0 xs
folds an operator β from the right, so thatfoldr β x0 [x1, x2, ..., xn]
Γ©sx1 β (x2 ... β (xn β x0)))
. -
Examples:
foldr (+) 0 [3, 2, (-1)] π 4
-
Header:
scanl :: (b -> a -> b) -> b -> [a] -> [b]
-
Description:
scanl f z xs
is just likefoldl f z xs
but rather than returning the final results, it returns the list with all the partial results. -
Examples:
scanl (+) 0 [3, 2, (-1)] π [0, 3, 5, 4]
-
Header:
scanr :: (a -> b -> b) -> b -> [a] -> [b]
-
Description:
scanr f z xs
is just likefoldr f z xs
but rather than returning the final results, it returns the list with all the partial results. -
Examples:
scanr (+) 0 [3, 2, (-1)] π [4, 1, -1, 0]
-
Header:
const :: a -> b -> a
-
Description:
Β Β const x
is the function tha always returns x
, independently of what is applied to it.
-
Examples:
map (const 42) [1 .. 5] π [42, 42, 42, 42, 42]
-
Header:
id :: a -> a
-
Description:
Β Β id
Γ©s the identity function. Looks unnecessary but may be useful at some point.
-
Examples:
map id [1 .. 5] π [1, 2, 3, 4, 5]
@felixarpa: Arreglat, grΓ cies! π