De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

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.

Verwante presentaties


Presentatie over: "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."— Transcript van de presentatie:

1 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

2 Currying nType nAanroep f :: Int  Bool  String > f 7 True “hoi” f 7 Bool  String  rechts-associatief links-associatief zonder haakjes!

3 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

4 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

5 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)

6 Partieel parametriseren > map (plus 5) [1.. 5] [6, 7, 8, 9, 10] > until ((<)1000) ((*)2) > filter (not. even) [1.. 5] [1, 3, 5]

7 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 )

8 Lambda-expressies x*x + 3*x + 2 expressie waar x vrij in voorkomt \ x  de functie die die expressie uitrekent

9 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

10 Lijsten maken nTwee fundamentele manieren [ ] :: [a] (:) :: a  [a]  [a] nDaarom ook twee patronen in definities f [ ] =... f (x:xs)=...

11 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!

12 Functies op lijsten > [4, 6, 1] ++ [2, 4] [4, 6, 1, 2, 4] (++) :: [a]  [a]  [a] [ ] ++ ys= ys (x:xs) ++ ys= x : (xs++ys)

13 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

14 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

15 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

16 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

17 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

18 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

19 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 = [ ]

20 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

21 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

22 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

23 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

24 Samenvatting Functies op lijsten n[ ] (:) (++) concat nhead tail init last take drop nfilter takeWhile dropWhile nmap foldr nrepeat replicate iterate neqList ordList


Download ppt "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."

Verwante presentaties


Ads door Google