Functional Programming Lecture 6 - Algebraic Data Types. Algebraic Data Types. A powerful mechanism for defining your own data types. All other types in Haskell can be defined using algebraic data types. We will start with simplest cases and work toward the most complicated. Enumerated Types.
A powerful mechanism for definingyour own data types.
All other types in Haskell can be defined using algebraic data types.
We will start with simplest cases and work toward the most complicated.
data Colour = Red | Green | Blue
data is a keyword - defines an algebraic data type.
Colour is the type name.
Red, Green, Blue are constructors.
Red :: Colour, Green ::Colour, etc.
Red:: Colour Green :: Colour
The type name and constructors must begin
with an upper case letter.
Variable must begin with lower case letter.
You can pattern match on this enumerated type.
value :: Colour -> Int
value Red = 15
value Green = 42
value Blue = 5
In general, define equations by
function-name pattern = expression
pattern is a constructor.
data Day = Mon|Tues ..
weekend :: Day -> Bool
weekend d = (d==Sat) || (d==Sun)
Often when you define a new data type, you also need to define a function that shows it.
(show -- convert to string, for printing)
showColour :: Colour -> String
showColour Red = “Red”
showColour Blue = “Blue”
showInt :: Int -> String
showInt 5 => “5”
data Colour = Red | Blue | Green deriving Show
deriving Show tells the compiler automatically to define
show :: Colour -> String
let c = Blue
in “The answer is” ++ c ++ “\n” wrong!
let c = Blue
in “The answer is” ++ (show c) ++ “\n”
today :: Colour -> String
today c = “the sky is “++ (show c) ++ “.”
data Bool = False | True deriving Show
This is a built-in declaration in Haskell.
False, True are the boolean constants -
constructors of type Bool.
data Thing = Item Int String deriving Show
A value of type Thing consists of a constructor Item (indicating what it is), a data field containing an Int, and another field containing a String.
u :: Int w :: String
Item u w :: Thing
x, y :: Thing
x = Item 3 “small”
y = Item (2+3) (str++”\n”)
show x => “Item 3 small”
Allow type a (where a is a type variable) instead of a specific type like Int.
Entire data type has type parameter a.
data Object a = Obj Int a
x = Obj 15 True :: Object Bool
y = Obj 27 13 :: Object Int
z = Obj 39 “Hello” :: Object String
compare :: (Eq a) => Object a -> Object a -> Bool
compare (Obj i x) (Obj j y) = (i ==j) && (x == y)
compare (Obj 27 13) (Obj 27 6) =>
compare (Obj 27 13) (Obj 27 “hello”) =>
compare1 :: Object a -> Object a -> Bool
compare1 (Obj i _) (Obj j _) = (i ==j)
data Pairs a b = Pair a b
data Triples a b c = Triple a b c
data Tuple4s a b c d = Tuple4 a b c d
The builtin tuple types behave essentially like
this, except they have a special syntax with
(, , ,)
Pair 2 3 :: Pairs Int Int
(2,3) :: (Int,Int)
There could be several different cases
each case has its own constructor
and can have different fields.
= LocalPhone Int
| BritishPhone Int Int
| InternationalPhone Int Int Int
Localphone 3304969 :: PhoneNumber
BritishPhone 141 3304969 :: PhoneNumber
InternationalPhone 44 141 3304969 :: PhoneNumber
data Course = FP String String Room
| DSA String [String] [Room]
FP “Calder” “Haskell” lect2 ::Course
DSA “Miller” [“Ada”,”Java”] [lec4,lec5] ::Course
Sometimes a computation may succeed or fail
but the program should deal with failure,
instead of crashing.
average :: [Double] -> Double
average xs = sum xs / fromInt (length xs)
average [4,12] => 8.0
average [ ] => divide by 0 error - crash!
Also, head 
data Maybe a = Just a | Nothing
Represents result of a computation that maybe gives
you a value of type a, but could give you nothing.
Normal values of a Nothing
Just 0 Nothing
Just 1 Just 2 Just 3 Just 4
Just 5 Just 6 …..
average :: [Double] -> Maybe Double
average [ ] = Nothing
average xs = Just (sum xs / length xs)
head :: [a] -> Maybe a
head  = Nothing
head (x:xs) = Just x
f xs ys = x : ys
where Just x = head xs
What is type of f?
[a] -> [a] -> [a] Why?
A type T which has some data fields of type T.
data T = T0 String | T1 Int T
s ::String x :: Int t :: T
TO s :: T T1 x t :: T
x, y, z :: T
x = T0 “aardvark”
y = T1 31 (T0 “cat”)
z = T1 17 (T1 86 (T0 “dog”))
data List = Empty | Cons Int List
All equivalent representations!
Cons 3 (Cons 4 (Cons 1 Empty))