import Data.List import Data.Function import Data.Maybe --------------------Constructori de tip a :: (,) Bool Char -- normal era (Bool, Char), scriem așa pentru a observa comportamentul de funcție al constructorilor de tip a = (True, 'a') b :: [] Int -- normal era [Int] b = [1,2,3] c :: (->) Int Int -- normal era Int -> Int c = (+1) fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) --------------------Expresii de tip --wrongMyMap :: (a -> b) -> [a] -> [b] wrongMyMap f [] = [] wrongMyMap f (x:xs) = f (f x) : wrongMyMap f xs myMap :: (a -> b) -> [a] -> [b] myMap f [] = [] myMap f (x:xs) = f x : myMap f xs --------------------Tipuri definite de utilizator data RH = Pos | Neg deriving Show -- doar constructori nulari data ABO = O | A | B | AB deriving Show -- doar constructori nulari data BloodType = BloodType ABO RH deriving Show -- constructor extern --definim axiomele folosind pattern matching pe constructori! mosquitoRisk :: BloodType -> Bool mosquitoRisk (BloodType A _) = True mosquitoRisk (BloodType B _) = True mosquitoRisk _ = False data Natural = Zero | Succ Natural -- constructor nular și constructor intern deriving Show -- face posibilă afișarea valorilor tipului unu = Succ Zero doi = Succ unu trei = Succ doi addN :: Natural -> Natural -> Natural -- arată exact ca axiomele addN Zero n = n addN (Succ m) n = Succ (addN m n) -- ex: de implementat conversia (bidirecțională) Int <-> Natural -- observați că implementăm axiomele folosind pattern matching pe constructori numToNat :: Int -> Natural numToNat 0 = Zero numToNat n = Succ $ numToNat (n-1) natToNum :: Natural -> Int natToNum Zero = 0 natToNum (Succ n) = 1 + natToNum n -- exemplu: o lista care contine si int si string data MyList a b = MyNil | MyCons1 a (MyList a b) | MyCons2 b (MyList a b) deriving Show myLst = MyCons1 '2' $ MyCons2 [12,13] $ MyCons2 [14] MyNil filterInt :: MyList a b -> [a] filterInt MyNil = [] filterInt (MyCons1 x rest) = x : filterInt rest filterInt (MyCons2 s rest) = filterInt rest filterStr :: MyList a b -> [b] filterStr MyNil = [] filterStr (MyCons1 x rest) = filterStr rest filterStr (MyCons2 s rest) = s : filterStr rest --------------------Tipuri enumerate data Dice = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Enum, Show) --------------------Tipuri înregistrare data Person = Person {name :: (String, String), age :: Int, kids :: [Person]} deriving Show --data Person = Person (String, String) Int deriving Show fc :: Person fc = Person ("Frederic","Chopin") 211 [Person ("Jacek","Chopin") 190 []] composer :: (String, String) composer = name fc findInfo :: (String, String) -> [Person] -> Person findInfo n people = head $ filter ((==n) . name) people --nu avem nevoie de funcții selector, acestea se pot defini separat, --tot mulțumită mecanismului de pattern matching --getName (Person name _) = name --------------------Tipuri recursive --data IntList = Nil | Cons Int IntList deriving Show data IntList = INil | ICons Int IntList deriving Show ilst = ICons 8 $ ICons 11 INil --------------------Tipuri parametrizate data List a = Nil | Cons a (List a) deriving Show lst1 = Cons 1 $ Cons 2.5 $ Cons 4 Nil -- :t lst1 => lst1 :: List Double lst2 = Cons "Hello " $ Cons "world!" Nil --ex: append :: List a -> List a -> List a append Nil b = b append (Cons x xs) b = Cons x $ append xs b sumL :: [Int] -> Int sumL [] = 0 sumL (x:xs) = x + sumL xs --findMaxEvenSum :: [[Int]] -> Maybe Int findMaxEvenSum [] = Nothing findMaxEvenSum (l:ls) | even lsum = case res of Just s -> Just (max lsum s) _ -> Just lsum | otherwise = res where lsum = sumL l res = findMaxEvenSum ls --ex: diceToNum folosind lookup --pt fromJust trebuie importat Data.Maybe diceToNum d = case lookup d $ zip [S1 .. S6] [1 .. ] of Just n -> n --------------------Construcția type: sinonime de tip type Age = Int type Name = (String, String) names :: [Name] names = [("Frederic","Chopin"), ("Antonio","Vivaldi"), ("Maurice","Ravel")] --------------------Construcția newtype: tipuri cu un singur constructor unar newtype Person2 = Person2 (Name, Age) deriving Show fc2 :: Person2 fc2 = Person2 (("Frederic","Chopin"), 211)