Topic 7: Algebraic Data Types 1
Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11, 14.12, 14.14, 14.21, 14.22 Readings: Chapter 5.3 14.1, 14.2, and 14.3 2
Types What types have we already seen? Base Types: Composite Types: Algebraic types provide a powerful mechanism for defining our own types 3
Enumerated Types Algebraic type that has a finite list of possible values Example: Primary Color Possible values: Red, Green and Blue Haskell Declaration: 4
Enumerated Types The values of an enumerated type are referred to as constructors Type names must begin with an uppercase letter Constructors must begin with an uppercase letter Another Example: Construct an enumerated type for the days of the week 5
Enumerated Types New types can be part of an existing type class Can derive the default implementation Example: Extend our Day type so that it can be tested for equality 6
Enumerated Types New types can be part of an existing type class Can provide its own implementation Example: Further extend our Day type so that when it is converted to a String, the full name of the day is used 7
Enumerated Types Enumerated types can be passed as parameters to functions And returned as results Example: Write a function that returns True for days on the weekend, False otherwise 8
Enumerated Types in Other Languages C Enumeration Example: enum PrimaryColor { RED, GREEN, BLUE }; Integers and enumeration constants can be freely mixed Programmer can select the integer assigned to each enumeration value Java Enumeration Example: enum PrimaryColor { RED, GREEN, BLUE }; Defines a new type Cannot be treated as an integer 9
Enumerated Types in Other Languages Python Must import the Enum class New enumerated type is a subclass of Enum Enumerations are not type safe Comparing two enumerated values from different collections returns False rather than raising a type error 10
Product Types Algebraic type that is similar to a tuple One or more values is provided to the constructor when an instance of the type is created Example: Construct a type that stores an arbitrary color as integer amounts of red, green and blue 11
Product Types Can a color be represented as a tuple? Yes! (Int, Int, Int) Can a triangle be represented as a tuple? Yes! (Int, Int, Int) What s the problem with this arrangement? 12
Product Types What benefit do product types provide? Better type checking Better error messages Better code readability What benefits do tuples provide? Functions like fst, snd, zip, unzip, etc. that can t be applied directly to product types Less typing 13
Product Types Product types can have multiple constructors Each constructor can take a different number of parameters Define a type that represents a shape Circles have a radius Rectangles have a length and width Triangles have lengths for all three of their sides 14
Product Types Write a function that computes the area of a shape 15
Recursive Algebraic Types Recall: A recursive definition defines something in terms of itself We previously applied this to functions It can also be applied to types Recursive algebraic type: A type defined in terms of itself 16
Recursive Algebraic Types Example: How do we represent a node in a binary tree in Java? This is a recursive type 17
Recursive Algebraic Types Example: How do we represent a node in a binary tree in Haskell? Base case: An empty tree Recursive case: A value, a left tree and a right tree 18
Creating a Binary Tree What expression represents this tree? 42 13 19 12 19
Creating a Binary Tree 25 What expression represents this tree? 50 75 60 20
Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that determines the height of a binary tree 21
Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that determines whether or not a value is present in the tree How is the function for a general binary tree different from the function for a binary search tree? 22
Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that inserts a new value into a binary search tree 23
Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that constructs a binary search tree from a list of values A non recursive alternative? 24
Functions on Binary Trees Tree traversals: Pre order In order Post order Level order 25
Functions on Binary Trees Functions that operate on recursive data structures are often recursive Example: Write a function that maps a function over every node in a tree 26
Polymorphic Algebraic Types What s wrong with the binary tree type we just created? How do we overcome this limitation? 27
Polymorphic Algebraic Types Create a polymorphic type for a binary tree How does this type need to be modified for a binary search tree? 28
Polymorphic Algebraic Types Example: Write a polymorphic function that adds an element to a polymorphic binary search tree 29
Polymorphic Algebraic Types Example: Write a function that performs an in order traversal of a polymorphic binary search tree 30
Polymorphic Types in Other Languages A polymorphic binary search tree in C++ template <class T> class Node { private: T data; Node* left; Node* right; }; public: Node(const T &val); ~Node(); void insert(const T &v); string tostring() const; template <class T> void Node<T>::insert(const T &v) { if (v < this >data) { if (this >left == NULL) { this >left = new Node<T>(v); } else { this >left >insert(v); } } else { if (this >right == NULL) { this >right = new Node<T>(v); } else { this >right >insert(v); } } } 31
Polymorphic Types in Other Languages A polymorphic binary search tree in C++ template <class T> string Node<T>::toString() const { stringstream ss; ss << (this >left == NULL? "" : this >left >tostring() + ",") << this >data << (this >right == NULL? "" : "," + this >right >tostring()); return ss.str(); } 32
Summary Algebraic types provide several ways to introduce new user defined types Enumerate types Product types Recursive types Polymorphic recursive types 33