1 / 35

Hogere-orde functies: herhaald patroon? Parametrizeer!

Hogere-orde functies: herhaald patroon? Parametrizeer!. product :: [Int]  Int. 1. product [ ] = product (x:xs) =. product xs. x. *. and :: [Bool]  Bool. True. and [ ] = and (x:xs) =. and xs. x. &&. sum :: [Int]  Int. 0.

harper
Download Presentation

Hogere-orde functies: herhaald patroon? Parametrizeer!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hogere-orde functies:herhaald patroon? Parametrizeer! product :: [Int]  Int 1 product [ ] =product (x:xs) = product xs x * and :: [Bool]  Bool True and [ ] =and (x:xs) = and xs x && sum :: [Int]  Int 0 sum [ ] =sum (x:xs) = sum xs x +

  2. Universele lijst-loper: foldr foldr :: [a]  a (aaa)  a  foldr :: (abb) b  [a]  b combineer-functie start-waarde foldr (#) e [ ] =foldr (#) e (x:xs)= e foldr (#) e xs x #

  3. Partiële parametrizatie • foldr is een generalisatievan sum, product, en and .... • …dus sum, product, en andkun je schrijven met foldr product = foldr (*) 1 and = foldr (&&) True sum = foldr (+) 0 or = foldr (||) False

  4. Voorbeeld: sorteren (1/2) insert :: a  [a]  [a] insert e [ ] = [ e ] insert e (x:xs) | e  x = e : x : xs | e  x = x : insert e xs Ord a isort :: [a]  [a] isort [ ] = [ ] isort (x:xs) = insert x (isort xs) Ord a isort = foldr insert [ ]

  5. Voorbeeld: sorteren (2/2) qsort :: [a]  [a] qsort [ ] = [ ] qsort (x:xs) = qsort (filter (<x) xs) ++ [x] ++ qsort (filter (x) xs) Ord a (Waarom hebben ze me nooit eerder verteld dat ‘t zo makkelijk is?)

  6. Handige notaties(geleend uit de wiskunde) • Lambda abstractie • Lijst comprehensie Om naamloze functies te maken \x  x*x [ x*y | x  [1..10] , even x , y  [1..x] ] intuitiever dan combinatievan map , filter & concat

  7. Deel II Boomstructurenin Haskell

  8. 6 34 26 18 8 5 11 14 3 10 29 15 23 4 1 Binaire bomen met interne waarden Hoe codeer je datin Java/C++/C# enz?

  9. OO-aanpak van bomen class Tree { private Tree left, right; private int value; // constructor public Tree(Tree al, Tree ar, int av) { left = al; right=ar; value=av; } // bladeren gecodeerd met null }

  10. OO-aanpak van bomen:binaire bomen met externe data class Tree { // lege superclass} class Leaf extends Tree { int value } class Node extends Tree { Tree left,right }

  11. Haskell notatie: dataTree a = Leaf a | Node (Tree a) (Tree a) Functionele aanpak van bomen Tree a • Ik wil een polymorf type • En constructorfuncties Leaf :: a  Tree a Node :: Tree a  Tree a  Tree a

  12. definition declaration type Name type = typ type var constructor operator type type type Name constructor Name data = type var type context |

  13. Functies op bomen • Naar analogie van lijst-functies • schrijf je een functie op een boom: length :: [a]  Int length [ ] = 0 length (x:xs) = 1 + length xs size :: Tree a  Int size (Leaf v) = 1 size (Node lef rit) = size lef + size rit

  14. Uitdaging: schrijf boom-functies • elem kijkt of een waarde in een boom zit • front verzamelt alle waarden in een lijst elem :: a  Tree a  Bool elem x (Leaf y) = x==y elem x (Node lef rit) = elem x lef || elem x rit Eq a front :: Tree a [a] front (Leaf y) = [ y ] front (Node lef rit) = front lef ++ front rit

  15. Toepassing: expressies (3*a + 4*b) / c typeExpr = plus :: Expr  Expr  Expr maal :: Expr  Expr  Expr waarde :: [(String,Int)]  Expr  Int eenvoud :: Expr  Expr

  16. Expressies: poging 1 (3*a + 4*b) / c typeExpr = String 3+a 4+b * plus :: Expr  Expr  Expr waarde :: [(String,Int)]  Expr  Int plus a b = a ++ “+” ++ b waarde tab s = foldr ??? ??? s

  17. Expressies: poging 2 dataExpr = | | | | | Con Var Plus Min Maal Deel Int String Expr Expr Expr Expr Expr Expr Expr Expr constructorfuncties waarmee je een Expr bouwt types van de parametersdie daarbij nodig zijn

  18. Een datastructuur opschrijven • Lijst • Boom [ ] 1 : 2 : 3 : 4 : Tak 3 ( ) ( ) Tak 7 ( ) ( ) Tak 4 Blad Blad Tak 2 Blad Blad Tak 5 (Tak 1 Blad Blad ) (Blad )

  19. Een datastructuur opschrijven (3*a + 4*b) / c • Expr (Maal (Con 3) (Var “a”)) (Maal (Con 4) (Var “b”)) Deel ( ) ( Var “c” ) Plus

  20. Expressies: poging 3 dataExpr = | | | | | Constructorenbeginnen methoofdletter of dubbelepunt Int String Expr Expr Expr Expr Expr Expr Expr Expr Con Var :+: :-: :*: :/: constructorfuncties/operatoren waarmee je een Expr bouwt constructorfuncties waarmee je een Expr bouwt

  21. Een datastructuur opschrijven (3*a + 4*b) / c • Expr Con 3 :*: Var “a” Con 4 :*: Var “b” ( ) :/: ( Var “c” ) :+: infixl 6 (:+:), (:-:) infixl 7 (:*:), (:/:)

  22. Functies op Expressies plus :: Expr  Expr  Expr maal :: Expr  Expr  Expr waarde :: [(String,Int)]  Expr  Int eenvoud :: Expr  Expr plus = (:+:) maal = (:+:)

  23. Functies op Expressies wrd :: [(String,Int)]  Expr  Int wrd t (Con n) wrd t (Var v) wrd t (a:+:b) wrd t (a:-:b) wrd t (a:*:b) wrd t (a:/:b) = = = = = = n zoekop t v wrd t a + wrd t b wrd t a – wrd t b wrd t a * wrd t b wrd t a / wrd t b

  24. Functies op Expressies afg :: Expr  String  Expr “de afgeleide naar eenvariabele” afg (Con c) dx afg (Var v) dx = Con 0 | eqString v dx = Con 1 | otherwise = Con 0 afg (a:+:b) dx afg (a :-:b) dx = afg a dx :+: afg b dx = afg a dx :-: afg b dx

  25. Functies op Expressies afg :: Expr  String  Expr = a :*: afg b dx :+: b :*: afg a dx afg (a:*:b) dx afg (a :/:b) dx = ( b :*: afg a dx :-: a :*: afg b dx ) :/: (b :*: b)

  26. Twee soorten berekening • Numeriek • Symbolisch diff :: (FloatFloat)  (FloatFloat) diff f x = (f (x+h) – f x) / h where h = 0.00001 dat is nou AI! afg :: Expr  String  Expr afg (Con c) dx = Con 0 afg (Var v) dx | eqString v dx = Con 1 |otherwise = Con 0 afg (a:+:b) dx = afg a dx :+: afg b dx afg (a:*:b) dx = a :*: afg b dx :+: b :*: afg a dx

  27. Rekenen met Booleans (&&) :: Bool  Bool  Bool (||) :: Bool  Bool  Bool not :: Bool  Bool True && y = y False && y = False True || y = True False || y = y not True = False not False = True

  28. Symbolisch Propositiesbewerken dataProp = | | | | | Bool String Prop Prop Prop Prop Prop Prop Prop Con Var Not :/\ :\/ :-> constructorfuncties/operatoren waarmee je een Prop bouwt constructorfuncties waarmee je een Expr bouwt

  29. Functies op Proposities typeBedeling = [(String,Bool)] evalueer :: Bedeling  Prop  Bool > evalueer [ (“p”,True), (“q”, False) ] (Var “p” :/\ Var “q”) False

  30. Functies op Proposities tautologie :: Prop  Bool contradictie :: Prop  Bool > tautologie (Var “p” :\/ Not (Var “p”)) True > contradictie (Var “p” :-> Var “q” ) False

  31. Functies op Proposities equivalentie :: Prop  Prop  Bool vervulbaar :: Prop  [Bedeling] > vervulbaar (Var “p” :-> Var “q” ) [ [ (“p”,True) , (“q”,True) ] , [ (“p”,False) , (“q”, False) ] , [ (“p”,False) , (“q”, True) ] ]

  32. Functies op Proposities eenvoudig :: Prop  Prop tautologieën en contradictiesvervangen door constanten > eenvoudig Not(Not (Var “p” :/\ (Var “q” :\/ Not Var “q”)) Not (Not (Var “p” :/\ Con True)) Not (Not (Var “p”)) Var “p” operatoren met constanteparameters wegwerken dubbele Not wegwerken

  33. Functies op Proposities normaalvorm :: Prop  Prop > normaalvorm (Not (Not Var “p” :\/ Var “q” ) Var “p” :/\ Not Var “q” Not alleen voor variabelen

  34. Functies op Proposities toon :: Prop  String ontleed :: String  Prop > putStr (toon (Var “p” :-> Var “q”)) p -> q > ontleed “p && !p” Var “p” :/\ Not Var “p”

  35. Functies op Proposities evalueer :: Bedeling  Prop  Bool tautologie :: Prop  Bool contradictie :: Prop  Bool equivalentie :: Prop  Prop  Bool vervulbaar :: Prop  [Bedeling] eenvoudig :: Prop  Prop normaalvorm :: Prop  Prop toon :: Prop  String ontleed :: String  Prop

More Related