Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Algol 68

Name: Anonymous 2013-03-12 23:02

Algol 68 looks like a modern language (except for the uppercase keywords) even though it's older than C. It had real garbage collection and user-defined operators.
This is a program that defines a tree structure and some operations on it:
MODE LEAF = INT;
MODE TREE = STRUCT(REF TREE l, LEAF c, REF TREE r);
MODE ACTION = STRUCT(PROC VOID in, PROC(REF LEAF)VOID leaf, PROC VOID out);
BOOL left = TRUE, right = FALSE;
OP & = (TREE t)REF TREE: HEAP TREE := t;
OP & = (LEAF l)REF TREE: HEAP TREE := (NIL, l, NIL);
OP ^ = (PROC(REF LEAF)VOID p)ACTION: (VOID: EMPTY, p, VOID: EMPTY);
PRIO -> = 1, OP -> = (REF TREE src, REF REF TREE dest)REF REF TREE: dest := src;
OP LEFT = (REF TREE t)REF REF TREE: l OF t;
OP RIGHT = (REF TREE t)REF REF TREE: r OF t;
PROC walk = (REF TREE t, ACTION action)VOID: (t :/=: NIL |
    in OF action;
    walk(l OF t, action);
    (leaf OF action)(c OF t);
    walk(r OF t, action);
    out OF action);
PROC follow = (REF TREE t, []BOOL path)REF REF TREE: (
    PROC loop = (REF REF TREE n, []BOOL path)REF REF TREE:
        IF UPB path < 1 THEN
            n
        ELSE
            loop(IF path[1] THEN l OF n ELSE r OF n FI, path[2:@1])
        FI;
    loop(LOC REF TREE := t, path[@1]));
PROC(REF TREE)VOID display = walk(, (VOID: print("("), (REF LEAF l)VOID: printf(($g(0)$, l)), VOID: print(")")));
PRIO JOIN = 5, OP JOIN = (LEAF init, REF TREE t)LEAF: (
    LEAF coll := init;
    walk(t, ^((REF LEAF l)VOID: coll +:= l));
    coll);

TREE tree := (&TREE(&7, 1, &TREE(&-3, -5, &-4)), 2, &TREE(&8, 3, NIL));
display(tree); print(new line);
print(0 JOIN tree);
&TREE(&5, 6, &7) -> LEFT LEFT tree;
display(RIGHT tree); print(new line);
display(tree); print(new line);
display(follow(tree, (left, right, right)))

This is a similar program in ML for comparison:
datatype 'a tree = NIL | TREE of 'a tree * 'a * 'a tree
type 'a action = (unit -> unit) * ('a -> unit) * (unit -> unit)
fun LEFT (TREE(l, _, _)) = l
  | LEFT NIL = NIL
fun RIGHT (TREE(_, _, r)) = r
  | RIGHT NIL = NIL
fun & l = TREE(NIL, l, NIL)
fun A l = ((fn() => ()), l, (fn() => ()))
fun walk ((TREE(l, c, r)), act as (in', leaf, out)) = (
         in'();
         walk(l, act);
         leaf(c);
         walk(r, act);
         out()
    )
  | walk (NIL, _) = ()
fun follow ((TREE(l, c, r)), (dir :: xs)) = follow(if dir then l else r, xs)
  | follow (t, nil) = t
  | follow (NIL, _) = NIL
fun display conv t = walk(t, (fn() => print("("), fn(l) => print(conv l), fn() => print(")")))
fun JOIN (init, t) = let val coll = ref init in
         walk(t, A(fn(l) => coll := !coll + l));
         !coll
    end
infix JOIN
val tree = TREE(TREE(&7, 1, TREE(& ~3, ~5, & ~4)), 2, TREE(&8, 3, NIL))
val _ = (print o Int.toString)(0 JOIN tree)
val _ = display Int.toString (tree)

Name: Anonymous 2013-03-13 7:20

>>1
Algol also had call-by-name, which only Haskell reintroduced.

Modern languages have not evolved beyond Algol-68.
But they got classes and design patterns!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List