Upload
walid-saba
View
127
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Revision uploaded March 14, 2014 This is module 3 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
Foundations of Computer Science and Software Engineering
module 3/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 3 (1 hour)
MATHEMATICAL PRILIMENARIES – SETS, RELATIONS AND FUNCTIONS
Foundations of Computer Science and Software Engineering
Sets
Introduction to Computer Science – Preliminaries
A set is an unordered collection of distinct objects
Sets
Introduction to Computer Science – Preliminaries
A set is an unordered collection of distinct objects
The following is a set that contains 4 distinct objects (in this case numbers)
Sets
Introduction to Computer Science – Preliminaries
A set is an unordered collection of distinct objects
Although we have to list the objects in some order, the order is not important at all
The following is a set that contains 4 distinct objects (in this case numbers)
Sets
Introduction to Computer Science – Preliminaries
A set is an unordered collection of distinct objects
Although we have to list the objects in some order, the order is not important at all
The following is a set that contains 4 distinct objects (in this case numbers)
Since objects in a set are unordered, the sets A, B and C are equal
Sets
Introduction to Computer Science – Preliminaries
A set is an unordered collection of distinct objects
Although we have to list the objects in some order, the order is not important at all
The following is a set that contains 4 distinct objects (in this case numbers)
It is therefore useful to visualize the sets A, B and C as a Venn diagram
Since objects in a set are unordered, the sets A, B and C are equal
Sets
Introduction to Computer Science – Preliminaries
Objects are collected in a set for some purpose.
Sets
Introduction to Computer Science – Preliminaries
Objects are collected in a set for some purpose. Here are some examples:
Set of countries that have a permanent seat in the United Nations Security Council (countries with veto power)
Sets
Introduction to Computer Science – Preliminaries
Objects are collected in a set for some purpose. Here are some examples:
Set of countries that have a permanent seat in the United Nations Security Council (countries with veto power)
Set of US presidents that died while in office
Sets
Introduction to Computer Science – Preliminaries
A set can be defined extensionally, by actually enumerating all the objects
EXAMPLES
Sets
Introduction to Computer Science – Preliminaries
A set can be defined extensionally, by actually enumerating all the objects
The set of prime numbers that are less than 19 (a prime number is a number that is divisible by itself and 1 only)
EXAMPLES
Sets
Introduction to Computer Science – Preliminaries
A set can be defined extensionally, by actually enumerating all the objects
EXAMPLES
The set of actors that starred in The Untouchables movie
The set of prime numbers that are less than 19 (a prime number is a number that is divisible by itself and 1 only)
Sets
Introduction to Computer Science – Preliminaries
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
A is the set of
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
A is the set of all objects x
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
A is the set of all objects x such that
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
A is the set of all objects x such that the property (or predicate) P is true of x
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
EXAMPLES
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
EXAMPLESThe set of all objects x such that x is an even number that is greater or equal to 2 and less than or equal to 8
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
EXAMPLES
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
EXAMPLES
The set of all pairs (x,y) such that x is an even number between 1 and 9 and y is the square of x
A set is often defined by comprehension – that is, by some predicate
Sets
Introduction to Computer Science – Preliminaries
EXAMPLES
A set is often defined by comprehension – that is, by some predicate
Set Membership
Introduction to Computer Science – Preliminaries
Objects either belong to (are members of) a set, or they are not.
Set Membership
Introduction to Computer Science – Preliminaries
Objects either belong to (are members of) a set, or they are not.
When an object e belongs to (or is a member of) a set S, we write
Set Membership
Introduction to Computer Science – Preliminaries
Objects either belong to (are members of) a set, or they are not.
When an object e belongs to (or is a member of) a set S, we write
If an object e does not belong to a set S, we write
Set Membership
Introduction to Computer Science – Preliminaries
Objects either belong to (are members of) a set, or they are not.
When an object e belongs to (or is a member of) a set S, we write
If an object e does not belong to a set S, we write
In advanced computer science curses you will encounter another kind of set membership that is not crisp (yes, or no), but fuzzy!
For example, in defining membership in the set of BIRDS, an eagle might be considered a member with degree 1.0, while a penguin might be considered a member with degree 0.6 only
Fuzzy sets and fuzzy membership are beyond the scope of this course, but reading a high-level introduction to the subject is not a bad idea
Empty Set
Introduction to Computer Science – Preliminaries
What happens if the predicate (or property) that defines membership in the set is not true of any object?
Empty Set
Introduction to Computer Science – Preliminaries
What happens if the predicate (or property) that defines membership in the set is not true of any object?
Consider these sets:
Empty Set
Introduction to Computer Science – Preliminaries
What happens if the predicate (or property) that defines membership in the set is not true of any object?
Consider these sets:
Empty Set
Introduction to Computer Science – Preliminaries
What happens if the predicate (or property) that defines membership in the set is not true of any object?
Consider these sets:
Now what members belong to this set?
Empty Set
Introduction to Computer Science – Preliminaries
It turns out that the set is an empty set – i.e., it has no members, since there are no objects that are members of D and also members of E
Empty Set
Introduction to Computer Science – Preliminaries
It turns out that the set is an empty set – i.e., it has no members, since there are no objects that are members of D and also members of E
When a set B is empty we write:
The Universal Set
Introduction to Computer Science – Preliminaries
Unlike the empty set, which has no members, the Universal Set, often denoted by U, is the set that has all objects as members, including itself – its is the set of all objects in the domain of discourse
The Universal Set
Introduction to Computer Science – Preliminaries
Unlike the empty set, which has no members, the Universal Set, often denoted by U, is the set that has all objects as members, including itself – its is the set of all objects in the domain of discourse
Graphically, we usually depict sets and the Universal Set as follows:
Set Operations
Introduction to Computer Science – Preliminaries
Note the figure below.
Set Operations
Introduction to Computer Science – Preliminaries
Note the figure below.
o C is entirely contained in the set A (we say that C is a subset of A)
Set Operations
Introduction to Computer Science – Preliminaries
Note the figure below.
o C is entirely contained in the set A (we say that C is a subset of A)o E and B have something in common (their intersection is the set H)
Set Operations
Introduction to Computer Science – Preliminaries
Note the figure below.
o C is entirely contained in the set A (we say that C is a subset of A)
o if we take the union of A and F, it will also contain all the elements of Co E and B have something in common (their intersection is the set H)
Set Operations
Introduction to Computer Science – Preliminaries
SUBSET
A set R is a subset of another set S if every element of R is also an element of S.
We write
We can state this in first-order logic (FOL) as follows:
Set Operations
Introduction to Computer Science – Preliminaries
SUBSET
A set R is a subset of another set S if every element of R is also an element of S.
We write
We can state this in first-order logic (FOL) as follows:
What the above says is the following:R is a subset of S if and only if any x that belongs to R also belongs to S
Set Operations
Introduction to Computer Science – Preliminaries
We can state this in first-order logic (FOL) as follows:
UNION
The union of a set R and a set S is a set that has the elements that are in R and those that are in S
We write
Set Operations
Introduction to Computer Science – Preliminaries
UNION
The union of a set R and a set S is a set that has the elements that are in R and those that are in S
We write
We can state this in first-order logic (FOL) as follows:
What the above says is the following:The union of R and S contains any x that is an element of R or an element of S
Set Operations
Introduction to Computer Science – Preliminaries
We can state this in first-order logic (FOL) as follows:
INTERSECTION
The intersection of A and B is a set that has all the elements that are in both, A and B
We write
Set Operations
Introduction to Computer Science – Preliminaries
We can state this in first-order logic (FOL) as follows:
What the above says is the following:The intersection of R and S contains any x that is in both R, and S
INTERSECTION
The intersection of A and B is a set that has all the elements that are in both, A and B
We write
Set Operations
Introduction to Computer Science – Preliminaries
We can state this in first-order logic (FOL) as follows:
COMPLEMENT
The complement of a set A is a set that contains all the elements that are not in A
We write
Set Operations
Introduction to Computer Science – Preliminaries
We can state this in first-order logic (FOL) as follows:
What the above says is the following:The complement of a set A is a set that has all the elements that are not in A
COMPLEMENT
The complement of a set A is a set that contains all the elements that are not in A
We write
Set Operations
Introduction to Computer Science – Preliminaries
For example, if S = {x,y,z} then P(S) = {{x,y,z},{x,y},{x,z},{y,z},{x},{y},{z},{ }}
If |S| is the size of a set S, then |P(S)| = 2 | S|
The Power Set of a set S, denoted to by P(S), or 2S, is the set of all subsets of S
Correspondence between Set Theory and Logic
Introduction to Computer Science – Preliminaries
Introduction to Computer Science – Preliminaries
Correspondence between Set Theory and Logic
Introduction to Computer Science – Preliminaries
Correspondence between Set Theory and Logic
Introduction to Computer Science – Preliminaries
Correspondence between Set Theory and Logic
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us assign truth values for R and S – where T means we are in the set
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us assign truth values for R and S – where T means we are in the set
T T TIt possible to be in R and S at the same time
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us assign truth values for R and S – where T means we are in the set
T T T
F T T
It possible to be in S but not in R
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us assign truth values for R and S – where T means we are in the set
T T T
F T TF F T
It is also possible not to be in R and not to be in S
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Introduction to Computer Science – Preliminaries
Set Theory Explanation of Implication
Recall also that corresponds to
Let us assign truth values for R and S – where T means we are in the set
T T TT F FF T TF F T
But it is not possible to be in R and not in S at the same time
Let us re-visit the truth table of the implication logical operator, where R => S is false in one case only, namely when R is true and S is false
Sets – An Example
Introduction to Computer Science – Preliminaries
Suppose that in a certain school we have the following data:
Suppose the school is holding a Christmas party and we want to make sure we have a tag for everyone in the school. How many tags should we make?
Sets – An Example
Introduction to Computer Science – Preliminaries
Suppose that in a certain school we have the following data:
We need a tag for every student, teacher and staff member. The entire set is
Suppose the school is holding a Christmas party and we want to make sure we have a tag for everyone in the school. How many tags should we make?
Sets – An Example
Introduction to Computer Science – Preliminaries
OPERATIONS
= 11
DATA
Sets – An Example
Introduction to Computer Science – Preliminaries
OPERATIONS
= 11
DATA
(1) How would you find out the number of presents the school should prepare if the school is giving a present to every teacher and every staff member?
(2) How can we know the names of those that will receive more than one present?
Relations
Introduction to Computer Science – Preliminaries
Relations
Introduction to Computer Science – Preliminaries
Cartesian product is an operation that returns a set which is the product between two or more sets.
CARTESIAN PRODUCT
For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y
Relations
Introduction to Computer Science – Preliminaries
Cartesian product is an operation that returns a set between two or more sets.
CARTESIAN PRODUCT
For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y
Formally, we have the following:
Relations
Introduction to Computer Science – Preliminaries
Cartesian product is an operation that returns a set between two or more sets.
CARTESIAN PRODUCT
For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y
Formally, we have the following:
Note that, in general,
Relations
Introduction to Computer Science – Preliminaries
Cartesian product is an operation that returns a set between two or more sets.
CARTESIAN PRODUCT
For example, the Cartesian product between two sets X and Y, denoted as X x Y is the set of all pairs (x,y) where x is from X and y is from Y
Formally, we have the following:
Note that, in general,
If |X| is the size of X and |Y| is the size of Y then |X x Y| is |X|.|Y|
Relations
Introduction to Computer Science – Preliminaries
A relation between two sets is some subset of the Cartesian product between the two sets
Relations
Introduction to Computer Science – Preliminaries
A relation between two sets is some subset of the Cartesian product between the two sets
For example, assuming that N is the set of natural numbers, and S is the set of all possible words in English, then the following is one possible relation that is a subset of S x N
R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }
Relations
Introduction to Computer Science – Preliminaries
A relation between two sets is some subset of the Cartesian product between the two sets
R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }
This relation might be used to pair strings with their size (number of characters in the string)
For example, assuming that N is the set of natural numbers, and S is the set of all possible words in English, then the following is one possible relation that is a subset of S x N
Visualizing a Relation
Introduction to Computer Science – Preliminaries
R = { (“Hendrix”, 7), (“Dylan”, 5), (“Marley”, 6), (“Morrison”, 8) }
SN
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Recall the quantifiers of first-order logic:
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Recall the quantifiers of first-order logic:
This says that for all x, R is true of x (or, R is true of any x)
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Recall the quantifiers of first-order logic:
This says that for all x, R is true of x (or, R is true of any x)
This says that there exists some x that R is true of (or, R is true of some x)
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Recall the quantifiers of first-order logic:
But what if the property (or predicate) R is a 2-place property, or a property that is true of two variables?
This says that for all x, R is true of x (or, R is true of any x)
This says that there exists some x that R is true of (or, R is true of some x)
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Recall the quantifiers of first-order logic:
But what if the property (or predicate) R is a 2-place property, or a property that is true of two variables?
The predicate R here is true of some x and y if x and y are related to each other by R – or, if the pair (x,y) is in the relation that corresponds to R
This says that for all x, R is true of x (or, R is true of any x)
This says that there exists some x that R is true of (or, R is true of some x)
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
john knows mary
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
carlos likes pineapple
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
Ottawa is the capital of Canada
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
Length of the string “abba” is 4
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
For example, we can have the following 2-place predicates:
BMW is made in Germany
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
Suppose we have a relation Likes that might hold between some x and some y, where x is from some set X and y is from set Y
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
When we have 2 variables and more than the order of the quantifiers is important
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
When we have 2 variables and more than the order of the quantifiers is important
Here there is at least one x that is related to some (at least one) y
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
When we have 2 variables and more than the order of the quantifiers is important
Here every x is related to some (at least one) y
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
When we have 2 variables and more than the order of the quantifiers is important
Here there is at least one x that is related to every y (at least one person likes all fruit types!)
Relations and Quantifiers in First-Order Logic
Introduction to Computer Science – Preliminaries
When we have 2 variables and more than the order of the quantifiers is important
Here we are saying that every x is related to every y (everyone likes all fruits types). Can you complete the picture of this relation?
Properties of Relations
Introduction to Computer Science – Preliminaries
A relation R can have any of these properties
Properties of Relations
Introduction to Computer Science – Preliminaries
A relation R can have any of these properties
Properties of Relations
Introduction to Computer Science – Preliminaries
A relation R can have any of these properties
Properties of Relations
Introduction to Computer Science – Preliminaries
A relation R can have any of these properties
Properties of Relations
Introduction to Computer Science – Preliminaries
AN EXAMPLE
The subset relation on sets is
Properties of Relations
Introduction to Computer Science – Preliminaries
AN EXAMPLE
The subset relation on sets is
sr t
Properties of Relations
Introduction to Computer Science – Preliminaries
AN EXAMPLE
The subset relation on sets is
Why is the subset relation not symmetric?
sr t
Properties of Relations
Introduction to Computer Science – Preliminaries
Consider the relation (‘less than or equal to’) on N x N
Here are some pairs in this relation
= {(0, 1), (0, 2), (0, 3), ... (1, 2), (1, 3), (1, 4), (1, 5), ... (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), ... ... }
Is this relation reflexive? Symmetric? Transitive?What about the equality (=) relation on natural numbers?
Relations in Data Modeling
Introduction to Computer Science – Preliminaries
Consider the following sets, that represent certain data objects:
Relations in Data Modeling
Introduction to Computer Science – Preliminaries
Consider the following sets, that represent certain data objects:
Several relations can be defined on these sets to represent, for example, data about employees in a certain organization:
Relations in Data Modeling
Introduction to Computer Science – Preliminaries
These relations might have the following data stored in them:
Company Name Age
Company City
Relations in Data Modeling
Introduction to Computer Science – Preliminaries
These relations might have the following data stored in them:
Company Name Age
Company City
Note that a relation on sets is itself a set: a set of pairs, triples, etc.. In mathematics we call these objects tuples. In database systems we call these objects records, and we call the relations tables
Relations in Data Modeling
Introduction to Computer Science – Preliminaries
These relations might have the following data stored in them:
Company Name Age
Company City
LOOKING AHEAD
These two relations are connected somehow (they share one data object, namely the company an employee works for)
Given these tables/relations, how would we get the list of employees that work in Tokyo?
Functions
Introduction to Computer Science – Preliminaries
A function is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
An example function that maps objects in some set to their color
A function is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(0) = 0
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(1) = 2
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
f(0) = 0
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(2) = 4f(1) = 2f(0) = 0
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(3) = 6f(2) = 4f(1) = 2f(0) = 0
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0
Can you guess what this function is doing?
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
012345...
0123456789
1011...
X: input
Y: output
f(5) = 10f(4) = 8f(3) = 6f(2) = 4f(1) = 2f(0) = 0
Can you guess what this function is doing?The function seems to map every number to its double ...
A function f is a relation between a set of inputs X and a set of outputs Y, or a map from some domain X to another domain Y
Functions
Introduction to Computer Science – Preliminaries
A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs
Functions
Introduction to Computer Science – Preliminaries
A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs
In engineering, machine is used as a metaphor for function since it takes some objects as input, does something with them, and produces new objects
Functions
Introduction to Computer Science – Preliminaries
A function is thus a relation from a set of inputs to a set of outputsA function is sometimes also called a map, since it maps inputs to outputs
In engineering, machine is used as a metaphor for function since it takes some objects as input, does something with them, and produces new objects
In computer science, a function is a program or a procedure
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare3
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare3 9
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare3 9 18
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare5
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare5 25
Functions
Introduction to Computer Science – Preliminaries
The output of a function is some value, and so it can be the input to another function
DoubleSquare5 25 50
Functions
Introduction to Computer Science – Preliminaries
Functions can have any number of inputs:
Functions
Introduction to Computer Science – Preliminaries
x y
f1 : if (x > y) then x else y
Functions can have any number of inputs:
Functions
Introduction to Computer Science – Preliminaries
f2 : (x * x) + (y * y)
x y
f1 : if (x > y) then x else y
x y
Functions can have any number of inputs:
Functions
Introduction to Computer Science – Preliminaries
f2 : (x * x) + (y * y)
x y
f1 : if (x > y) then x else y
x y
What do you thing the function f1 does?
Functions can have any number of inputs:
Functions
Introduction to Computer Science – Preliminaries
f2 : (x * x) + (y * y)
x y
f1 : if (x > y) then x else y
x y
What do you thing the function f1 does?What about the function f2?
Functions can have any number of inputs:
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
For example, suppose we want a function that takes a string and return
1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
For example, suppose we want a function that takes a string and return
1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates
f“santana”
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
We can return a single output value, which is a triple that contains the three values
f“santana”
For example, suppose we want a function that takes a string and return
1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
We can return a single output value, which is a triple that contains the three values
f“santana” (“anatnas”, 7, “sant”)
For example, suppose we want a function that takes a string and return
1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates
Functions
Introduction to Computer Science – Preliminaries
What if we want to return more than a single output value for the same input?
We can return a single output value, which is a triple that contains the three values
f“santana” (“anatnas”, 7, “sant”)
For example, suppose we want a function that takes a string and return
1- the reverse of the string,2- the size of the string (number of characters in the string), and3- the original string with no duplicates
In some programming languages these tuples (pairs, triples, 4-tuples, etc.) are exactly as you see it here. In other languages, however, we need to define a structure that holds this triple. This is done as follows:
structure Triple{ string reverseOfString, integer numberOfCharactersInString, string stringWithNoDuplicates}
Conditions in Functions
Introduction to Computer Science – Preliminaries
In most cases functions compute an output value in several ways, based on some conditions of the input
For example, the max function, which returns the maximum of two values, uses an if-then-else construct:
max (x,y) = if (x > y) then return x else return y
Conditions in Functions
Introduction to Computer Science – Preliminaries
As another example, recall the truth table of the logical and operator
Conditions in Functions
Introduction to Computer Science – Preliminaries
As another example, recall the truth table of the logical and operator
and (x,y) = if ((x == T) & (y == T)) then return T else return F
A function to compute logical and using if-then-else
Conditions in Functions
Introduction to Computer Science – Preliminaries
As another example, recall the truth table of the logical and operator
and (x,y) = if ((x == T) & (y == T)) then return T else return F
A function to compute logical and using if-then-else
and T T = Tand x y = F
In some languages we can handle the different input cases by doing pattern-matching on the input values
Conditions in Functions
Introduction to Computer Science – Preliminaries
As another example, recall the truth table of the logical and operator
and (x,y) = if ((x == T) & (y == T)) then return T else return F
A function to compute logical and using if-then-else
and T T = Tand x y = F
In some languages we can handle the different input cases by doing pattern-matching on the input valuesIn these languages cases are tested in the order they appear. If
the input does not match one case, the next case is applied.
1) Try different inputs on this function2) Define the logical OR function in the same manner
Exercises for Modules 1, 2 and 3
Introduction to Computer Science – Preliminaries
1. Devise a coding scheme to represent simple text in binary code (i.e., as a sequence of 1’s and 0’s). You can assume that the text will contain only lower case characters of the English alphabet, the period (‘.‘), the comma (‘,‘) and the space (‘ ‘) character. According to your coding scheme, how many bits would be required to code a piece of text that has 100 characters.
2. Consider the following statements:a) Animals that have wings flyb) Eagles have wingsc) John owns a pet eagle d) The name of the eagle John owns is ‘Clay’
2.1 Represent the above statements in first-order logic (FOL)2.2 Show/prove using logical inefrencing rules that Clay flies
3. Show/prove using the inference rules of logic that the following are true:a) (A U (B \ C)) = (A U B) \ (A U C)
b) If (A µ B) and (B µ A) then A = B
c) if (A U B) = A then (A µ B)
4. Suppose we have an operation called difference (‘ – ‘) such that (A – B) returns a set that has all the elements that are in A but in B. How can you get the intersection of A and B using this operation only?
Introduction to Computer Science
END OF MODULE 3
MATHEMATICAL PRILIMENARIES – RECURSION AND INDUCTIVE STRUCTURES (part a)
Next module