RosettaCodeData/Task/Executable-library/Limbo/executable-library-2.limbo

46 lines
1.0 KiB
Plaintext

implement ExecsExeclib;
include "sys.m"; sys: Sys;
include "draw.m";
ExecsExeclib: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
};
# Usually, this would be placed into something like "execlib.m",
# but it's fine here.
Execlib: module {
hailstone: fn(i: big): list of big;
};
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
# This program expects that the result of compiling Execlib is execlib.dis,
# so you'll need to adjust this line if you used a different filename.
lib := load Execlib "execlib.dis";
if(lib == nil)
die("Couldn't load execlib.dis");
counts := array[352] of { * => 0 };
for(i := 1; i < 100000; i++) {
counts[len lib->hailstone(big i)]++;
}
max := 0;
maxi := 0;
for(i = 1; i < len counts; i++) {
if(counts[i] > max) {
max = counts[i];
maxi = i;
}
}
sys->print("The most common sequence length is %d (encountered %d times)\n", maxi, max);
}
die(s: string)
{
sys->fprint(sys->fildes(2), "runls: %s: %r", s);
raise "fail:errors";
}