The Department of Computer Science & Engineering
 STUART C. SHAPIRO: CSE 305

# Preliminaries

In class survey: What programming languages do you know?

Course Objectives
• You will understand the various components of programming languages, and the available design choices.
• You will expand your notions of how procedures may be expressed.
• You will increase your programming abilities.
• You will be able to learn new programming languages easily.
• You will be able to participate in the design of new programming languages.

Basic Concepts
• The computer as a linear array of addressable sets of binary switches
Like other machines: set the switches; turn it on; it does something.
A group of bits can represent an: integer, real, character, truth value, etc. It's all in the interpretation.

• The von Neumann architecture (equivalence of data and program)
Programs can be treated as data. Some data can be stored or modified, and then executed as program.
Enables high-level programming languages.

• Compilation, Interpretation, and Hybrid (Compilation & Interpretation)

• What's a programming language anyway?
• A well-defined language,
• for which there exists an interpreter or compiler (or hybrid) for at least one computer,
• that has a means for specifying input,
• and a means for specifying output,
• and at least one basic operation on data,
• and a means for specifying the performance of a sequence of operations,
• and a means for specifying the conditional performance of one or another operation,
• and a means for specifying the repetitive performance of an operation.

Key Distinctions
• Standards vs. Manuals
Language standards are written for implementors. They say what different operations must do, but may also explicitly state that some operation is undefined in some way. For example,
6.3.1.4 Real floating and integer
...
[#1] When a finite value of real floating type is converted to integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
[ISO C Standard JTC1/SC22/WG14 N843]

Manual are written for programmers. They should specify all the operations, but some specifications may be implementation-dependent.

• Space vs. Time
For example, what is the statement after statement `A` in the below C program?
```#include <stdio.h>

void sub()
{
int x = 2;
printf("In sub, x = %d\n", x);
}

int main()
{
int x = 1;
printf("In main, x = %d\n", x);
A: sub();
printf("In main, x = %d\n", x);
return 0;
}```

• Compile time vs. Run time
For example, when is `x` given the value `1` in the above C program?

Overview of Language Categories
There are two orthogonal sets of categories:
• Imperative vs. Functional vs. Logic
• Procedural vs. Object-Oriented

• Imperative
The characteristic feature of an imperative programming language is the assignment statement. Here is a pair of C functions that use insertion sort to sort an array.
```void insert(int x, int a[], int open, int max) {
int i;
for (i = open; i < max; i++) {
if (x <= a[i+1]) {
a[i] = x;
return;
}
else {
a[i] = a[i+1];
}
}
a[max] = x;
}

void isort(int a[], int max) {
int i;
if (max >= 1) {
for (i = max-1; i >= 0; i--) {
insert(a[i], a, i, max);
}
}
}
```
• Functional
The characteristic feature of a functional programming language is the conditional expression. Here is a pair of Common Lisp functions that use insertion sort to sort a list.
```(defun isort (list)
(if (or (endp list)
(endp (rest list)))
list
(insert (first list) (isort (rest list)))))

(defun insert (x list)
(cond ((endp list)
(list x))
((<= x (first list))
(cons x list))
(t (insert (first list)
(insert x (rest list))))))
```
• Logic
The characteristic feature of a logic programming language is parameter binding by pattern matching. Here is a pair of Prolog procedures that use insertion sort to sort a list.
```isort([], []).
isort([X], [X]).
isort([First | Rest], Sorted) :- isort(Rest, Srest), insert(First, Srest, Sorted).

insert(X, [], [X]).
insert(X, [Y | Z], [X, Y | Z]) :- X =< Y.
insert(X, [Y | Z], Result) :- insert(X, Z, Inter), insert(Y, Inter, Result).
```
• Procedural
The major structure of a procedural program is a procedure that takes some data and does something with them.

• Object-Oriented
The major structure of an object-oriented program is a class of objects with some behaviors defined for them.

This course will not be structured by these categories, but by PL components that all, or most, PLs have.