The aim for this tutorial is to get students to understand why we are studying programming language principles, types, structuring concepts and expressions.
1. Basic, Cobol and Fortran are archaic, but still among the most commonly used programming languages in the world. Why do you think this is?
Answer: There are several reasons why this is true. Main reasons are: they were the earliest high-level programming languages; there are many proven applications written in these languages; updates to these applications were made on the original codes rather than re-writing the code using a new modern language; they have a large number of library routines available which are developed over the years by experienced programmers. For example, NASA has large number of scientific routines written in Fortran, many business and financial applications are written in Cobol.
2. Why do you think that object-oriented languages such as Smalltalk, C++ and Java are becoming the most popular new languages?
Answer: Recently, object oriented (OO) languages have gained much popularity in computing industry. In these languages everything is based on classes of objects, an object being a variable that may be accessed only through operations associated with it. Main features of OO are inheritance, polymorphism and reusability. OOP corrects the fundamental flaw of imperative programming which is that global variables can potentially be accessed(and modified) by every part of the program. A problem can be analyzed and designed using an OO methodology (e.g., UML) and coded in OO programming languages.
3. In the first lecture, it was stated that a programming language should be universal, i.e. it should be able to compute anything than can be computed. Do you think this requirement is too strict?
Answer: This might seem to be a very strong requirement, but even a very small language can meet it: any language in which we can define recursive functions or do iteration will be universal. However, some languages are obviously more appropriate for certain applications than others.
4. Why are we concentrating on semantics in this course, rather than language syntax?
Answer: It is impossible to learn the syntax of every existing language as there are just too many (estimated at well over 4000). It is (relatively) easier to learn the semantics, or principles of programming languages and then apply these principles to the syntax of a language. For example, if we know that a loop is needed in certain situations, it is straightforward to look a language manual to find the desired syntax.
5. Why use structuring concepts such as Cartesian product and powersets to define a languages composite types?
Answer: Programming languages support a wide variety of data structures: tuples, records, unions, arrays, sets, strings, classes, lists, trees, relations etc. The variety may seem bewildering, but these types can ALL be understood in terms of the five structuring concepts. The structuring concepts are the principles and the specific types are the syntax.
6. Explain the relationship between types and sets.
Answer: Types correspond to the sets of values which they represent.
For example, the integer type can be represented by a giant set of
integers {..., -1, 0, 1, ...}. Taking this concept to the extreme,
we can say that types are just a short-hand was of specifying the
entire set of legal values that a variable can take.7. Consider the two sets Suit = {club, diamond, heart, spade} and Rank = {2,3,4,5,6,7,8,9, 10,11,12,13}. Show the result of applying the first three structuring concepts discussed in lectures to this set.
Answer:
Cartesian Product:
Suit X Rank = {(club, 2), (club, 3), (club, 4), ... , (spade, 12),
(spade, 13)}
Disjoint Union:
Suit + Rank = {Suit:club, Suit:diamond, Suit:heart, Suit:spade,
Rank:2, Rank:3, Rank:4, Rank:5, Rank:6, Rank:7,Rank:8,
Rank:9, Rank:10, Rank:11, Rank:12, Rank:13}
Mapping:
Suit --> Rank= { {club->2, diamond->2, heart->2, spade->2},
{club->2, diamond->2, heart->2, spade->3},
... ... ... ...
{club->13, diamond->13, heart->13, spade->13} }8. Analyse the types and expressions in C++ and answer the following:
(a) What is the set of values of each of the primitive types and what
is its cardinality?
(b) Can recursive types be defined? If so, how?
Answers:
(a) bool = {false, true}
integer = {..., -1, 0, 1, ...}
float = {..., -1.0, ..., 0.0, ..., 1.0, ...}
character = {'\0', ..., 'a', ..., 'z', ..., '\255'}
#bool = 2
#int = MAXINT - MININT (in Pascal)
#float = infinity?
#char = 255
b) Yes. Eg. lists, trees (need to add definitions for these)
lists: node_ptr head, current, next;
// where current is the current node and next is
// a pointer to the next node
trees: node_ptr head, current, left, right;
9. Consider the relationship between S - Truth-value and p(S). In what ways are they similar? Hint: think of S as the set of the three primary colours and think of their use in pixels.
Answer: Let S = {red, green, blue}, T = {0,1}, then S --> T is:
{ {red->0, green->0, blue->0},
{red->0, green->0, blue->1},
{red->0, green->1, blue->0},
{red->0, green->1, blue->1},
{red->1, green->0, blue->0},
{red->1, green->0, blue->1},
{red->1, green->1, blue->0},
{red->1, green->1, blue->1} }
p(s) is then:
{} {blue} {green} {green,blue} {red} {red,blue} {red,green}
{red,green,blue}
If you take the result of S-->T and replace the -> with multiplication
then S-->T and p(S) become virtually the same.10. Examine the following program. Assume static type checking. At which lines in the program will a type mismatch error be generated assuming type checking is by name? by structure?
line#
typedef int numericType;
typedef struct {
int dummy;
} intStructType;
int intVariable;
numericType numericVariable;
intStructType intStructVariable;
numericType foo(numericType variable) {
1 variable = intVariable;
2 if (variable < numericVariable)
3 variable = intStructVariable;
else
4 variable = numericVariable;
return variable;
}
void main() {
5 int tempVariable;
6 intVariable = 3;
7 tempVariable = foo(3);
}
Solution: By name, lines 1, 3 and 7. By structure, line 3.
11. Examine the following program. Assume dynamic type checking. At which lines in the program will a type mismatch error be generated (assume execution begins in main and if a type error occurs the resulting test fails, that is evaluates to false, but execution continues)?
line#
foo() {
1 if (variable < 10)
2 variable = 4;
else
3 variable = "harry";
return variable;
}
main() {
4 variable = "joe";
5 if (foo() < 23)) print "success!";
}
Solution: Only lines 1 and 5 have type errors, when the string "joe" is compared to 10, and when "harry" is returned by foo and compared to 23.
12. What is the conditional expression in C? What is the conditional command in C? Are two conditionals really needed?
Solution:
The conditional expression is the question mark operator, e.g.max = (x < y)? y : x;
The conditional command is the if or switch statement. They serve different purposes and so both are needed in the sense that their functionality does not overlap.