even 1
False
even 2
True
map even [1,2,3,4,5]
[False,True,False,True,False]
filter even [1,2,3,4,5]
[2,4]
even
is a first-order function.
map
and filter
are higher-order functions.
map toUpper "Hello!" == "HELLO!" map (*3) [1,2,3,4] == [3,6,9,12] map (take 2) ["Hallo","Haskell"] == ["Ha","Ha"] filter (not . isSpace) "bla bla \n bla" == "blablabla" map (\x->x*x) [1,2,3,4] == [1,4,9,16]
map :: (a -> b) -> [a] -> [b] filter :: (a -> Bool) -> [a] -> [a]
map f xs = [f x | x<-xs] filter p xs = [x | x<-xs, p x]
map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x:xs) = f x:map f xs
filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) | p x = x:filter p xs | otherwise = filter p xs
foldr
foldr (+) 0 [5,6,7] == 5+6+7+0 == 18
foldr (&) u
&
:
&
[]
u
xs == a : b : c : d : … : []
foldr (&) u xs == a & b & c & d & … & u
foldr (+) 0 xs == a + b + c + d + … + 0
foldr (*) 1 xs == a * b * c * d * … * 1
foldr (++) [] xs == a ++ b ++ c ++ d ++ … ++ []
foldr f z [1,2,3,4,5]
foldr f z (1:2:3:4:5:[])
foldr
foldr op u [] = u foldr op u (x:xs) = x `op` foldr op u xs
foldr
foldr :: (a->b->b) -> b -> [a] -> b
f1 xs = foldr (:) [] xs
f2 xs ys = foldr (:) ys xs
f3 f xs = foldr ((:) . f) [] xs
takeLine :: String -> String
takeLine "abc\ndef\nghi\n" = "abc"
takeWhile :: (a->Bool) -> [a] -> [a]
dropWhile :: (a->Bool) -> [a] -> [a]
lines :: String -> [String]
lines "abc\ndef\nghi\n" = ["abc","def","ghi"]
segments :: (a->Bool) -> [a] -> [[a]]
words :: String -> [String]
words "abc def ghi" = ["abc","def","ghi"]
zip [4,5,6] [100,10,1] == [(4,100),(5,10),(6,1)]
zip :: [a] -> [b] -> [(a,b)]
zip
outputs pairs, but we often want do do something with
more the pairs
zipWith :: (a->b->c) -> [a] -> [b] -> [c]
zipWith
scalarProduct xs ys = sum (zipWith (*) xs ys)
scalarProduct [4,5,6] [100,10,1]
==
sum (zipWith (*) [4,5,6] [100,10,1]) == sum [400,50,6] == 456
zipWith :: (a->b->c) -> [a] -> [b] -> [c] zipWith3 :: (a->b->c->d) -> [a] -> [b] -> [c] -> [d] zipWith4 :: (a->b->c->d->e) -> [a] -> [b] -> [c] -> [d] -> [e]
zipWith5
zipWith6
zipWith7
zipWith0 :: a -> [a] zipWith1 :: (a->b) -> [a] -> [b] zipWith :: (a->b->c) -> [a] -> [b] -> [c] zipWith3 :: (a->b->c->d) -> [a] -> [b] -> [c] -> [d] zipWith4 :: (a->b->c->d->e) -> [a] -> [b] -> [c] -> [d] -> [e]
zipWith5
zipWith6
zipWith7
zipWith0
zipWith1
zipWith1
map
zipWith0
repeat
removeSpaces "abc def \n ghi" == "abcdefghi"
isSpace
from module Data.Char
,
removeSpaces s = filter (not . isSpace) s
(f . g) x = f (g x)
(.)
(b->c) -> (a->b) -> (a->c)
f x = f1 (f2 (f3 (f4 (f5 x))))
f x = (f1 . f2 . f3 . f4 . f5) x
f x = g x
f = g
or = foldr (||) False
and = foldr (&&) True
concat = foldr (++) []
removeSpaces = filter (not . isSpace)
unlines = foldr (\xs ys->xs++"\n"++ys) []
f = f1 . f2 . f3 . f4 . f5
mm f xs = map (map f) xs zwzw f xs ys = zipWith (zipWith f) xs ys
mm = map . map zwzw = zipWith . zipWith
Int -> Int -> Int
Int -> (Int -> Int)
(Int -> Int) -> Int
Int -> Int -> Int
Int -> (Int -> Int)
(b->c)->(a->b)->(a->c)
(b->c)->(a->b)-> a->c
Int -> Int -> Int
(Int,Int) -> Int
Int
Int
|
|
curry :: ((a,b)->c) -> (a->b->c) curry f x y = f (x,y) uncurry :: (a->b->c) -> ((a,b)->c) uncurry f (x,y) = f x y
sort :: Ord a => [a] -> [a] sortBy :: (a->a->Ordering) -> [a] -> [a]
\x -> x*x
==
\y -> y*y
(\x -> x*x) 5
==
5*5
\x -> f x
==
f
pick 1 = fst pick 2 = snd
id :: a -> a