Kolommen uniformeren
description
Transcript of Kolommen uniformeren
Kolommen uniformeren
jetzuswim
miesnootaap
schapenhokweide
unikol =
Kolommen uniformeren
jetzuswim
miesnootaap
schapenhokweide
unikol = wissel . map uniform . wissel
Wissel
[1, 2] [3, 4] [5, 6] [7, 8]
[1, 3, 5, 7] [2, 4, 6, 8]
[3, 5, 7] [4, 6, 8][1, 2]
wissel ( xs : xss )
= (wissel xss)xs
zipWith (:)
Aanloop
1 2 3 4
1
aanloop ( x : xs )
= (aanloop xs) xmap ((:) )
[ ] [1,2,3,4][1,2,3][1,2][1]
[ ] [2,3,4][2,3][2]
[ ] :
:: [a] [[a]]
segment
1 2 3 4
1
segment ( x : xs )
= (segment xs)++ aanloop (x:xs)
[ ]
[1,2,3,4][1,2,3][1,2][1]
tail( )
:: [a] [[a]]
[2,3,4][2,3][2][3,4][3][4]
[ ] [2,3,4][2,3][2][3,4][3][4]
deelrij
1 2 3 4
1
deelrij ( x : xs )
= (deelrij xs)
[ ]
[1,2,3,4][1,2,3][1,2][1]
(deelrij xs)
:: [a] [[a]]
[2,3,4][2,3][2][3,4][3][4] [2,4]
[1,4] [1,3] [1,3,4] [1,2,4]
[ ] [2,3,4][2,3][2][3,4][3][4] [2,4]
xmap ((:) )++
> kies 2
kies
1 2 3 4
kies 0 xs =kies n [ ] =kies n (x:xs)=
:: Int [a] [[a]]
[ [ ] ][ ]map ((:)x)
[2,3] [3,4][2,4][1,2] [1,3] [1,4]
(kies (n-1) xs)++ kies n xs
Hogere-ordefuncties zien
kwadraten [ ] = [ ]kwadraten (x:xs) = x*x : kwadraten xs
kwadraten = map (\ x x*x)
totaal [ ] = 0totaal (x:xs) = x + totaal xs
totaal = foldr (+) 0
Hogere-ordefuncties zien
concat [ ] = [ ]concat (xs:xss) = xs ++ concat xss
concat =
length [ ] = 0length (x:xs) = 1 + length xs
length =
foldr (++) [ ]
foldr (\ e r 1+r) 0
Hogere-ordefuncties zien
sorteer [ ] = [ ]sorteer (x:xs)= insert x (sorteer xs)
sorteer =
wissel [ ] = repeat [ ]wissel (xs:xss) = zipWith (:) xs (wissel xss)
wissel =
foldr insert [ ]
foldr (zipWith (:)) (repeat [ ])
Toepassing: Breuken
type Breuk = (Int, Int)
3
4
maal :: Breuk Breuk Breukplus :: Breuk Breuk Breukeq :: Breuk Breuk Booleenvoud :: Breuk Breuk
plus (x,y) (p,q) = (x*q+y*p, y*q)
Toepassing: Polynomen
type Term = (Float, Int)type Poly = [Term]
3.5x4 + 6.1x2 + 2.5
maal :: Poly Poly Polyplus :: Poly Poly Polyeq :: Poly Poly Booleenvoud :: Poly Poly
plus p q = eenvoud (p ++ q)
Polynomen vermenigvuldigen
Alle termen met elkaar vermenigvuldigen
* =
maal :: Poly Poly Polymaal p q = eenvoud (cross tMaal p q)
3x + 1 2x2 + 5 6x3+15x + 2x2+5
tMaal :: Term Term TermtMaal (c1,e1) (c2,e2) = (c1*.c2, e1+e2)
Kruisproduct
Met lijstcomprehensie
Met expliciete recursie
cross :: (aab) [a] [a] [b]cross f xs ys = [ f x y | x xs, yys ]
cross f [ ] ys =cross f (x:xs) ys =
[ ]map (f x) ys++cross f xs ys
Toepassing: expressies
type Expr =
(3*a + 4*b) / c
plus :: Expr Expr Exprmaal :: Expr Expr Exprwaarde :: [(String,Int)] Expr Inteenvoud :: Expr Expr
Expressies: poging 1
type Expr =
(3*a + 4*b) / c
String
maal :: Expr Expr Expr
waarde :: [(String,Int)] Expr Intmaal a b = a ++ “*” ++ b
waarde tab s = foldr ??? ??? s
3+a 4+b*
Expressies: poging 2
data Expr=|||||
constructorfunctieswaarmee je een Expr bouwt
ConVarPlusMinMaalDeel
IntStringExpr ExprExpr ExprExpr ExprExpr Expr
types van de parametersdie daarbij nodig zijn
Een datastructuur opschrijven
Lijst
Boom
[ ]4 :3 :2 :1 :
BladTak 2 BladTak 4 Blad BladTak 7 ( )
( )Tak 5 (Tak 1 Blad Blad ) (Blad )
Tak 3 ( ) ( )
Een datastructuur opschrijven
Expr
(Maal (Con 3) (Var “a”)) (Maal (Con 4) (Var “b”))
(3*a + 4*b) / c
PlusDeel(
) ( Var “c” )
Expressies: poging 3
data Expr=|||||
constructorfunctieswaarmee je een Expr bouwt
Int String
Expr ExprExpr ExprExpr ExprExpr Expr
ConVar:+::-::*::/:
Constructorenbeginnen methoofdletter
of dubbelepunt
constructorfuncties/operatorenwaarmee je een Expr bouwt
Een datastructuur opschrijven
Expr
Con 3 :*: Var “a” Con 4 :*: Var “b”
(3*a + 4*b) / c
:+:(
) :/: ( Var “c” )
infixl 6 (:+:), (:-:)infixl 7 (:*:), (:/:)
Functies op Expressies
plus :: Expr Expr Expr
maal :: Expr Expr Expr
waarde :: [(String,Int)] Expr Inteenvoud :: Expr Expr
plus = (:+:)
maal = (:*:)
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)
======
nzoekop t vwrd t a + wrd t bwrd t a – wrd t bwrd t a * wrd t bwrd t a / wrd t b
Functies op Expressies
afg :: Expr String Expr
afg (Con c) dxafg (Var v) dx
= Con 0
| eqString v dx = Con 1| otherwise = Con 0
afg (a:+:b) dxafg (a :-:b) dx
= afg a dx :+: afg b dx= afg a dx :-: afg b dx
“de afgeleidenaar een
variabele”
Functies op Expressies
afg :: Expr String Expr
afg (a:*:b) dx
afg (a :/:b) dx
= a :*: afg b dx :+: b :*: afg a dx
= ( b :*: afg a dx :-: a :*: afg b dx ) :/: (b :*: b)
Twee soorten berekening
Numeriek
Symbolisch
diff :: (FloatFloat) (FloatFloat)diff f x = (f (x+h) – f x) / h where h = 0.00001
afg :: Expr String Exprafg (Con c) dx = Con 0afg (Var v) dx | eqString v dx = Con 1
|otherwise = Con 0afg (a:+:b) dx = afg a dx :+: afg b dxafg (a:*:b) dx = a :*: afg b dx :+: b :*: afg a dx
dat is nou AI!
Rekenen met Booleans
(&&) :: Bool Bool Bool
(||) :: Bool Bool Bool
not :: Bool Bool
True && y = yFalse && y = False
True || y = TrueFalse || y = y
not True = Falsenot False = True
Symbolisch Propositiesbewerken
data Prop=|||||
constructorfunctieswaarmee je een Expr bouwt
Bool String Prop
Prop PropProp PropProp Prop
ConVarNot:/\:\/:->
constructorfuncties/operatorenwaarmee je een Prop bouwt
Functies op Proposities
type Bedeling = [(String,Bool)]evalueer :: Bedeling Prop Bool
> evalueer [ (“p”,True), (“q”, False) ](Var “p” :/\ Var “q”)
False
Functies op Proposities
tautologie :: Prop Boolcontradictie :: Prop Bool
> tautologie (Var “p” :\/ Not (Var “p”))True
> contradictie (Var “p” :-> Var “q” )False
Functies op Proposities
equivalentie :: Prop Prop Boolvervulbaar :: Prop [Bedeling]
> vervulbaar (Var “p” :-> Var “q” )[ [ (“p”,True) , (“q”,True) ], [ (“p”,False) , (“q”, False) ], [ (“p”,False) , (“q”, True) ] ]
Functies op Proposities
eenvoudig :: Prop Prop
> eenvoudig Not(Not (Var “p” :/\ (Var “q” :\/ Not Var “q”))
Not (Not (Var “p” :/\ Con True))Not (Not (Var “p”))Var “p”
tautologieën en contradictiesvervangen door constanten
operatoren met constanteparameters wegwerken
dubbele Notwegwerken
Functies op Proposities
normaalvorm :: Prop Prop
> normaalvorm (Not (Not Var “p” :\/ Var “q” )
Var “p” :/\ Not Var “q”
Not alleen voor variabelen
Functies op Proposities
toon :: Prop Stringontleed :: String Prop
> putStr (toon (Var “p” :-> Var “q”))p -> q
> ontleed “p && !p”Var “p” :/\ Not Var “p”
Functies op Proposities
evalueer :: Bedeling Prop Booltautologie :: Prop Boolcontradictie :: Prop Boolequivalentie :: Prop Prop Boolvervulbaar :: Prop [Bedeling]eenvoudig :: Prop Propnormaalvorm:: Prop Proptoon :: Prop Stringontleed :: String Prop