123
Foundations of Computer Science and Software Engineering module 4a/30 WALID S. SABA

Intro to computer science module 4a

Embed Size (px)

DESCRIPTION

This is module 4a of an introductory course that will introduce computer science and software engineering to the novice, and it is also a course that will teach even veteran computer scientists some new concepts, or explain concepts they thought they understood well. Concepts are introduced with hands on experience using object-oriented pseudo code (which can very easily be translated to C#, Java, C++, etc.) and functional programming pseudo code (which can easily be translated into F#, Erlang, Python, Haskell, etc.)

Citation preview

Page 1: Intro to computer science   module 4a

Foundations of Computer Science and Software Engineering

module 4a/30

WALID S. SABA

Page 2: Intro to computer science   module 4a

Foundations of Computer Science and Software Engineering

ALBERT EINSTIEN

TO THE TEACHER

Page 3: Intro to computer science   module 4a

Foundations of Computer Science and Software Engineering

TO THE STUDENT

SOCRATES

Page 4: Intro to computer science   module 4a

Foundations of Computer Science and Software Engineering

About this CourseThis is an introductory course that will introduce computer science and software engineering to the novice, and it is also a course that will teach even veteran computer scientists some new concepts, or explain concepts they thought they understood well. Concepts are introduced with hands on experience using object-oriented pseudo code (which can very easily be translated to C#, Java, C++, etc.) and functional programming pseudo code (which can easily be translated into F#, Erlang, Python, Haskell, etc.)

About the AuthorWalid Saba has 20 years of experience in information technology, where he worked at such places as the American Institutes for Research, AT&T Bell Labs, Metlife, Nortel Networks, IBM and Cognos. He has also spent 7 years in academia where he has taught computer science at Carelton University, the New Jersey Institute of Technology, the University of Windsor, and the American University of Beirut. He has published over 30 technical articles, including an award wining paper that he recieved at KI-2008 in Germany. Walid holds a PhD in Computer Science which he obtained from Carleton University in1999

Page 5: Intro to computer science   module 4a

MODULE 4 (1 hour)

Foundations of Computer Science and Software Engineering

MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part 1)

Page 6: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

In most programming languages you can define an integer variable and assign it an initial value in a statement like this:

int n = 31789;

Page 7: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

In most programming languages you can define an integer variable and assign it an initial value in a statement like this:

int n = 31789;

In other programming languages we might define a function that expects an integer value as input:

square n = n * n

Page 8: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

In most programming languages you can define an integer variable and assign it an initial value in a statement like this:

int n = 31789;

square n = n * n

How does the compiler of the programming language check if the assigned value or the value passed as input is a valid integer?

In other programming languages we might define a function that expects an integer value as input:

Page 9: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

IsAValidInteger

Input string

true/false

Somewhere, there must be some truth-valued (or Boolean) function, say IsAValidInteger, that takes a string and tests whether that string is a valid integer

Page 10: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

IsAValidInteger

Input string

true/false

bool IsAValidInteger(string input){ if (input == "0") return true; if (input == "1") return true; if (input == "2") return true; ... if (input == "1732") return true; ...}

// this function might look like this

But what would the logic in the function IsAValidInteger look like?

Page 11: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Infinite Objects

IsAValidInteger

Input string

true/false

bool IsAValidInteger(string input){ if (input == "0") return true; if (input == "1") return true; if (input == "2") return true; ... if (input == "1732") return true; ...}

// this function might look like this

But what would the logic in the function IsAValidInteger look like?

But how do we check all possible cases? Where do we stop?

Page 12: Intro to computer science   module 4a

Recursion and Infinite Objects

Introduction to Computer Science – Preliminaries

It turns out that the only way to have a finite definition of an infinite object is by recursion, the easiest and one of the most important concepts in computer science

Let us define the function that will take any string whatsoever and check whether it is a valid number or not ...

Page 13: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

First, we need to define a function that checks if the input is a valid digit:

Recursion

Page 14: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

RecursionFirst, we need to define a function that checks if the input is a valid digit:

Page 15: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Recursion

A single character is a valid digit if it is 0, 1, 2, 3, 4, 5, 6, 7, 8 or 9

First, we need to define a function that checks if the input is a valid digit:

Page 16: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Recursion

Example of how this predicate is applied

First, we need to define a function that checks if the input is a valid digit:

Page 17: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

RecursionFirst, we need to define a function that checks if the input is a valid digit:

Another example of how this predicate is applied

Page 18: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

RecursionFirst, we need to define a function that checks if the input is a valid digit:

What if the input is not a single character?

Modern programming languages are what we call strongly-typed – meaning the input to the function must be of a specific type. Thus, this function will not even accept another type of input, and thus those situations will not even be considered, and thus they can never return true!

We will discuss this more later in the course.

Page 19: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Now we can define the recursive function number as follows:

First, we need to define a function that checks if the input is a valid digit:

Page 20: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

An empty sequence of characters is not a valid number

Now we can define the recursive function number as follows:

First, we need to define a function that checks if the input is a valid digit:

Page 21: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A sequence of a single character is a valid number if that character is a valid digit

Now we can define the recursive function number as follows:

First, we need to define a function that checks if the input is a valid digit:

Page 22: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A sequence of n characters is a valid number if the first character is a valid digit and the rest of the sequence is in turn a valid number

Now we can define the recursive function number as follows:

First, we need to define a function that checks if the input is a valid digit:

Page 23: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

How is the function number recursive?

Page 24: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

How is the function number recursive?

number is a function that is defined in terms of itself

Page 25: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 26: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 27: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 28: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 29: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 30: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 31: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 32: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 33: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 34: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 35: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Here’s how number(“3047”) comes out to be true

Page 36: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

What would you have to change to make this function accept any sequence of binary digits (that is, any sequence of 1’s and 0’s)?

Page 37: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

What would you have to change to make this function accept any sequence of binary digits (that is, any sequence of 1’s and 0’s)?

Page 38: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 39: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

STRINGS

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

A string such as “page” is constructed as follows

Page 40: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

STRINGS

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

A string such as “page” is constructed as follows

Page 41: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

The string “page”

STRINGS

A string such as “page” is constructed as follows

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 42: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

is made up of

STRINGS

A string such as “page” is constructed as follows

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 43: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

the character ‘p’

STRINGS

A string such as “page” is constructed as follows

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 44: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

attached to

STRINGS

A string such as “page” is constructed as follows

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 45: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

the string “age”

STRINGS

A string such as “page” is constructed as follows

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

Page 46: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

LISTS

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

A list such as [3,7,10,22] is constructed as follows

Page 47: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

The list [3,7,10,22]

LISTS

A list such as [3,7,10,22] is constructed as follows

Page 48: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

is made up of

LISTS

A list such as [3,7,10,22] is constructed as follows

Page 49: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

the object 3

LISTS

A list such as [3,7,10,22] is constructed as follows

Page 50: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

attached to

LISTS

A list such as [3,7,10,22] is constructed as follows

Page 51: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Let the symbol '::=' stand for “is made up of” and the symbol ':' stand for “attached to”

the list [7,10,22]

LISTS

A list such as [3,7,10,22] is constructed as follows

Page 52: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 53: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 54: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 55: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 56: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 57: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 58: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

The base case

Page 59: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 60: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 61: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 62: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 63: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

Page 64: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

Recursion must at some point end – there must be a base case

The base case

Page 65: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

Page 66: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 67: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

= =One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 68: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

31

=

=

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

=

Page 69: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

31

25

==

=

=One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 70: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

18 31

25

=

==

==

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 71: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

18 31

16 25

=

= ===

=

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 72: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

18 31

16 25 43

=

= = ====

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

Page 73: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

As we will see later in the course, trees are data structures that have important applications in computer science

22

18 31

16 25 43

=

= = ====

One special type of a tree is the binary tree, which is either empty (we say it is a null tree), or if not empty it is a tree that has a node with left and right (sub)trees

This is the root node of the right subtree

Page 74: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

All recursive objects have a base case and a construction case

Page 75: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A string is either empty or it is a character attached to another string

All recursive objects have a base case and a construction case

Page 76: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A string is either empty or it is a character attached to another string

A list is either an empty list or it is some element attached to another list

All recursive objects have a base case and a construction case

Page 77: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A string is either empty or it is a character attached to another string

A list is either an empty list or it is some element attached to another list

A number is either 0 or it is 1 plus some other number

All recursive objects have a base case and a construction case

Page 78: Intro to computer science   module 4a

Recursion

Introduction to Computer Science – Preliminaries

A string is either empty or it is a character attached to another string

A list is either an empty list or it is some element attached to another list

A number is either 0 or it is 1 plus some other number

A binary tree is either null or it is a node with left and right trees

All recursive objects have a base case and a construction case

Page 79: Intro to computer science   module 4a

The List Data Structure

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

Page 80: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The list could be empty

The List Data Structure

Page 81: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The list could have at least one object e followed by the rest of the list es

The List Data Structure

Page 82: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The first pattern matches only the empty list. The second pattern matches actual lists as follows:

The List Data Structure

Page 83: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The first pattern matches only the empty list. The second pattern matches actual lists as follows:

The List Data Structure

Page 84: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The first pattern matches only the empty list. The second pattern matches actual lists as follows:

The List Data Structure

Page 85: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

In a class of programming languages – known as functional programming languages a list matches any of two patterns:

The first pattern matches only the empty list. The second pattern matches actual lists as follows:

The List Data Structure

Page 86: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

Page 87: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

17 0 22 1

Page 88: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

17 0 22 1

Page 89: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

17 0 22 1

Page 90: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

17 0 22 1

Page 91: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

One way to visualize how the list data structure is implemented is to see it as a collection of nodes that are pointing to each other

The List Data Structure

17 0 22 1

Page 92: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

Page 93: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

Page 94: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

A list of numbers

Page 95: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

A list of pairs where each pair is a character and a number

Page 96: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

A list of strings

Page 97: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

A list of ‘lists of numbers’

Page 98: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

The list data structure can hold objects of any type, including lists

The List Data Structure

In most pure functional programming languages the objects in the list must be of the same type

A list of triples, where each triple has a number, a list of strings and a character

Page 99: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

When writing functions (programs) that take these recursive structures as inputs, it is natural to traverse these structures the way they were created, namely, recursively.

Recursive programs – an introduction

Page 100: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

When writing functions (programs) that take these recursive structures as inputs, it is natural to traverse these structures the way they were created, namely, recursively.

Recursive programs – an introduction

Page 101: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

When writing functions (programs) that take these recursive structures as inputs, it is natural to traverse these structures the way they were created, namely, recursively.

Recursive programs – an introduction

Since there are two distinct possibilities of the input, namely an empty list, or a list that has at least one element, we must consider these two possibilities:

Page 102: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

When writing functions (programs) that take these recursive structures as inputs, it is natural to traverse these structures the way they were created, namely, recursively.

Since there are two distinct possibilities of the input, namely an empty list, or a list that has at least one element, we must consider these two possibilities:

Recursive programs – an introduction

Page 103: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Page 104: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

Page 105: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

The function length takes a list so we must consider the two possible input cases

Page 106: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

What is a reasonable value to return as the size when the list is empty?

Page 107: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

0 sounds a reasonable value to return as the size of an empty list

Page 108: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

What else should we do if we trust that the function length applied recursively on the rest of the list does work and returns the length of the rest?

Page 109: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Let us introduce a method by which recursive programs can be defined almost automatically

Recursive programs – an introduction

Let us start with the function length that computes the size of a list

If length applied on es returns the size of the rest, then the size of the entire list is just 1 more

Page 110: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 111: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 112: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 113: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 114: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 115: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 116: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Done! Start evaluating the final result

Page 117: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 118: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 119: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 120: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Page 121: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Using the same reasoning as above, can you complete the definition of the function sum that takes a list of numbers and returns the sum of all the numbers in the list?

Recursive programs – an introduction

Page 122: Intro to computer science   module 4a

Introduction to Computer Science – Preliminaries

Using the same reasoning as above, can you complete the definition of the function sum that takes a list of numbers and returns the sum of all the numbers in the list?

Recursive programs – an introduction

In the next module we will start writing some interesting functions and programs over a number of data structures

Page 123: Intro to computer science   module 4a

Introduction to Computer Science

END OF MODULE 4a

MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part b)

Next module