16 lines
432 B
Plaintext
16 lines
432 B
Plaintext
:- pred fib_acc(int::in, int::in, int::in, int::in, int::out) is det.
|
|
|
|
fib_acc(N, Limit, Prev2, Prev1, Res) :-
|
|
( N < Limit ->
|
|
% limit not reached, continue computation.
|
|
( N =< 2 ->
|
|
Res0 = 1
|
|
;
|
|
Res0 = Prev2 + Prev1
|
|
),
|
|
fib_acc(N+1, Limit, Prev1, Res0, Res)
|
|
;
|
|
% Limit reached, return the sum of the two previous results.
|
|
Res = Prev2 + Prev1
|
|
).
|