210 likes | 350 Views
TÜÜBID. Haskellis on igal avaldisel tüüp Tüübituletus Polümorfsed tüübid. (+) :: Int -> Int -> Int (+) :: Float -> Float -> Float (==) :: a -> a -> Bool show :: a -> String Ülemääratud operaatoreid defineeritakse tüübiklassidega. Andmetüübi deklareerimine: data Loenditüübid
E N D
TÜÜBID • Haskellis on igal avaldisel tüüp • Tüübituletus • Polümorfsed tüübid
(+) :: Int -> Int -> Int (+) :: Float -> Float -> Float (==) :: a -> a -> Bool show :: a -> String • Ülemääratud operaatoreid defineeritakse tüübiklassidega.
Andmetüübi deklareerimine: data • Loenditüübid data Päev = ETKNRLP data Bool = FalseTrue • Rekursiivsed andmetüübid data Tree a = Empty|Branch a(Tree a) (Tree a)
Näidiste sobitamine workday :: Päev -> Bool workday E = True workday T = True workday K = True workday N = True workday R = True workday L = False workday P = False
Variant-kirjed data Either = Left Bool | Right Char Left :: Bool -> Either Right :: Char -> Either Data Either a b = Left a | Right b data Maybe a = Just a | Nothing Just :: a -> Maybe a Nothing :: Maybe a
Tüübisünonüümid type • Võib olla polümorfne, kuid mitte rekursiivne • Pärivad aluseks võetud klassilt kõik meetodid • Kui tahame meetodeid ise kirjeldada, võib kasutada ka newtype
Näide. type Kg = Double type Metres = Double type Newtons = Double g = 6.67e-11 gravity::(Kg,Metres,Kg)Newtons gravity (m1,r,m2) r == 0 = 0.0 otherwise=(g*m1*m2)/(square r)
TÜÜBIKLASSID • Tüübiklass Eq • Class Eq a where (==), (/=) :: a -> a -> Bool (==), (/=) :: Eq a => a -> a -> Bool
Tüübiklass Ord • Class (Eq a) => Ord a where (<), (<=), (>), (>=) :: a -> a -> Bool compare :: a -> a -> Ordering max, min :: a -> a -> a
compare x y | x == y = EQ | x <= y = LT | otherwise = GT x <= y = compare x y /= GT x < y = compare x y == LT x >= y = compare x y /= LT x > y = compare x y == GT max x y = if x >= y then x else y min x y = if x < y then x else y
Tüüpe saab kuulutada tüübiklassi kuuluvaks: • instance Eq Bool where ( x==y) = (xy)(not x not y) • instance Eq Char where (x==y) = (ord x == ord y) • instance Ord Char where (x<y) = (ord x < ord y) • instance Enum Char where toEnum = ord fromEnum = chr
Eeldefineeritud funktsioone: (&&), () :: Bool Bool Bool not :: Bool Bool otherwise :: Bool (==),(/=),(<),(<=),(>),(>=) :: :: Eq a => a a a Bool
Eeldefineeritud funktsioone: isSpace, isDigit, isAlpha, isUpper, isLower :: Char Bool toUpper, toLower :: Char Char ord :: Char Int chr :: Int Char
Näide. Binaarpuude võrdus. data IntBtree = Lf Int | Br IntBtree IntBtree instance Eq IntBtree where Lf i1 == Lf i2 = i1 == i2 Br t11 t12 == Br t21 t22 = t11 == t21 && t12 == t22 _ == _ = False
Näide. Listide võrdus. instance Eq a => Eq [a] where [] == [] = True (x:xs)==(y:ys) = x==y && xs==ys _ == _ = False • Näide. Põõsaste võrdus. data Bush a = One a|Two(Bush a)(Bush a)|Many [Bush a]
instance Eq a => Eq (Bush a) where One x == One y = x == y Two x1 x2 == Two y1 y2 = x1 == y1 && x2 == y2 Many xs == Many ys = xs == ys _ == _ = False (==) ::a -> a -> Bool (==) :: Bush a -> Bush a -> Bool (==) :: [Bush a]->[Bush a]->Bool
Tüübiklass Show • Sellesse kuuluvatel tüüpidel on olemas funktsioon nimega show, mis teisendab vastava tüübi stringiks. show :: a String • Nt. show(5) = “5”
Tüübiklass Num class (Eq a, Show a) => Num a where (+), (-), (*) :: a a a negate:: a a fromInteger :: Integer a
class (Num a, Ord a) => Real a where toRational :: a Rational • class(Real a, Enum a) => Integral a where (div),(mod) :: a a a toInteger :: a Integer • class (Num a ) =>Fractional a where (/) :: a a a fromRational :: Rational a abs, signum :: Num a => a a
Tüübiklass Enum class Enum a where toEnum :: Int -> a fromEnum :: a -> Int enumFrom :: a -> [a] --[n..] enumFromThen :: a -> a -> [a] –– [n,n’..] enumFromTo :: a -> a -> [a] --[n..m] enumFromThenTo :: a -> a -> a -> [a] -- [n,n’..m] succ, pred :: Enum a => a -> a succ = toEnum . (+1) . fromEnum pred = toEnum . (subtract 1) . fromEnum
Klassikuuluvuse tuletamine: data Päev = ETKNRLP deriving (Eq, Ord, Enum) • derivingabil saab tuletada kuuluvust ainult eeldefineeritud klassidesse Eq,Ord,Enum,Bounded,Show,Read.