Executable library: Difference between revisions
Add Limbo version
(→{{header|C}}: Add Déjà Vu example) |
(Add Limbo version) |
||
Line 541:
Notes: <code>9!:29]1</code> tells the interpeter to run a phrase. <code>9!:27'phrase'</code> tells the interpeter the phrase to execute. (<code>9!:</code> means, in essence: standard library number 9, and <code>9!:29</code> identifies a specific entry point in that library.) In 66.ijs we can not use the presence of <code>9!:29]1</code> from <code>hailseq.ijs</code> because hailseq.ijs was loaded with require which means that if it had already been loaded it will not be loaded again. (And, <code>66</code> here is just an arbitrary temporary file name.)
=={{header|Limbo}}==
There's no real difference in compilation or output for libraries versus commands in Inferno; commands (by convention) are expected to define an init() function that accepts a reference to a graphical context and a list of strings (i.e., the argument list) in order to satisy the type-checker. So this task is fairly simple. First, execlib.b looks like this:
<lang Limbo>implement Execlib;
include "sys.m"; sys: Sys;
include "draw.m";
Execlib: module {
init: fn(ctxt: ref Draw->Context, args: list of string);
hailstone: fn(i: big): list of big;
};
init(nil: ref Draw->Context, nil: list of string)
{
sys = load Sys Sys->PATH;
seq := hailstone(big 27);
l := len seq;
sys->print("hailstone(27): ");
for(i := 0; i < 4; i++) {
sys->print("%bd, ", hd seq);
seq = tl seq;
}
sys->print("⋯");
while(len seq > 4)
seq = tl seq;
while(seq != nil) {
sys->print(", %bd", hd seq);
seq = tl seq;
}
sys->print(" (length %d)\n", l);
max := 1;
maxn := big 1;
for(n := big 2; n < big 100000; n++) {
cur := len hailstone(n);
if(cur > max) {
max = cur;
maxn = n;
}
}
sys->print("hailstone(%bd) has length %d\n", maxn, max);
}
hailstone(i: big): list of big
{
if(i == big 1)
return big 1 :: nil;
if(i % big 2 == big 0)
return i :: hailstone(i / big 2);
return i :: hailstone(big 3 * i + big 1);
}
</lang>
And execsexeclib.b (which executes execlib) looks like this:
<lang Limbo>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 < 10000; 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";
}
</lang>
{{out}}
<pre>
% apply {limbo $1} *execlib.b
% apply {echo Running $1; $1} *execlib.dis
Running execlib.dis
hailstone(27): 27, 82, 41, 124, ⋯, 8, 4, 2, 1 (length 112)
hailstone(77031) has length 351
Running execsexeclib.dis
The most common sequence length is 53 (encountered 190 times)
</pre>
=={{header|NetRexx}}==
|