1 / 21

TÜÜBID

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

Télécharger la présentation

TÜÜBID

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. TÜÜBID • Haskellis on igal avaldisel tüüp • Tüübituletus • Polümorfsed tüübid

  2. (+) :: Int -> Int -> Int (+) :: Float -> Float -> Float (==) :: a -> a -> Bool show :: a -> String • Ülemääratud operaatoreid defineeritakse tüübiklassidega.

  3. Andmetüübi deklareerimine: data • Loenditüübid data Päev = ETKNRLP data Bool = FalseTrue • Rekursiivsed andmetüübid data Tree a = Empty|Branch a(Tree a) (Tree a)

  4. 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

  5. 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

  6. 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

  7. 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)

  8. TÜÜBIKLASSID • Tüübiklass Eq • Class Eq a where (==), (/=) :: a -> a -> Bool (==), (/=) :: Eq a => a -> a -> Bool

  9. Tüübiklass Ord • Class (Eq a) => Ord a where (<), (<=), (>), (>=) :: a -> a -> Bool compare :: a -> a -> Ordering max, min :: a -> a -> a

  10. 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

  11. Tüüpe saab kuulutada tüübiklassi kuuluvaks: • instance Eq Bool where ( x==y) = (xy)(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

  12. Eeldefineeritud funktsioone: (&&), () :: Bool  Bool  Bool not :: Bool  Bool otherwise :: Bool (==),(/=),(<),(<=),(>),(>=) :: :: Eq a => a  a  a  Bool

  13. Eeldefineeritud funktsioone: isSpace, isDigit, isAlpha, isUpper, isLower :: Char  Bool toUpper, toLower :: Char  Char ord :: Char  Int chr :: Int  Char

  14. 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

  15. 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]

  16. 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

  17. 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”

  18. Tüübiklass Num class (Eq a, Show a) => Num a where (+), (-), (*) :: a  a  a negate:: a  a fromInteger :: Integer  a

  19. 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

  20. 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

  21. Klassikuuluvuse tuletamine: data Päev = ETKNRLP deriving (Eq, Ord, Enum) • derivingabil saab tuletada kuuluvust ainult eeldefineeritud klassidesse Eq,Ord,Enum,Bounded,Show,Read.

More Related