Академический Документы
Профессиональный Документы
Культура Документы
, a
Traian Florin S, erbanut
FMI, UNIBUC
Departamentul de Informatica,
traian.serbanuta@fmi.unibuc.ro
30 octombrie 2015
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
1 / 22
Orice!
data
data
data
data
data
data
data
data
data
data
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
2 / 22
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
3 / 22
, ii s, i a reprezentarii
Definirea egalitat
Eq s, i Show
Eq
eqBool
eqBool
eqBool
eqBool
Show
showBool : : Bool > S t r i n g
showBool False = " False "
showBool True = " True "
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
4 / 22
Anotimpuri
Anotimpuri
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
5 / 22
Anotimpuri
, ii s, i a reprezentarii
Definirea egalitat
Eq s, i Show
eqSeason
eqSeason
eqSeason
eqSeason
eqSeason
eqSeason
showSeason
showSeason
showSeason
showSeason
showSeason
: : Season > S t r i n g
Spring = " Spring "
Summer = "Summer"
Autumn = " Autumn "
Winter = " Winter "
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
6 / 22
Anotimpuri
s, i indici
Enumerari
data Season = W i n t e r | S p r i n g | Summer | F a l l
t o I n t : : Season > I n t
t o I n t Winter = 0
t o I n t Spring = 1
t o I n t Summer = 2
toInt Fall
= 3
fromInt
fromInt
fromInt
fromInt
fromInt
: : I n t > Season
0 = Winter
1 = Spring
2 = Summer
3 = Fall
30 octombrie 2015
7 / 22
Forme geometrice
Cercuri s, i dreptunghiuri
type
type
type
Radius
Width
Height
=
=
=
Float
Float
Float
data
Shape
=
|
C i r c l e Radius
Rect Width H e i g h t
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
8 / 22
Forme geometrice
, ii s, i a reprezentarii
Definirea egalitat
Eq s, i Show
eqShape
eqShape
eqShape
eqShape
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
9 / 22
Forme geometrice
30 octombrie 2015
10 / 22
Forme geometrice
Pattern-matching
area : : Shape > F l o a t
area ( C i r c l e r ) = p i * r ^2
area ( Rect w h ) = w * h
area : : Shape > F l o a t
area s =
i f i s C i r c l e s then
let
r = radius s
in
p i * r ^2
else i f i s R e c t s then
let
w = width s
h = height s
in
w * h
else e r r o r " i m p o s s i b l e "
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
11 / 22
Liste
Pattern-matching
List a
= Nil
| Cons a ( L i s t a )
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
12 / 22
Liste
Constructori simboluri
Declarat, ie ca tip de date algebric cu simboluri
= Nil
| a : : : List a
d e r i v i n g (Show)
infixr 5 : : :
data
List a
30 octombrie 2015
13 / 22
Liste
, ii s, i a reprezentarii
Definirea egalitat
Eq s, i Show
eqList
eqList
eqList
eqList
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
14 / 22
Numere naturale
Nat
=
|
Zero
Succ Nat
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
15 / 22
Numere naturale
30 octombrie 2015
16 / 22
Numere naturale
Date personale
type
type
type
type
type
type
FirstName = S t r i n g
LastName
= String
Age
= Int
Height
= Float
PhoneNumber = S t r i n g
Flavor
= String
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
17 / 22
Numere naturale
Proiect, ii
f i r s t N a m e : : Person > S t r i n g
f i r s t N a m e ( Person f i r s t n a m e _ _ _ _ _ ) = f i r s t n a m e
lastName : : Person > S t r i n g
lastName ( Person _ lastname _ _ _ _ ) = lastname
age : : Person > I n t
age ( Person _ _ age _ _ _ ) = age
h e i g h t : : Person > F l o a t
h e i g h t ( Person _ _ _ h e i g h t _ _ ) = h e i g h t
phoneNumber : : Person > S t r i n g
phoneNumber ( Person _ _ _ _ number _ ) = number
f l a v o r : : Person > S t r i n g
f l a v o r ( Person _ _ _ _ _ f l a v o r ) = f l a v o r
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
18 / 22
Numere naturale
Utilizare
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
19 / 22
nregistrari
,a (UNIBUC)
Traian Florin S, erbanut
firstName : : String
lastName : : S t r i n g
age : : I n t
height : : Float
phoneNumber : : S t r i n g
f l a v o r : : String
30 octombrie 2015
20 / 22
nregistrari
Utilizare
Putem folosi att forma algebrica ct s, i cea de nregistrare
i o n e l = Person " I o n " " Ionescu " 20 175.2
" 0712334567 " " Caramel "
g i g e l = Person {
,
,
,
,
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
21 / 22
nregistrari
De ce algebric?
,a (UNIBUC)
Traian Florin S, erbanut
30 octombrie 2015
22 / 22