Algol68

Appeared in:
December 12, 1968
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.a68 .8
Dialects:
Versions and implementations (Collapse all | Expand all):
Programming language

The main aims and principles of design of ALGOL 68:

  1. Completeness and clarity of design,

  2. Orthogonal design,

  3. Security,

  4. Efficiency:

    • Static mode checking
    • Mode-independent parsing
    • Independent compilation
    • Loop optimization
    • Representations — in minimal & larger character sets

Elements of syntax:

Nestable comments use alternate non-nestable comments
Non-nestable comments ¢ ~ ¢ OR # ~ # OR co ~ co OR comment ~ comment
Case-sensitivity yes - typefont sensitive also
Variable identifier regexp [a-z][a-z0-9 ]*
Function identifier regexp [a-z][a-z0-9 ]*
Variable assignment :=
Variable declaration type variablename
Variable declaration with assignment type variablename:=value;
Grouping expressions begin ~; ~ end OR (~; ~) OR for parallel/collateral clauses: begin ~, ~ end OR (~, ~)
Block begin ~ end OR ( ~ )
Physical (shallow) equality = OR eq
Physical (shallow) inequality /= OR ≠ OR ne
Deep equality :=: OR is
Deep inequality :/=: OR :≠: OR isnt
Comparison < <= >= > ≤ ≥
Function definition proc functionname = (mode a,b,c)mode: ~
Function call functionname(a,b,c)
Function call with no parameters functionname
If - then if ~ then ~ fi OR ( ~ | ~ )
If - then - else if ~ then ~ else ~ fi OR ( ~ | ~ | ~ )
Loop forever do ~ od
While condition do while ~ do ~ od
Do until condition while ~ ; ~ do ~ od
For each value in a numeric range, 1 increment for i from start by step to stop do ~ od
For each value in a numeric range, 1 decrement for i from start by -step to stop do ~ od

Examples:

Hello, World!:

Example for versions Algol68g-1.18.0

Note the use of printf with the formatting being described between dollars. eg $gl$ — meaning “g”eneral pattern, then new “l”ine.

( 
  printf(($gl$,"Hello, world!"))
)

Factorial:

Example for versions Algol68g-1.18.0, Algol68toc-1.8

This example uses recursive factorial definition with first 7 numbers precomputed.

PROC factorial = (INT n)LONG LONG INT:
  CASE n+1 IN
    1,1,2,6,24,120,720 # a brief lookup #
  OUT
    n*factorial(n-1)
  ESAC
;

Quadratic equation:

Example for versions Algol68g-1.18.0

A direct translation of the C++ example.

PROC print root = (INT ind, LONG COMPL x)VOID:
(   print(("x", ind, " := "));
    IF ABS(im OF x) < 1E-6 THEN
        print((re OF x, new line))
    ELSE print((x, new line))
    FI
);

main:
(   LONG COMPL a, b, c, d, x1, x2;
    print(("a := "));
    read((a));
    IF ABS a <1E-3 THEN
        print(( "Not a quadratic equation", new line));
        stop
    FI;
    print(("b := "));
    read((b));
    print(("c := "));
    read((c));

    a *:= 2;
    d := b*b-a*c*2.0;
    IF ABS d <1E-3 THEN
        print(("x := ", re OF (-b/a)))
    ELSE
        print root(1, (-b+long complex sqrt(d))/a);
        print root(2, (-b-long complex sqrt(d))/a)
    FI
)

Fibonacci numbers:

Example for versions Algol68g-1.18.0, Algol68toc-1.8

Generative method. This code specimen uses a callback to generate the sequence on demand.

MODE YIELDINT = PROC(INT)VOID;

PROC fibonacci = (INT n, YIELDINT yield)VOID: (
  INT even:=0, odd:=1;
  yield(even);
  yield(odd);
  FOR i FROM odd+1 TO n DO
    yield( (ODD i|odd|even) := odd + even )
  OD
);

main:(
  # FOR INT n IN # fibonacci(16, # ) DO ( #
  ##   (INT n)VOID:(
        print((" ",whole(n,0)))
    )); # OD # 
    print(new line)
)

Fibonacci numbers:

Example for versions Algol68g-1.18.0, Algol68toc-1.8

Analytic method

PROC analytic fibonacci = (INT n)INT:(
  REAL sqrt 5 = sqrt(5);
  REAL p = (1 + sqrt 5) / 2;
  REAL q = 1/p;
  ENTIER( (p**n + q**n) / sqrt 5 )
);

FOR i FROM 0 TO 16 WHILE
  print(whole(analytic fibonacci(i),0));
# WHILE # i /= 16 DO
  print(", ")
OD;
print(new line)