Title: Functional Programming Lecture 6 - Algebraic Data Types
1Functional ProgrammingLecture 6 - Algebraic Data
Types
2Algebraic Data 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.
3Enumerated Types
- Example
- 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.
4Pattern Matching
- You can pattern match on this enumerated type.
- Example
- value Colour -gt 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 MonTues ..
- weekend Day -gt Bool
- weekend d (dSat) (dSun)
5Deriving
- Often when you define a new data type, you also
need to define a function that shows it. -
- (show -- convert to string, for
printing) - Example
- showColour Colour -gt String
- showColour Red Red
- showColour Blue Blue
- ...
- showInt Int -gt String
- showInt 5 gt 5
6deriving Show
- data Colour Red Blue Green deriving Show
- deriving Show tells the compiler automatically
to define - show Colour -gt String
- Example
- let c Blue
- in The answer is c \n wrong!
-
- let c Blue
- in The answer is (show c) \n
- today Colour -gt String
- today c the sky is (show c)
.
7Example The Bool Type
- data Bool False True deriving Show
- This is a built-in declaration in Haskell.
- False, True are the boolean constants -
- constructors of type Bool.
8Data Fields
- Example
- 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
-
- Examples
- x, y Thing
- x Item 3 small
- y Item (23) (str\n)
- show x gt Item 3 small
9Polymorphic Data Fields
- 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) gt Object a -gt Object a -gt Bool
- compare (Obj i x) (Obj j y) (i j) (x y)
- Examples
- compare (Obj 27 13) (Obj 27 6) gt
- compare (Obj 27 13) (Obj 27 hello) gt
- compare1 (Eq a) gt Object a -gt Object a -gt
Bool - compare1 (Obj i _) (Obj j _) (i j)
10Example Tuple Types
- 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
- (, , ,)
- So,
- Pair 2 3 Pairs Int Int
- (2,3) (Int,Int)
11Constructors and Union Types
- There could be several different cases
- each case has its own constructor
- and can have different fields.
- Example
- data PhoneNumber
- LocalPhone Int
- BritishPhone Int Int
- InternationalPhone Int Int Int
- Localphone 3304969 PhoneNumber
- BritishPhone 0141 3304969 PhoneNumber
- InternationalPhone 44 141 3304969 PhoneNumber
12Union Types
- Data Course FP String String Room
- DSA String String
Room - FP Calder Haskell lect2 Course
- DSA Jamieson Ada,Java lec4,lec5 Course
13Computations that might fail
- Sometimes a computation may succeed or fail
- but the program should deal with failure,
- instead of crashing.
- Example
- average Double -gt Double
- average xs sum xs / fromInt (length xs)
- average 4,12 gt 8.0
- average gt divide by 0 error -
crash! - Also, head
14The Maybe type
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
.. Maybe Int
15The Maybe type
- average Double -gt Maybe Double
- average Nothing
- average xs Just (sum xs / length xs)
- head a -gt Maybe a
- head Nothing
- head (xxs) Just x
- f xs ys x ys
- where Just x head xs
- What is type of f?
- a -gt a -gt a Why?
16Recursive Types
- A type T which has some data fields of type T.
- Example
- 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))
17Integer Lists
- data List Empty Cons Int List
- All equivalent representations!
- Cons 3 (Cons 4 (Cons 1 Empty))
- 3(4(1Empty))
- 3,4,1