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) f g = fib $ g $ fib 0 --------------------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 --fără această signatură, se deduce una mai generală --contains :: String -> String -> Bool contains str sub = loop str where n = length sub loop [] = False loop str = sub == take n str || loop (tail str) --------------------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 numToNat 0 = Zero numToNat n = Succ $ numToNat $ n - 1 natToNum :: Natural -> Int natToNum Zero = 0 natToNum (Succ n) = 1 + natToNum n {- 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 = head . filter ((== pname) . 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: o listă care conține și Int și String data List2 a b = Nil2 | CA a (List2 a b) | CB b (List2 a b) deriving Show lst21 = CB "Hello " $ CA 2 $ CB "world!" Nil2 filterA :: List2 a b -> List2 a b filterA Nil2 = Nil2 filterA (CA a rest) = CA a $ filterA rest filterA (CB _ rest) = filterA rest --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 = case lookup d $ zip [S1 ..] [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"), 216)