44 lines
843 B
Plaintext
44 lines
843 B
Plaintext
fib' := a -> n -> target ->
|
|
if true then
|
|
loop
|
|
if n = target then break end
|
|
[a1,a2] := a
|
|
a <- [a2, a1+a2]
|
|
n <- n+1
|
|
end
|
|
get(a,1)
|
|
end;
|
|
|
|
fib := fib'([0,1])(1);
|
|
|
|
fib_rec(n) := if n < 2 then n else fib_rec(n-1) + fib_rec(n-2); end;
|
|
|
|
concat'(acc,set) := if length(set) = 0 then acc else [h,t] := head(set); concat'(acc+h, t) end
|
|
concat := x -> concat'(0,x)
|
|
g := i -> set -> get(set,i)
|
|
|
|
test := f -> it -> v ->
|
|
concat(
|
|
map(
|
|
g(1),
|
|
map(
|
|
x ->
|
|
benchmark(
|
|
[] ->
|
|
f(x)
|
|
),
|
|
map(
|
|
_ -> v,
|
|
[0..it]
|
|
)
|
|
)
|
|
)
|
|
)/it
|
|
|
|
print("Benchmarking fibonacci...");
|
|
print("-------------------------");
|
|
print("Non-recursive implementation");
|
|
print(test(fib)(30)(30));
|
|
print("Recursive implementation");
|
|
print(test(fib_rec)(30)(30));
|