Algol 68 Introduction

by "Blag" - Senior Developer Evangelist

Return to Geeky Thursday

What is Algol?


Algol (Algorithmic Language) is a family of imperative programming languages.


Algol 58, Algol 60 and Algol 68.


Used by the Association for Computer Machinery (ACM) for algorithm description for more than 50 years.


One of the most influential programming languages, giving rise to, among others, to Simula, B, Pascal and C.

How to install Algol 68?


We're going to learn Algol 68 using Algol 68 Genie


sudo apt-get install -y algol68g


On Mac


ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null


brew install algol68g


Who uses Algol 68?


  • While it's not used anymore...
  • Algol (60) is one of the most influential languages of all time...
  • Algol 68 was not as popular as Algol 60...but still widely used on it's own time...

Starting out...


Because Algol 68 uses compilation checks it doesn't have a REPL so we will need to use an editor to code...


If your're not using it already I will recommend you to get Geany, an awesome IDE that comes pre-configured for a lot of languages.

Basic Concepts


Let's see the most basic example...a Hello World...


Create a new file and call it "hello.a68g" (in Lowercase).


				
BEGIN

#This is a comment#

    print("Hello, World!")
	
END					
				

Compile time...


To compile and run our code, you can configure Geany


  • a68g %e.a68g


Or call it from the terminal


  • a68g hello.a68g

If everything worked out fine...you should see this...


Variables


Variables need to have a type.


Coersion only works on some types, so no INT to CHAR or STRING.


INT is printed with a sign, so we need a formatted print if we want just the number...


Create a new file and call it "variables.a68g" (All in lowercase).


				
BEGIN

    INT age := 42;
	
    STRING name := "Blag";
	

    print(age);
	
    print(newline);
	
    print(("My name is ", name, " and I'm "));
	
    printf(($g(0)x$, age))
	
END
				

When a command is finished, we need a ";".

Arrays


Arrays are static and need bounds when declared.


They also must be initialized before being used...


Arrays can't shrink or grow...unless when creeate them as flexible...


Create a new file and call it "arrays.a68g" (All in lowercase).

				
BEGIN

    [5]INT numbers := (1,2,3,4,5);

    printf(($g(0)x$, numbers));

	

    print(newline);


	
    FLEX [20]CHAR string := "Hello, World!";

    print(string);

    print(newline);

    string := "Algol 68 is fun! And you know it!";

    print(string)

END
				

Multidimension Arrays


We can also create multidimensional arrays


Create a new file and call it "multiple_arrays.a68g" (All in lowercase).

				
BEGIN

    [,] INT e = ((1,2,3),
    
                 (4,5,6));
                 
    printf(($g(0)x$, e(2,2)));
    
	
    [][] CHAR days = ("Monday", "Tuesday", "Wednesday",
    
                      "Thursday", "Friday", "Saturday",
                      
                      "Sunday");
                      
                     
    print(newline);
    
   
    print(days[2][1:4])
    
END
				

Loops

We have some options to loop our data...


				
BEGIN

INT x := 5;

FOR i TO 5 DO

    print(i)

OD;



print(newline);



WHILE x > 0

DO

    print(x);

    x := x -1

OD;
				

				
print(newline);



DO print(x);

    x := x + 1

UNTIL x > 4

OD

END
				

Procedures

Procedures can have zero or more parameters...

They return the last value and must always return or print a value...

				
BEGIN

    INT i, j := 3;

    PROC INT p1 = INT: i:=3+j;

    print(p1);

	
    print(newline);

	
    PROC p2 = (INT i)VOID:

        print(i);
		
    p2(5)

END
				

Fibonacci List


Finally...we're going to make our first application...


Ok...we have made a lot of applications...but now this one makes sense -;)


Name your file "fibonacci.a68g (all in lowercase)"


				
BEGIN

    print("Enter a number: ");
    
    INT num = read int;
    
	
    PROC fib = (INT num, a, b)VOID:
    
        IF a > 0 THEN
        
            IF num > 1 THEN
            
                printf(($g(0)x$, a + b));
                
                fib(num-1, a+b, a)
                
            FI
            
        ELIF a = 0 THEN
        
            printf(($g(0)x$, a + b));
            
            fib(num-1, a+b, b)
            
        FI;
				
				
    printf(($g(0)x$, 0, 1));	
    
    fib(num,0,1);
    
    print(newline)
    
END
				

When we run it we're going to see...

Making an LED Number App


This is one of my favorite codes of all time...


