I’m having trouble coming up with a happy Prolog Fibonacci function.
The function I’d like to have would work with the full unification:
fib(3,5,X) --> X = 8 ? fib(3,X,8) --> X = 5 ? fib(X,5,8) --> X = 3 ?
It’d be cool if it could also handle:
fib(X,Y,89) --> X = 34, Y = 55 ?
But the best I have been able to do so far is only the very first style:
fib(3,5,X) --> X = 8 ?
Is my goal realistic? If so, how would it best be solved?
When searching for other examples of Fibonacci series in Prolog, I’ve only found other programs with similar limitations.
Here’s what I’ve got so far:
fib(0, 1, 1). fib(1, 1, 2). fib(N, M, F) :- F is N+M, M1 is N, N1 is M-N, fib(N1, M1, F1), F1 is N1+M1.