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-15 0:58

This is an alternate representation of the program in >>1.
mode Leaf = int;
mode Tree = struct(ref Tree l, Leaf c, ref Tree r);
mode Action = struct(proc void inf, proc(ref Leaf)void leaf, proc void outf);
bool left = true, right = false;
op & = (Tree t)ref Tree: heap Tree := t;
op & = (Leaf l)ref Tree: heap Tree := (○, l, ○);
op ^ = (proc(ref Leaf)void p)Action: (void: (), p, void: ());
prio -> = 1, op -> = (ref Tree src, ref ref Tree dest)ref ref Tree: dest := src;
proc walk = (ref Tree t, Action action)void: (t :≠: ○ |
    inf→action;
    walk(l→t, action);
    (leaf→action)(c→t);
    walk(r→t, action);
    outf→action);
proc follow = (ref Tree t, []bool path)ref ref Tree: (
    proc loop = (ref ref Tree n, []bool path)ref ref Tree:
        (⌈path < 1 | n | loop((path[1] | l→n | r→n), path[2:@1]));
    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, ○));
display(tree); print(new line);
print(0 Join tree);
&Tree(&5, 6, &7) -> l→l→tree;
display(r→tree); print(new line);
display(tree); print(new line);
display(follow(tree, (left, right, right)))

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