{- types.hs This file contains some of the examples on types to be shown in the second class of the course "Haskell for Life", by Sergiu Ivanov (sivanov@lacl.fr): http://lacl.fr/~sivanov/doku.php?id=en:haskell_for_life This file is distributed under the Creative Commons Attribution Alone licence.-} data BookInfo = Book Int String [String] deriving (Show) data Coordinates = Cartesian2D Double Double | Polar2D Double Double deriving (Show) printCoordinate :: Coordinates -> String printCoordinate (Cartesian2D x y) = "x=" ++ show x ++ ",y=" ++ show y printCoordinate (Polar2D ρ φ) = "ρ=" ++ show ρ ++ ",φ=" ++ show φ data Pair a = Pair a a deriving (Show) -- | Returns 'True' if the given 'Maybe' contains 'Nothing'. myIsNothing :: Maybe a -> Bool myIsNothing Nothing = True myIsNothing (Just _) = False -- | Extracts the contents of a 'Maybe' (unpacks it). myFromJust :: Maybe a -> a myFromJust (Just x) = x data List a = Cons a (List a) | Nil deriving (Show) -- | Converts an instance of 'List' to a normal Haskell list. fromMyList :: List a -> [a] fromMyList (Cons x xs) = x:fromMyList xs fromMyList Nil = [] -- | Converts a normal Haskell list to an instance of 'List'. toMyList :: [a] -> List a toMyList (x:xs) = Cons x (toMyList xs) toMyList [] = Nil data FunnyList a = a :/ (FunnyList a) | Void deriving Show data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show, Read) -- | Returns a list of the values contained in the leaves of a tree. fruit :: Tree a -> [a] fruit Empty = [] fruit (Node x Empty Empty) = [x] fruit (Node _ l r) = fruit l ++ fruit r