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! 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) numToNat :: Int -> Natural numToNat 0 = Zero numToNat n = Succ $ numToNat (n - 1) natToNum :: Natural -> Int natToNum Zero = 0 natToNum (Succ m) = 1 + natToNum m --------------------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 --data Person = Person (String, String) Int fc :: Person fc = Person ("Frederic","Chopin") 211 composer :: (String, String) composer = name fc --getName (Person n a) = n --------------------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 --append :: List a -> List a -> List a sumL :: [Int] -> Int sumL [] = 0 sumL (x:xs) = x + sumL xs --findMaxEvenSum :: [[Int]] -> Maybe Int findMaxEvenSum [] = Nothing findMaxEvenSum (l:ls) | even lsum = case findMaxEvenSum ls of Just s -> Just (max lsum s) _ -> Just lsum | otherwise = findMaxEvenSum ls where lsum = sumL l --ex: diceToNum folosind lookup diceToNum :: Dice -> Int diceToNum dice = num where Just num = lookup dice (zip [S1 .. S6] [1 .. 6]) --------------------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)