Functioneel programmeren
Post on 02-Jan-2016
56 Views
Preview:
DESCRIPTION
Transcript
Functioneel programmeren
Een snelle herhaling…
Functie-definitie
static int kwad (int x){ return x*x ;}
kwad x = x * x Haskell
kwad :: Int Int
Functie-definitie
static int fac (int n){ int tel, res; res = 1; for (tel=1; tel<=n; tel++) res *= tel; return res;}
fac n = product [1..n] Haskell
fac :: Int Int
Functies als parameter
Pas functie toe op alleelementen van een lijst
map
> map fac [1, 2, 3, 4, 5][1, 2, 6, 24, 120]
> map sqrt [1.0, 2.0, 3.0, 4.0][1.0, 1.41421, 1.73205, 2.0]
> map even [1 .. 6][False, True, False, True, False, True]
Herhalen
fac :: Int Int
fac n = 1=
| n==0| n>0
“recursie”
n * fac (n-1)
graag zonder product te gebruiken
Partieel parametriseren
Stel:
En dan:
plus :: Int Int Intplus x y = x + y
drieMeer :: Int Int drieMeer = plus 3
> map drieMeer [1, 2, 3, 4, 5][4, 5, 6, 7, 8]
Definitie van map
Geef een recursieve definitie:
map :: (ab) [a] [b]
map f [ ] =map f (x:xs)=
[ ]map f xs
f x
:
Een ander soort lijst-functies
product :: [Int] Int product [ ] =product (x:xs)
=
1product xsx *
and :: [Bool] Bool and [ ] =and (x:xs)=
Trueand xsx
&&sum :: [Int] Int sum [ ] =sum (x:xs)
=
0sum xsx +
Universele “lijst-totalisator”
foldr :: [a] a
foldr (#) e [ ] =foldr (#) e (x:xs)=
efoldr (#) e xsx #
(aaa) a
zo combineren neutrale waarde
Universele “lijst-totalisator”
foldr :: [a] b
foldr (#) e [ ] =foldr (#) e (x:xs)=
efoldr (#) e xsx #
(abb) b
zo combineren neutrale waarde
Had dat eerder gezegd...
Als foldr de generalisatie isvan sum, product, en and ....
.... dan zijn sum, product, en andspeciale gevallen van foldr
product = foldr (*) 1and = foldr (&&) Truesum = foldr (+) 0or = foldr (||) False
Hoger-ordefuncties op lijsten
Doorloop een lijst en ...
map :: (ab) [a] [b]
filter :: (aBool) [a] [a]
foldr :: (abb) b [a] b
[a]
[a]
[a]
doe dit
pak deze
combineer zo
Anonieme functies
> 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 waarx vrij in voorkomt
\ x
de functie die dieexpressie uitrekent
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
Oneindige lijsten
repeat :: a [a]repeat x = x : repeat x
> repeat 3[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
replicate :: Int a [a]replicate n x = take n (repeat x)
> concat (replicate 5 ”info ” )”info info info info info ”
Lazy evaluation
Parameters worden pas uitgerekendals ze echt nodig zijn
Geldt ook voor (:)dus alleen deel van een lijstdat echt nodig iswordt uitgerekend
Oneindige lijsten
iterate :: (aa) a [a]iterate f x = x : iterate f (f x)
> iterate ((+)1) 3[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
Lijst-comprehensies
Notatie uit de verzamelingenleer
{ x*x | x N, x<10 }
> [ x*x | x [1..10], even x ][4, 16, 36, 64, 100]
> [ (x,y) | x [1,4,8], y ”ab” ][ (1,’a’), (1,’b’), (4,’a’), (4,’b’), (8,’a’), (8,’b’) ]
Lijst-comprehensies
Speciale notatie
betekent hetzelfde alsmaar leest lekkerder dan
[ expressie | x lijst , predicaat ]
map (\xexpressie ) (filter (\xpredicaat) lijst )
Zelf datastructuren ontwerpen
data Boom a = Blad | Tak a (Boom a) (Boom a)
constructorfuncties
het nieuwetype
types van de parameters van de constructorfuncties
Opnieuw de lijst uitvinden
data Lijst a = Nil | Cons a (Lijst a)
constructorfuncties
het nieuwetype
types van de parameters van de constructorfuncties
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 ( ) ( )
Functies op datastructuren
omvang
diepte
omvang :: Boom a Intomvang Blad = 0omvang (Tak x li re)
= 1 + omvang li + omvang re
diepte :: Boom a Intdiepte Blad = 0diepte (Tak x li re) = 1 + max (diepte li) (diepte re)
top related