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-17 5:28

I translated the Cont monad from Haskell into Algol 68.
http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style
PROGRAM monad CONTEXT VOID USE standard
BEGIN
PRIO >> = 1, >>= = 1;
MODE A = INT, R = INT;
MODE CONT = STRUCT(PROC(PROC(A)R)R cont);
MODE KCONT = PROC(PROC(A)CONT)CONT, KACT = PROC(A)CONT, KFUN = PROC(A)R, KTYPE = PROC(PROC(A)R)R;
PROC cont = (KTYPE k)CONT: (CONT m; cont OF m := k; m);
PROC run cont = (CONT c)KTYPE: cont OF c;

OP RETURN = (A n)CONT: cont((KFUN k)R: k(n));
OP >>= = (CONT m, KACT f)CONT: cont((KFUN k)R: run cont(m)((A a)R: run cont(f(a))(k)));
OP >> = (CONT m, n)CONT: m >>= ((A a)CONT: n);
PROC call cc = (KCONT f)CONT: cont((KFUN k)R: run cont(f((A a)CONT: cont((KFUN _)R: k(a))))(k));
#
return n = Cont (\k -> k n)
m >>= f  = Cont (\k -> runCont m (\a -> runCont (f a) k))
callCC f = Cont $ \k -> runCont (f (\a -> Cont $ \_ -> k a)) k
#
PROC print a = (A a)R: (print(a); a);
PROC square c = (A x)CONT: RETURN (x ^ 2);
PROC add three c = (A x)CONT: RETURN (x + 3);
CONT bar = call cc((KACT k)CONT: (A n = 5; k(n) >> RETURN 25));

run cont(bar)(print a);
print(new line);
run cont(square c(4) >>= add three c)(print a)
END
FINISH

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