Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

5/10/2020 Functions over recursive datatypes | Reading 2: Recursive Data Types | 6.005.

2x Courseware | edX

Course  Readings  Readin…  Functio…

Functions over recursive datatypes


Functions over recursive datatypes
This way of thinking about datatypes — as a recursive de nition of an abstract
datatype with concrete variants — is appealing not only because it can handle recursive
and unbounded structures like lists and trees, but also because it provides a
convenient way to describe operations over the datatype, as functions with one case
per variant.

For example, consider the size of the list, which is certainly an operation we'll want in
ImList. We can de ne it like this:

size : ImList → int // returns the size of the list

and then fully specify its meaning by de ning size for each variant of ImList:

size(Empty) = 0
size(Cons( rst: E, rest: ImList)) = 1 + size(rest)

This function is recursive. We can think about the execution of size on a particular list
as a series of reduction steps:

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/02-Recursive-Data-Types/?child=first 1/4
5/10/2020 Functions over recursive datatypes | Reading 2: Recursive Data Types | 6.005.2x Courseware | edX

size(Cons (0, Cons (1, Empty)))


= 1 + size(Cons (1, Empty))
= 1 + (1 + size(Empty))
= 1 + (1 + 0)
=1+1
=2

And the cases from the de nition can be translated directly into Java as methods in
ImList, Empty, and Cons:

public interface ImList<E> {


// ...
public int size();
}

public class Empty<E> implements ImList<E> {


// ...
public int size() { return 0; }
}

public class Cons<E> implements ImList<E> {


// ...
public int size() { return 1 + rest.size(); }
}

This pattern of implementing an operation over a recursive datatype by

declaring the operation in the abstract datatype interface

implementing the operation (recursively) in each concrete variant

is a very common and practical design pattern. It sometimes goes by the unhelpful
name interpreter pattern.

Let's try a few more examples:

isEmpty : ImList → boolean

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/02-Recursive-Data-Types/?child=first 2/4
5/10/2020 Functions over recursive datatypes | Reading 2: Recursive Data Types | 6.005.2x Courseware | edX

isEmpty(Empty) = true
isEmpty(Cons( rst: E, rest: ImList)) = false

contains : ImList × E → boolean

contains(Empty, e: E) = false
contains(Cons( rst: E, rest: ImList), e: E) = ( rst = e) ∨ contains(rest, e)

get: ImList × int → E

get(Empty, n: int) = unde ned


get(Cons( rst: E, rest: ImList), n: int) = if n=0 then rst else get(rest, n-1)

append: ImList × ImList → ImList

append(Empty, list2: ImList) = list2


append(Cons( rst: E, rest: ImList), list2: ImList) = cons( rst, append(rest,
list2))

reverse: ImList → ImList

reverse(Empty) = empty()
reverse(Cons( rst: E, rest: ImList)) = append(reverse(rest), cons( rst, empty())

For reverse, it turns out that the recursive de nition produces a pretty bad
implementation in Java, with performance that's quadratic in the length of the list
you're reversing. We could rewrite it using an iterative approach if needed.

Discussion Hide Discussion


Topic: Reading 2 / Functions over recursive datatypes

Show all posts by recent activity

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/02-Recursive-Data-Types/?child=first 3/4
5/10/2020 Functions over recursive datatypes | Reading 2: Recursive Data Types | 6.005.2x Courseware | edX

 Recursive helper methods for recursive data types


25
The reading says: > *For reverse, it turns out that the recursive de nition produces a pretty bad i…

 Is there a template for ImList<E> test cases we could, "borrow?"


1
As per title.

 Static Calls to contains() 2


Is there any purpose or need to make static calls to the methods that take an ImList<E> as an arg…

 Seems very costly 8


I understand that some problems are inherently recursive. And in those cases recursion is a goo…

 append 6
I am trying to implement the append function, but I can't gure out how to do it without reversin…

 get method de nition 2


For the empty case, it should take and `int` not an `E` and maybe also call it `n` and not `e`. A…

© All Rights Reserved

https://courses.edx.org/courses/course-v1:MITx+6.005.2x+1T2017/courseware/Readings/02-Recursive-Data-Types/?child=first 4/4

You might also like