Hoger-ordefuncties op lijsten nDoorloop een lijst en... map :: (a b) [a] [b] filter :: (a Bool) [a] [a] foldr :: (a a a) a [a] a [a] doe dit pak deze combineer zo
Currying nType nAanroep f :: Int Bool String > f 7 True “hoi” f 7 Bool String rechts-associatief links-associatief zonder haakjes!
Zelftest nGegeven nSchrijf sum en sorteer m.b.v. foldr sum[ ]= 0 sum (x:xs)= x + sum xs sorteer [ ]= [ ] sorteer (x:xs)= insert x (sorteer xs) sumxs= foldr (+) 0 xs sorteerxs= foldr insert [ ] xs
Andere hogere-ordefuncties until :: (a Bool) (a a) a a begin hiermee doe dit totdat dit geldt > until ((<)1000) ((*)2) until p f x = | p x | True = x (f x) until p f
Andere hogere-ordefuncties (.) :: begin hiermee doe dit en dan dat > filter (not. even) [1, 2, 3, 4, 5, 6] [1, 3, 5] (.) g f x = g (f x) a (a b) (b c) c (a c)
Partieel parametriseren > map (plus 5) [1.. 5] [6, 7, 8, 9, 10] > until ((<)1000) ((*)2) > filter (not. even) [1.. 5] [1, 3, 5]
Partieel parametriseren > map f [1.. 4] where f x = x*x + 3*x + 2 [6, 12, 20, 30] > map f [1.. 4] [6, 12, 20, 30] ( \ x x*x+3*x+2 )
Lambda-expressies x*x + 3*x + 2 expressie waar x vrij in voorkomt \ x de functie die die expressie uitrekent
Lambda ? x. x*x + 3*x + 2 x. x*x + 3*x + 2 x. x*x + 3*x + 2 (LAMBDA x)( x*x + 3*x + 2 ) \ x x*x + 3*x + 2 \ x -> x*x + 3*x + 2
Lijsten maken nTwee fundamentele manieren [ ] :: [a] (:) :: a [a] [a] nDaarom ook twee patronen in definities f [ ] =... f (x:xs)=...
Lijsten maken: afkortingen nOpsomming nVan... tot en met... [ ]:: [Int] [ 10, 8,.. 0 ]:: [Int] [ 2.. ]:: [Int] [True, False, True]:: [Bool] [sin, cos, tan]:: [Float Float] [ [1,2,3], [1,2] ]:: [ [Int] ] oneindig!
Functies op lijsten > [4, 6, 1] ++ [2, 4] [4, 6, 1, 2, 4] (++) :: [a] [a] [a] [ ] ++ ys= ys (x:xs) ++ ys= x : (xs++ys)
Functies op lijsten > concat [ [3, 4, 5], [ ], [1, 2], [6] ] [3, 4, 5, 1, 2, 6] concat :: [[a]] [a] concat [ ]= [ ] concat (xs:xss)= xs ++ concat xss concat xss= foldr (++) [ ] xss
Functies op lijsten head[ ]= head(x:xs)= x tail(x:xs)= xs init(x:[ ])= [ ] init(x:xs)= x : init xs last(x:[ ])= x last(x:xs)= last xs
Functies op lijsten take, drop :: Int [a] [a] take 0 xs= [ ] take n [ ]= [ ] take n (x:xs)= x : take (n-1) xs drop 0 xs= xs drop n [ ]= [ ] drop n (x:xs)= drop (n-1) xs
Hoger-ordefuncties op lijsten nDoorloop een lijst en... map :: (a b) [a] [b] filter :: (a Bool) [a] [a] foldr :: (a a a) a [a] a [a] doe dit pak deze combineer zo
Het type van foldr sum[ ]= 0 sum (x:xs)= x + sum xs sum = foldr (+) 0 length [ ]= 0 length (x:xs)= 1 + length xs length = foldr f 0 where f elem rest = 1 + rest foldr :: (a a a) a [a] a b (a b b) b
Functies op lijsten > filter even [2, 4, 1, 6, 3, 8] [2, 4, 6, 8] filter :: (a Bool) [a] [a] filter p [ ] = [ ] filter p (x:xs)| p x = x : filter p xs | True = filter p xs
Functies op lijsten > takeWhile even [2, 4, 1, 6, 3, 8] [2, 4] takeWh :: (a Bool) [a] [a] takeWh p [ ] = [ ] takeWh p (x:xs) | p x = x : takeWh p xs | True = [ ]
Functies op lijsten > dropWhile even [2, 4, 1, 6, 3, 8] [1, 6, 3, 8] dropWh :: (a Bool) [a] [a] dropWh p [ ] = [ ] dropWh p (x:xs) | p x = dropWh p xs | True = x : xs
Lijsten vergelijken nLijsten zijn gelijk als uelementen gelijk uvolgorde gelijk eqListInt:: [Int] [Int] Bool eqListInt [ ][ ] = eqListInt [ ](y:ys) = eqListInt (x:xs)[ ] = eqListInt (x:xs)(y:ys) = True False x==y && eqListInt xs ys
Lijsten polymorf vergelijken nExtra functie meegeven eqList :: (a a Bool) [a] [a] Bool eqList eq [ ][ ] = eqList eq [ ](y:ys) = eqList eq (x:xs)[ ] = eqList eq (x:xs)(y:ys) = True False eq x y && eqList eq xs ys
Lijsten vergelijken > eqList (==) [1,2,3] [1,2,3] True > eqList (eqBool) [True,False] [True] False > eqList (eqList (==)) [[1,2], [3,4]] [[1,2], [4,3]] False
Samenvatting Functies op lijsten n[ ] (:) (++) concat nhead tail init last take drop nfilter takeWhile dropWhile nmap foldr nrepeat replicate iterate neqList ordList