Name your file "LED_Numbers.a68g"


				
BEGIN

    [] STRING led = (" _  | | |_| ","     |   |  ",
    
                     " _   _| |_  ","_   _|  _|  ",
                     
                     "    |_|   | "," _  |_   _| ",

                     " _  |_  |_| ","_    |   |  ",
                     
                     " _  |_| |_| "," _  |_|  _| ");
                     

    STRING line_1, line_2, line_3, val;

    print("Enter a number: ");

    INT num = read int;
				
				
    PROC digits = (INT number)VOID:

        IF number > 0 THEN

            digits(number % 10);

            val := led[(number MOD 10) + 1];

            line_1 := line_1 + val[1:4];

            line_2 := line_2 + val[5:8];

            line_3 := line_3 + val[9:12]

        FI;
				
				
    digits(num);
    

	
    print(line_1);

    print(newline);

    print(line_2);

    print(newline);

    print(line_3);

    print(newline);

    print(newline)	

END
				

When we run it we're going to see...


Random Names


This App will generate 100,000 (10,000) random semi-random names using two 16 elements arrays


We will measure the runtime


Name your file "random_names.a68g (all in lowercase)"


				
BEGIN

INT seed := 0, ans := 0, counter := 0;

INT aux_seed := 0, aux_ans := 0;


[] STRING names = ("Anne","Gigi","Blag","Juergen",

                   "Marek","Ingo","Lars","Julia",
                   
                   "Danielle","Rocky","Julien",

                   "Uwe","Myles","Mike", "Steven", 
                   
                   "Fanny");
				
				
[] STRING last_names = ("Hardy","Read","Tejada",

                        "Schmerder","Kowalkiewicz",
                        
                        "Sauerzapf","Karg","Satsuta",

                        "Keene","Ongkowidjojo",
                        
                        "Vayssiere","Kylau","Fenlon",
                        
                        "Flynn","Taylor", "Tan");


						
[10000] STRING full_names;



FOR i TO 10000 DO

    counter := i;

    seed := 7 * counter MOD 101;

    ans := (seed - 1) MOD 16 + 1;
				
				
    aux_seed := 7 * counter MOD 4;

    aux_ans := (aux_seed - 1) MOD 16 + 1;

    full_names[i] := names[ans] + " " + 
    
                     last_names[aux_ans]

OD    


END
				

When we run it we're going to see...

Pretty fast even though were not using real random numbers...

And 10,000 instead of 100,000 records -:)

Why? well...

Real Random Numbers in Algol 68 are Real and there's no Int coersion...

Algol 68 crashed with 100,000 records -:'(

Decimal to Romans


This App will create a Roman Numeral based on a decimal number


This will include some nice commands...


Name your file "decimal_to_romans.a68g" (all in lowercase)


				
BEGIN

    [] INT roman_keys = (1000, 900, 500, 400, 100, 90, 
    
                         50, 40, 10, 9, 5, 4, 1);
                         
                         
    [] STRING roman_values = ("M", "CM", "D", "CD", 
	
                              "C", "XC","L", "XL", 
	                          
                              "X", "IX", "V", "IV", 
	                          
                              "I");
	                                
    INT counter := 1;
    
    INT key_num;
    
    INT aux_num;
    
    
 	
    STRING roman_num;
				
				
    print("Enter a number: ");
    
    INT num = read int;
    
    aux_num := num;
	
	
    WHILE aux_num > 0
	
    DO
    
        key_num := roman_keys[counter]; 
        
        IF aux_num >= key_num THEN
        
            roman_num := roman_num + 
            
                         roman_values[counter];
            
            aux_num := aux_num - key_num
            
        ELSE
        	
            counter := counter + 1
            
        FI
        
    OD;
				
					
	print(roman_num);
	
	print(newline)
	
END
				

When we run it we're going to see...


Count Letters


In this example we're going to read a file and count how many time a letter appears...


Call your file "count_letters.a68g" (all in lowercase).


Create a file called "readme.txt" with the following text...


"This is a text file that we're going to read using Algol 68"


				
BEGIN

FLEX [100]STRING letters;

FLEX [100]INT count; 

INT key := 0;



FILE inf;

open(inf,"readme.txt",stand in channel);

STRING line;



get(inf, line);
				
				
PROC find_key = (STRING value)INT:

(    FOR i TO UPB line DO

       IF key = 0 THEN

           IF value = letters[i] THEN

               key := i

           FI

       FI

    OD;

    key

);
				
				
FOR i TO UPB line DO

    key := 0;

    count[i] := 0;

    key := find_key(line[i]);

    IF key = 0 THEN

        letters[i] := line[i];

        count[i] := 1

    ELSE

        count[key] := count[key] + 1

    FI

OD;
				
				
FOR i TO UPB line DO

    IF count[i] /= 0 THEN

        print(letters[i]);

        print(count[i]);

        print(newline)

    FI

OD


END
				

When we run it we're going to see...


That's it for now


Algol 68 is a nice and interesting language


Not mind bended like I love, but still worth learning


Specially because it has been very influential...



Contact Information


Blag --> blag@blagarts.com

@Blag on Twitter

Go back home...