import Data.List import Data.Function import Data.Maybe import Debug.Trace --------------------Constructori de tip a :: (,) Bool Char -- normal este (Bool, Char), scriem așa pentru a observa comportamentul de funcție al constructorilor de tip a = (True, 'a') b :: [] Int -- normal este [Int] b = [1,2,3] c :: (->) Int Int -- normal este Int -> Int c = (+1) --ex: Folosiți fib pentru a scrie o funcție cu tipul (Int -> Int) -> Int fib :: Int -> Int fib 0 = 0 fib 1 = 1 fib n = fib (n-2) + fib (n-1) --------------------Expresii de tip --Rol de verificare --wrongMyMap :: (a -> b) -> [a] -> [b] wrongMyMap f [] = [] wrongMyMap f (x:xs) = f (f x) : wrongMyMap f xs --Rol de documentare --contains :: String -> String -> Bool --fără această signatură, se deduce una mai generală --contains str sub --------------------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 -- Implementăm axiomele folosind pattern matching pe constructori! --numToNat :: Int -> Natural --natToNum :: Natural -> Int {- instance Show Natural where show n = show $ natToNum n -} --------------------Tipuri enumerate data Dice = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Enum, Show) --------------------Tipuri înregistrare data Person = Person { name :: (String, String), age :: Int } deriving Show -- echivalent cu data Person = Person (String, String) Int deriving Show fc :: Person fc = Person ("Frederic","Chopin") 216 composer :: (String, String) composer = name fc --nu avem nevoie de funcții selector, acestea se pot defini separat, --mulțumită mecanismului de pattern matching getName (Person name _) = name --ex: findInfo :: (String, String) -> [Person] -> Person --findInfo pname --------------------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: o listă care conține și Int și String --data List2 --filterA :: --filterB :: --------------------Tipul Maybe a 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 --diceToNum d --------------------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"), 216)