Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,29 @@
% The following code is derived from the Mercury Tutorial by Ralph Becket.
% http://www.mercury.csse.unimelb.edu.au/information/papers/book.pdf
:- module fib.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- pred fib(int::in, int::out) is det.
fib(N, X) :-
( if N =< 2
then X = 1
else fib(N - 1, A), fib(N - 2, B), X = A + B ).
:- func fib(int) = int is det.
fib(N) = X :- fib(N, X).
main(!IO) :-
fib(40, X),
write_string("fib(40, ", !IO),
write_int(X, !IO),
write_string(")\n", !IO),
write_string("fib(40) = ", !IO),
write_int(fib(40), !IO),
write_string("\n", !IO).

View file

@ -0,0 +1,15 @@
:- 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
).

View file

@ -0,0 +1 @@
fib_acc(1, 40, 1, 1, Result)

View file

@ -0,0 +1,6 @@
:- pragma memo(fib/2).
:- pred fib(int::in, int::out) is det.
fib(N, X) :-
( if N =< 2
then X = 1
else fib(N - 1, A), fib(N - 2, B), X = A + B ).