import Data.Foldable type Age = Int type Name = (String,String) names :: [Name] names = [("Frederic","Chopin"), ("Antonio", "Vivaldi"), ("Maurice", "Ravel")] newtype Person = Person (Name, Age) deriving Show fc :: Person fc = Person (("Frederic","Chopin"), 209) data Dice = S1 | S2 | S3 | S4 | S5 | S6 deriving (Eq, Ord, Enum) data Person2 = Person2 {name :: Name, age :: Age} deriving (Read, Show) --data Person2 = Person2 Name Age fc2 :: Person2 fc2 = Person2 ("Frederic","Chopin") 209 composer :: Name composer = name fc2 data Natural = Zero | Succ Natural deriving Show add Zero n = n add (Succ m) n = Succ (add m n) --data IntList = Nil | Cons Int IntList deriving Show 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 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 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 --rollSum :: (Dice, Dice) -> Int --rollSum :: (Valuable a, Valuable b) => (a, b) -> 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) class Container t where contents :: t a -> [a] instance Container [] where contents = id --data List a = Nil | Cons a (List a) deriving Show instance Container List where contents Nil = [] contents (Cons element lst) = element : contents lst -- fmap :: (a -> b) -> f a -> f b -- [] | [{}, {} ... {}] ex1 = fmap (+1) (Just 5) -- Nothing | Just {} -- (+1) 5 = 6 -- Just 6 ex2 = fmap (+1) (+1) 2 -- \x -> {} -- (+1) (x + 1) = x + 2 -- \x -> x+2 ex3 = fmap (+1) (1,2) -- (a, {}) -- (+1) 2 = 3 -- (1,3) -- foldl :: (a -> b -> a) -> a -> t b -> a ex4 = Data.Foldable.foldl (+) 10 (1,2) -- (a, {}) -- (+) 10 2 = 12 -- 12 {- eq :: a -> a -> Bool eq x y = x == y -} {- eq :: (Eq a, Eq b) => a -> b -> Bool eq x y = x == y -}