Download Recursive Data Types - University of Surrey

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures
Large amounts of data
Computing is often about large amounts of data
Databases
Recursive Data Types
COM1022 Functional Programming and Reasoning
Customer data
Inventories
Research observations
Dr Hans Georg Schaathun
How do you manage it?
Indexing
Searchable
Editing
Adding data
University of Surrey
Spring 2010 – Week 7
... and make it fast?
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
1 / 29
Dr Hans Georg Schaathun
Data Structures
Recursive Data Types
Spring 2010 – Week 7
3 / 29
Spring 2010 – Week 7
5 / 29
Data Structures
Limitations of the list
How to store data
Many alternative data structures exist; e.g.
Lists
The Haskell list
Very general – many tasks are slow
allows unlimited data
built-in in Haskell
Queues (First In; First Out)
Only the first element can be accessed
So what is the problem?
Efficiency
Stacks (First In; Last Out)
Only the top element can be accessed
Both queues and stacks can be made efficient
Searching for an item has to traverse the list
Removing an item may require moving remaining elements
Sorted trees
Maintains sort order
Only part of the tree has to be searched
Slow for large lists
We shall use the sorted tree as an example
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
4 / 29
Dr Hans Georg Schaathun
Recursive Data Types
Recursive Data Types
Simple Examples
Recursive Data Types
Recursive Data Types
Simple Examples
Pointers and linked lists
The imperative analogue
Recall the Algebraic Datatype
data Expr = Term Float | Add Float Float | Mult
Float Float
Algebraic data types may be recursive
Are you familiar with linked lists in imperative languages?
data Expr = Term Float | Add Expr Expr | Mult
Expr Expr
Arbitrary length expressions with addition and multiplication
What are linked lists used for?
Principle as in other types of recursion
Base case: The primitive term (Float)
Recursive case(s): combine two expressions with an operator
Dr Hans Georg Schaathun
Recursive Data Types
Recursive Data Types
Spring 2010 – Week 7
7 / 29
Simple Examples
Recursive Data Types
Recursive Data Types
Recursive data types vs. linked lists
Spring 2010 – Week 7
8 / 29
Functions on a recursive type
Functions on a recursive type
All data types require functions to be useful
Haskell does not have pointers
How do we work with recursive datatypes?
The data type has a base case and a recursive case
Recursive data types (etc) can do the same job
data List = Node Data List | EmptyList
data List = Node Data List | EmptyList
where Data is the constituent type
Recursive functions will typically use the same structure
This list is a toy example
A base case: f EmptyList = ...
A recursive case: f (Node d l) = g d ...
you can just as well use a native list [a]
It is a simple illustration
Dr Hans Georg Schaathun
Dr Hans Georg Schaathun
f l
Operate on the data d
Operate recursively on the rest of the list l
Recursive Data Types
Spring 2010 – Week 7
9 / 29
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
10 / 29
Recursive Data Types
Functions on a recursive type
Recursive Data Types
Length of a list
Minimum of a list
An example
An example
Consider a list of numbers
data List = Node Data List | EmptyList
data List = Node Float List | EmptyList
How do we define the length?
How do we define the minimum?
ll EmptyList = 0
lmin (Node a EmptyList) = a
ll (Node _ l) = 1 + ll l
Dr Hans Georg Schaathun
Functions on a recursive type
Recursive Data Types
lmin (Node a l) = min a (lmin l)
Spring 2010 – Week 7
11 / 29
Dr Hans Georg Schaathun
Polymorphic datatypes
Recursive Data Types
Spring 2010 – Week 7
12 / 29
Polymorphic datatypes
Recall polymorphism
Polymorphic datatypes
data List = Node Data List | EmptyList
List of objects of type Data
Function definitions for a range of data types
For instance, unrestricted
Do we need to know the constituent datatype when defining
List?
length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs
Of course not. There is polymorphism.
data List a = Node a (List a) | EmptyList
This allows us to declare the types we want
Or restricted to a class
min :: ( Ord a ) => a -> a -> a
min a b | a <= b = a
min a b | b <= a = b
ilist :: List Int
staff :: List (String,String,String,Int)
newtype Library = Lib (List Book)
Your Library ADT could be implemented using this list
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
14 / 29
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
15 / 29
Polymorphic datatypes
Sorted trees
The Type Constructor
Sorting data in trees
The binary tree
0
data List a = Node a (List a) | EmptyList
This declares a Type Constructor List
The type constructor is a function
Input: a type – the constituent type
Output: another type – the list type
Each node stores a piece of data
1
Each node has two subtrees (binary)
2
Such polymorphic datatype aka. parameterised datatype
Dr Hans Georg Schaathun
Recursive Data Types
Sorted trees
Spring 2010 – Week 7
16 / 29
Dr Hans Georg Schaathun
Sorting data in trees
Recursive Data Types
Sorted trees
A tree datatype
6
4
Spring 2010 – Week 7
18 / 29
Sorting data in trees
A sorted tree
How do we define the tree in Haskell?
Algebraic data type for the node:
1
2
Will this do?
What about leaf nodes? With no children?
3
We need a base case ...
the node is larger than any node in
the left hand subtree
the node is smaller than any node in
the right hand subtree
both subtrees are sorted
Note the recursive definition
data Tree = Node Data Tree Tree | Nil
Recursive Data Types
3
The tree is sorted if for every node
data Tree = Node Data Tree Tree
Dr Hans Georg Schaathun
5
3
The parameter is the type a
Spring 2010 – Week 7
19 / 29
Dr Hans Georg Schaathun
Recursive Data Types
5
1
0
2
4
6
Spring 2010 – Week 7
20 / 29
Sorted trees
Sorting data in trees
Sorted trees
Tree versus list
Implementing the tree
The data type
0
The tree is defined recursively
1
4
Suppose there are n elements
Finding an element requires
traversing
2
n/2 elements on average in a
linked list (even if sorted)
log n elements on average in a
sorted tree
3
It can be empty (no nodes at all)
Or it is a root node (implicitly including its children)
Thus it could look like this
1
4
data Tree = Nil | Node Int Tree Tree
Recognise the elements of recursion
0
2
3
Base case: Nil
Recursive case: Node ...
5
4
Dr Hans Georg Schaathun
Recursive Data Types
Sorted trees
Spring 2010 – Week 7
Dr Hans Georg Schaathun
21 / 29
Implementing the tree
Recursive Data Types
Sorted trees
Functions for the tree
Spring 2010 – Week 7
22 / 29
Spring 2010 – Week 7
24 / 29
Implementing the tree
How to insert
Again we work recursively
1
Each input takes a tree and an element
insert Nil x
... and produces a new tree
2
insert ::
Tree -> Int -> Tree
find ::
= Node x Nil Nil
The recursive case
insert (Node y t1 t2) x
Compare x and y
Tree -> Int -> Tree
delete ::
Base case: the empty tree
1
Tree -> Int -> Tree
2
x < y : insert in t1
x > y : insert in t2
insert (Node y t1 t2) x
| x < y = Node y (insert t1 x) t2
| x > y = Node y t1 (insert t2 x)
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
23 / 29
Dr Hans Georg Schaathun
Recursive Data Types
Sorted trees
Implementing the tree
Sorted trees
Real applications
Implementing the tree
Imperative vs. Functional
Important difference between imperative and functional
programming
Why would you need a sorted tree of integers?
More often each node contains composite data
... evident in implementation of data structures
Imperative programming has one structure of data
Tupple
Product Data Type
this can be modified and updated
i.e. it can be in different states
The data includes a key (e.g. surname)
Pure functional programming does not permit that
sorting by key
look up (find) by key
the insert function produces a new tree
Is this good or bad?
Dr Hans Georg Schaathun
Recursive Data Types
Sorted trees
Spring 2010 – Week 7
25 / 29
Dr Hans Georg Schaathun
Implementing the tree
Recursive Data Types
Spring 2010 – Week 7
26 / 29
Summary
Imperative vs. Functional
Summary
Selected thoughts on advantages and disadvantages
Mainly, it depends on the compiler/interpreter
If the compiler is good
Recursive datatypes provide large data structures
Data copied only when needed
Only computed when necessary
Recursive datatypes require recursive functions
Garbage collectors remove unused data
Functional programming requires a good compiler
It has to optimise and make short-cuts
Hence, the programmer should have to think less
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
27 / 29
Dr Hans Georg Schaathun
Recursive Data Types
Spring 2010 – Week 7
29 / 29
Related documents