Intro to computer science module 4a

Post on 13-Dec-2014

60 views 0 download

Tags:

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.)

Transcript of Intro to computer science module 4a

Foundations of Computer Science and Software Engineering

module 4a/30

WALID S. SABA

Foundations of Computer Science and Software Engineering

ALBERT EINSTIEN

TO THE TEACHER

Foundations of Computer Science and Software Engineering

TO THE STUDENT

SOCRATES

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

MODULE 4 (1 hour)

Foundations of Computer Science and Software Engineering

MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part 1)

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;

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

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:

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

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?

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?

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 ...

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

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:

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:

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

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.

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:

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:

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:

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:

Recursion

Introduction to Computer Science – Preliminaries

How is the function number recursive?

Recursion

Introduction to Computer Science – Preliminaries

How is the function number recursive?

number is a function that is defined in terms of itself

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

Introduction to Computer Science – Preliminaries

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

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)?

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)?

Recursion

Introduction to Computer Science – Preliminaries

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

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

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

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”

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”

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”

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”

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”

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

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

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

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

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

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

The base case

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

Recursion

Introduction to Computer Science – Preliminaries

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

The base case

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

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

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

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

=

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

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

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

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

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

Recursion

Introduction to Computer Science – Preliminaries

All recursive objects have a base case and a construction case

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

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

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

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

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:

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

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

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

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

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

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

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

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

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

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

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

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

Introduction to Computer Science – Preliminaries

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

The List Data Structure

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

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

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

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

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’

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

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

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

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:

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

Introduction to Computer Science – Preliminaries

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

Recursive programs – an introduction

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

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

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?

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

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?

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

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Done! Start evaluating the final result

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

Introduction to Computer Science – Preliminaries

How does this function work?

Recursive programs – an introduction

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

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

Introduction to Computer Science

END OF MODULE 4a

MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part b)

Next module