--------------------Supraîncărcare myElem _ [] = False myElem a (x:xs) = a == x || myElem a xs --myElem2 :: (a -> b -> Bool) -> a -> [b] -> Bool myElem2 :: (a -> a -> Bool) -> a -> [a] -> Bool myElem2 _ _ [] = False myElem2 eq a (x:xs) = eq a x || myElem2 eq a xs --------------------Clase: definiri, instanțieri data Dice = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum) instance Show Dice where show S1 = "[·]" show S2 = "[:]" show S3 = "[·:]" show S4 = "[::]" show S5 = "[:·:]" show S6 = "[:::]" class Valuable a where value :: a -> Int instance Valuable Dice where value S1 = 1 value S2 = 2 value S3 = 3 value S4 = 4 value S5 = 5 value S6 = 6 --------------------Contexte rollSum :: (Dice, Dice) -> Int rollSum (x, y) = value x + value y myLen lst | lst == [] = 0 | otherwise = 1 + myLen (tail lst) myLen2 lst | null lst = 0 | otherwise = 1 + myLen2 (tail lst) --------------------Containere class Container t where contents :: t a -> [a] instance Container [] where -- [a]; [a] ~ [] a contents = id data List a = Nil | Cons a (List a) deriving Show --ex: instațierea clasei Container cu constructorul List (pt tipul List a) instance Container List where contents Nil = [] contents (Cons x xs) = x : contents xs -- fmap :: (a -> b) -> f a -> f b (întoarcem un container cu valorile din container transformate) -- exemplu: instance Functor [] -- [1,2,3] :: [a] => fiecare valoare din listă este o valoare închisă în container, deci toate trebuie supuse transformării --instance Functor Maybe ex1 = fmap (+1) (Just 5) -- Just {5} :: Maybe a => Just 6 --instance Functor ((->) r) ex2 = fmap (+1) (+1) 2 -- \x -> {x+1} :: (->) a b => fmap (+1) (+1) = \x -> {x+1+1} => fmap (+1) (+1) 2 = 4 --instance Functor ((,) a) ex3 = fmap (+1) (1,2) -- (1,{2}) :: (,) a b => fmap (+1) (1,2) = (1,3) -- foldl :: (a -> b -> a) -> a -> t b -> a (întoarcem un rezultat bazat pe procesarea acumulatorului și a valorilor din container) --instance Foldable ((,) a) ex4 = foldl (+) 10 (1,2) -- (1,2) :: (,) a b {- eq :: a -> a -> Bool eq x y = x == y -} {- eq :: (Eq a, Eq b) => a -> b -> Bool eq x y = x == y -}