1.2 Programming Basics
1. Hello World
To write and compile a simple program in Algol 68 using the a68g
compiler on Ubuntu, follow these steps:
Step 1: Install the Algol 68 Compiler (a68g
)
First, you need to install the a68g
compiler. Open your terminal and run the following command:
sudo apt-get updatesudo apt-get install algol68g
This will install the a68g
compiler on your system.
Step 2: Write a Simple Algol 68 Program
Let’s create a simple “Hello, World!” program in Algol 68.
- Open a text editor (like
nano
orgedit
) and create a new file namedhello.alg
.
nano hello.alg
- Write the following code into the file:
BEGIN print(("Hello, World!", newline))END
- Save the file and exit the editor (
Ctrl + X
, thenY
to confirm innano
).
Step 3: Compile the Algol 68 Program
To compile the program using a68g
, run the following command in your terminal:
a68g hello.alg
This command interprets and executes the program directly. Since a68g
is an interpreter rather than a traditional compiler, it will run the program and output “Hello, World!” to the terminal immediately.
Step 4: Run the Program
As a68g
is an interpreter, the previous step already executed the program. However, if you want to compile and run it in one step, you can use:
a68g -run hello.alg
This will output:
Hello, World!
Additional Notes
- a68g is an interpreter for Algol 68, so it does not produce a separate compiled binary file like C compilers do. Instead, it reads and executes the source code directly.
- If you’re writing more complex programs, you can use the same approach, but make sure to structure your code and files accordingly.
Algol 68, implemented by the a68g
interpreter, provides a rich set of control structures and data structures. Here’s an overview of the control structures and data structures available in Algol 68:
2. Control Structures in Algol 68
-
Conditional Statements
IF
Statement: Used to make decisions.IF condition THENstatementsELSEstatementsFICASE
Statement: Multi-way branching.CASE expression INvalue1: statements;value2: statements;...valueN: statements;ESAC
-
Looping Constructs
FOR
Loop: Iterates over a range of values.FOR i FROM start TO end DOstatementsODWHILE
Loop: Repeats as long as a condition is true.WHILE condition DOstatementsODUNTIL
Loop: Repeats until a condition becomes true.UNTIL condition DOstatementsODDO
Loop: Similar to awhile
loop, but always executes the loop body at least once.DOstatementsWHILE conditionOD
-
Switch Statements
GOTO
Statement: Jumps to a labeled statement.GOTO label;EXIT
Statement: Exits from a loop.EXIT;
-
Block Structures
BEGIN ... END
Blocks: Encapsulates a sequence of statements into a block.BEGINstatementsEND
3. Data Structures in Algol 68
-
Basic Data Types
INT
: Integer numbers.REAL
: Floating-point numbers.BOOL
: Boolean values (TRUE
orFALSE
).CHAR
: Single characters.STRING
: Sequences of characters.
-
Compound Data Types
- Arrays: Fixed-size sequences of elements of the same type.
[3]INT array := (1, 2, 3); // Array of 3 integers
- Structures: User-defined types (similar to
struct
in C).STRUCT (INT id,STRING name) person; - References: Pointers to data, similar to pointers in C.
REF INT refVar := LOC INT;
- Arrays: Fixed-size sequences of elements of the same type.
-
Higher-order Data Types
- Sets: Collections of elements without duplicates.
SET [10]INT set := (1, 2, 3);
- Unions: A data type that can store different types of data in the same memory location.
UNION(INT, REAL) num;
- Procedures: Functions and subroutines.
PROC(INT)INT square = (INT x)INT: x * x;
- Sets: Collections of elements without duplicates.
-
Other Constructs
- Slices: Subarrays or subranges of arrays.
slice := array[2:4];
- Rows, Cols, and Strides: Used for multi-dimensional array access.
[3,3]INT matrix;matrix[1,2] := 5;
- Slices: Subarrays or subranges of arrays.
4. Example Usage
Here is a small example to illustrate some of these control structures and data structures:
BEGIN INT x := 5; REAL y := 10.5; [3]INT array := (1, 2, 3);
FOR i FROM 1 TO 3 DO print((array[i], " ")); OD;
CASE x IN 1: print(("One")); 5: print(("Five")); ESAC;
PROC(REAL)REAL square = (REAL z) REAL: z * z; print(("Square of y: ", square(y), newline));
EXIT;END
5. Procedures and Functions
In Algol 68, procedures and functions are fundamental constructs used to encapsulate code that can be reused and executed with different arguments. They allow for modular programming by defining blocks of code that can be invoked with parameters.
5.1 Procedures in Algol 68
A procedure in Algol 68 is a block of code that performs an operation but does not necessarily return a value. However, in Algol 68, the distinction between procedures and functions is less rigid than in some other languages because both are defined using the PROC
keyword, and whether or not they return a value depends on the return type specified.
Defining a Procedure
To define a procedure, you use the PROC
keyword followed by the parameter list and the code block.
PROC procedure_name = (parameter_list) VOID:BEGIN -- statementsEND
Example: Simple Procedure
PROC print_message = () VOID:BEGIN print(("Hello, Algol 68!", newline))END;
BEGIN print_messageEND
PROC print_message = () VOID:
defines a procedure namedprint_message
that takes no parameters and returns nothing (VOID
).- The procedure prints “Hello, Algol 68!” when called.
5.2 Functions in Algol 68
A function is a special kind of procedure that returns a value. In Algol 68, functions are also defined using the PROC
keyword, but with a specified return type.
Defining a Function
To define a function, you specify the return type after the PROC
keyword.
PROC function_name = (parameter_list) return_type:BEGIN -- statements return_valueEND
Example: Simple Function
PROC sum = (INT a, INT b) INT: a + b;
BEGIN INT result := sum(3, 4); print(("Sum of 3 and 4 is ", result, newline))END
PROC sum = (INT a, INT b) INT:
defines a function namedsum
that takes two integer parameters (a
andb
) and returns an integer.- The function returns the sum of
a
andb
. - The
result
variable stores the returned value from thesum
function, which is then printed.
5.3 Procedures with Return Values (Functions)
Since Algol 68 treats procedures and functions similarly, the main difference lies in whether or not a value is returned. Even procedures that return values are defined using the PROC
keyword, but they specify a return type rather than VOID
.
Example: Procedure vs. Function
-- Function that returns an integerPROC square = (INT x) INT: x * x;
-- Procedure that prints the squarePROC print_square = (INT x) VOID:BEGIN print(("Square of ", x, " is ", square(x), newline))END;
BEGIN INT y := 5; print_square(y)END
square
is a function that returns the square of an integer.print_square
is a procedure that callssquare
and prints the result.- The
print_square
procedure does not return a value; it just prints the result.
5.4 Recursive Functions
Algol 68 supports recursive procedures and functions, which means a procedure or function can call itself.
Example: Recursive Function
PROC factorial = (INT n) INT:BEGIN IF n = 0 THEN 1 ELSE n * factorial(n - 1) FIEND;
BEGIN INT result := factorial(5); print(("Factorial of 5 is ", result, newline))END
factorial
is a recursive function that calculates the factorial ofn
.- It calls itself with
n - 1
untiln
is 0, at which point it returns 1.
5.5 Keypoints
- Procedures in Algol 68 are defined using
PROC
and typically do not return a value (ifVOID
is used as the return type). - Functions are procedures that return a value and are defined similarly to procedures, but with a specified return type.
- Both procedures and functions can take parameters and be used for modular and reusable code.
- Recursive functions are supported, allowing for the implementation of algorithms that involve self-referential logic.