import Data.Foldable --------------------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 instance Container List where contents Nil = [] contents (Cons x lst) = x : contents lst acrostic Nil = [] acrostic (Cons Nil others) = acrostic others acrostic (Cons (Cons "" _) others) = acrostic others acrostic (Cons (Cons s _) others) = head s : acrostic others ex = acrostic (Cons (Cons "Finally" (Cons "I" Nil)) -- prima listă interioară (Cons Nil -- lista #2 (Cons (Cons "Understand" (Cons "What" (Cons "I" Nil))) -- #3 (Cons (Cons "Need" (Cons "To" Nil)) -- #4 (Cons (Cons "Code" Nil) -- #5 (Cons (Cons "To" (Cons "Solve" (Cons "This" Nil))) -- #6 (Cons (Cons "Interesting" (Cons "And" Nil)) -- #7 (Cons (Cons "Original" Nil) -- #8 (Cons (Cons "" (Cons "Task" (Cons "Right" Nil))) -- #9 (Cons (Cons "Now!" Nil) -- #10 Nil)))))))))) -- fmap :: (a -> b) -> f a -> f b (întoarcem un container cu valorile din container transformate) -- [a] : [{1},{2},{3}] ex1 = fmap (+1) (Just 5) -- Maybe a : Just {5} -- (+1) 5 = 6 -- Just 6 ex2 = fmap (+1) (+1) 2 -- (->) a b : \x -> {x+1} -- \x -> {x+2} -- 4 ex3 = fmap (+1) (1,2) -- (,) a b : (1,{2}) -- (1,3) -- foldl :: (a -> b -> a) -> a -> t b -> a (întoarcem un rezultat bazat pe procesarea acumulatorului și a valorilor din container) ex4 = Data.Foldable.foldl (+) 10 (1,2) -- (,) a b : (+) 10 2 = 12 {- eq :: a -> a -> Bool eq x y = x == y -} {- eq :: (Eq a, Eq b) => a -> b -> Bool eq x y = x == y -}