Introduction To ALGOL 60 For Those Who Have Used Other Language
Introduction To ALGOL 60 For Those Who Have Used Other Language
1971
Recommended Citation
.
This Technical Report is brought to you for free and open access by the School of Computer Science at Research Showcase @ CMU. It has been
accepted for inclusion in Computer Science Department by an authorized administrator of Research Showcase @ CMU. For more information, please
contact [email protected].
NOTICE WARNING CONCERNING COPYRIGHT RESTRICTIONS:
The copyright law o f the United States (title 17, U.S. Code) governs the making
o f photocopies or other reproductions o f copyrighted material. Any copying o f this
document without permission of its author may be prohibited by law.
Introduction to ALGOL 60
for those who have used
other programming languages
by A. N. Habermann
September, 1971
Carnegie-Mellon University
Department of Computer Science
Pittsburgh, Pa. 15213
Introduction to ALGOL 60
for those who have used other programming languages
by A.N. Habermann
0. Introduction
1. Data structures
2« Control statements
3. Block structure
4. Procedures
5. Recursion
6. Call by name/value
-1-
0. Introduction
implementation details.
-2
1. Data structures
1.1 Constant
1.2 Variable
1.3 Expression
1.4 Array
1.5 Procedure
1.1 Constant
1.2 Variable
Note: transitions to a new line or a new page or next card have no meaning
integer i, j, k; real x, y, z;
Boolean a, b, c; integer n;
•4
i := j + 1 ; x := 2 * y + z; a := true
-3-
1.3 Expressions
unary operators - +
a - b * c t 2
a * - b is incorrect.
a * (b + c) a * (-b).
operands.
is of type real.
of type integer.
-4-
yields i = 3 .
1.3-.2 Relations
The operands are of type integer or real, the result is of type Boolean.
A and
V or
(binary operators)
-> imp
= eqv
na b A - ] C V x < y + z
table
a: = b A c = x > 0
-5-
1.4 Array
v [4] M [ 3,7] v [i + k] M [ i + 1, j - 2 ] .
v[i + j] := 2 * k + 1
1.5 Procedure
A programmer can also write his own procedures (like MAX and SWAP).
in section 4.
-7-
2. Control statements
by a semicolon (since new line, next card and new page have no
meaning).
v[i - 1] t2
n : = - 14;
begin end
begin k := v[i] ;
v[i] = v[18 - i ]
: ;
v[18 - i] = k
:
end
i := 12;
for i = i + 1 while i £ 20 do
: v[i] = SQRT (v[i])
s
while BE do S
for a : = a while BE do S
and b is computed by
while r / 0 do (or for a : = a while r / 0 do)
begin
q:= a -r b;
r:= a - q * b;
a:= b;
b:=r
end;
gcd:= a
Conditional statement
if BE then S
This statement prints the index and value of the elements unequal zero,
if BE then Si else S2
As a thumb rule one had better avoid always the sequences ... then if
The restrictions are not serious, since the problem can be avoided by
end
max := 0;
3. Blockstructure
block.
A block is a statement.
by else or end.
sum : = 0;
for k := 0 step 1 until 10 do
end
statements and so,no value can have been assigned to n in this block
using an outerblock:
end
end
READ(k); READ(y); i : = 3;
if x > y then
begin real i;
i : = x;
x = y;
:
••e
y : = i;
end;
i := i + 1
end
-13-
i does not exist any more. The list of defined names is in the
outerblock innerblock
real y real i
real x real y
integer i real x
////////// integer 1
///////////
top down for the first occurrence of that name. Thus, in the
4. Procedures
begin integer i, j;
end;
the declarator procedure, the procedure name, the (list of) formal
Procedure MREAD can easily be modified into the more general procedure
begin integer i, j;
end;
begin
end;
sum: = 0;
end;
" pr:= X - 2 * ( X T 2 ) / 0 ;
i:= 1;
PRIME: = p r
end;
assigned. The procedure name may only be used as a local of the body
i 12 ^ x and pr
said to have side effects. These may be very useful, but one should
begin integer i;
end
Recursion
From this definition one can derive that an identifier is a non empty
f (a,n) = a * f(a,n-l)
begin
if n == 1 then POWER: = a
end;
begin
end;
G,n = 1 for n ^ 0
n
for n 2> 0, 1 ^ i ^ n-1
-18-
begin
if i = 0 or i = n then PC0EF:=1
else PC0EF:= 0
end;
-19-
In the last example the formal parameters n and i are used several
The ALGOL 60 rule for using formal parameters is: when a procedure
PCOEF (a t 2 + 1, 3)
n is needed.
locn:= n; loci:= i;
else PCOEF:=0
end;
begin
if i = 0 or i f n then PCOEF:= 1
else PCOEF: = 0
end;
parameter (and not just its once computed value). This use of a
To clarify the difference of call by name and call by value this example:
begin
integer s; s:= 0;
VSUM:= s
-21-
begin
integer S; S:= 0;
NSUM:== S
end;
i:=5;
a:= VSUM(v[i]);
b:=NSUM(v[i])
end
When VSUM is called, v[5] is evaluated and u gets (only once) the
value 5. Variable a gets the value 500. When NSUM is executed v[i]
is substituted for u and the result is that b gets the value 5050*
Epilogue.
Various concepts of ALGOL 60 and several details have been left out
of this introduction. The most important ones left out ar^e: goto
description.
-22-
Bibliography on ALGOL 60
Manual
Definition
Philosophy
History-Bibliography
Introductory
T
-23-
Imp lamentation
Extensions
1. Data structures
+10.10&-4 -12.24&f3
42.1516 -0.07236
.541778 +.0321927
.23&-2 -.032&8
&5 -&-7
1.2 Variables
onethrunine a6
lthru9 a(6)
1.3 Expressions
a: 3.0
b: 4.0
c: 4.23
i: 3
k: 6
hit: true true
ment statements?
begin
integer i,j,k; real a,b,c; Boolean p,q;
a := 3.1415&H;
b := 2.718&-1;
c := 1.01;
2. Control statements
file
n n-1
a x + a -x + ... -f a,x + a^
n n-1 1 0
(4) Write a piece of program that computes the sum of the elements
(6) Write a piece of program that computes the inner product of two
vector of 45 components.
-27-
begin integer a, b, f, i; a := 0; b : 3
1;
for i := 2 step 1 until 25 do
begin f = a+b;
b = a;
a = f
end
end
-28-
vectors in A.
and B[0:9,0:11] and stores the result into matrix C of proper size.
3 Conditional repetition
F(x) = 0.01(x 4
+ 2x 3
- 3x 2
- 4)
k := i; next := i;
while A[next] > 0 do begin k : = next; next := A[k] end;
A[k] :« 0; A[next] :=* -1
-30-
(3) To assign the value true to Boolean variable "zero" if and only
i := 0; zero := false;
while i < 2048 and —i zero do
begin i := i+1; zero := v[i] - 0 end
(4) Write a program that gets a real number x as input and computes
U 1 z o
x^ ^ - x
+ - f(x^)/f (x^).
1
Write a program to approximate
f(x) » x - 6 .
3
(7) Write a program that computes the largest Fibonacci number less
than 8000.
(8) Write a program that joins all the queues of the example
presented earlier into one queue q^ leaving out the first elements
^2'^3 * * * *'^9*
-31-
0.5*(f(m) + f(mb))*(b-a)/4 +
0.5*(f(mb) + f (b))*(b-a)/4
or
i2 := i(l)/2 + (f(am) + f (mb))*(b-a)/4
where am is the midpoint of segment (a,m) and bm of segment (m,b).
Write a program that computes Integral(x*sin(x),0,1) until two
(1) Vector v[l:100] has positive elements. To compute the value of the
largest element
end
-32-
(2) a[l:50] and b[l:50] are sets of integer numbers. The sets are
called "disjunct" if for all i,j - (1,...,50) either a[i] < b[j]
i := 1; ascending := true;
while i £ 512 and ascending do
begin i i+1;
if v[i] < v[i-l]
then begin i := i-1; ascending false end
end
that computes the minimum value of these numbers and how many
one statement
if a > b
then begin if a > c
then begin if a > d then max :» a
else max := d
end
else if. c > d then max := c
else max := d
end
else if b > c
then begin if b > d then max := b
else max := d
end
else if c > d then max : = c
else max := d
-34-
max := a;
ranging from 1 to 100. The results are regrouped into four cate-
are at 90, 70 and 50. To program how many of each category there
are:
(3) Does the following program compute correctly the greatest common
T
-35-
(4) Let f(x) be a continuous function on interval [a,b] and let f(x)
that part in which the zero point lies. Write a piece of pro-
4 3 2
method.
if i ^ 100
then begin if a[i] > 0
then begin PRINT(a[i]); i := i+1 end
else stop := true
end
Block structure
(1) The first number on an input file represents the number of components
begin integer n;
READ(n);
begin real array u,v[l:n]; integer i; real inprod;
for i := 1 step 1 until n do READ(u[i]);
for i := 1 step 1 until n do
begin
READ(v[i]);
inprod := inprod + v[i]*u[i]
end;
end
end
variables.
to solve A*x = e
a. . 4 0 for all 1 £ i £ n.
11 '
B[m] :« 0
end
end
end
end
and
end
-39-
A*x = c
address (zero for the last segment); the second contains the
first segment. Segments are added and deleted and so, after a
program that moves the segments to one end of array A (and up-
dates the links properly) such that only one free area results
(7) The Boolean product and Boolean sum of two integers represented
0 * 0 = 0 0 + 0= 0
0*1 = 0 0 + 1=1
1 * 0 = 0 1 + 0=1
1 * 1 = 1 1 + 1 = 0
tion, carries out the indicated operation and prints the sequence
of resulting bits.
(8) Write a program that prints out in how many ways, and how, one
Procedures
(1) To write a Boolean procedure that gets the value true if and
added and from which elements are deleted at one end of the list
accuracy
a given accuracy
-43-
The program could use the procedures for instance in the statement
y := ZERO(F,0,1,0.0005) or
y := ZERO(SIN,-.5,.5,0.00001)
by x.
-44-
(7) Write procedure POP which deletes an element from the stack
(see problem 2 ) .
a := i+k;
PRINT(a)
end;
procedure S; begin k := k+1 end;
i := 0; j := 0; k := 0;
P(S)
end
A H ; B-ADDV(INCV(A));
comment A IS NOW , B IS NOW
A H ; B«-ADDV(INCN(A));
comment A IS NOW , B IS NOW
AH.; B«-ADDN(INCV(A));
comment A IS NOW
A H ; B«-ADDN(INCN(A) ) ;
comment A IS NOW , B IS NOW
end;
Miscellaneous
• Scratch out 1.
Repeat:
Scratch out all multiples of the first number not yet
scratched out (for the first time this number is 2) until
to the right of this number all numbers are scratched out.
r
-46-
Write a program that lists the prime numbers less than 1000 in
let q x = 1/(1+1)
can capture another queen in one move. Write a program that pro-
of this problem.
t
-47- i
Given three pins and a pack of n disks on the first pin. The
so that (1) the disks end up in the same order on pin 2 as they
ler one and (3) only one disk at a time is moved. Write a pro-
est number z that can be decomposed into the sum of two n-th
= 9t3 + 10t3