; Define the base cases. (= (fib 0 0) ) (= (fib 1 1) ) ; Define the recursive case using CLP(FD) constraints. (= (fib $N $F) (#>-p $N 1) ;;Ensure that N is greater than 1. (#=-p $N1 (- $N 1)) ;;Define N1 as N - 1. (#=-p $N2 (- $N 2)) ;;Define N2 as N - 2. (fib $N1 $F1) ;;Recursive call to find the (N-1)th Fibonacci number. (fib $N2 $F2) ;;Recursive call to find the (N-2)th Fibonacci number. (#=-p $F (+ $F1 $F2))) ; The Nth Fibonacci number is the sum of the (N-1)th and (N-2)th Fibonacci numbers. ; Example query for finding the Nth Fibonacci number: ; ; ?- fib(10, F). ; ; F = 55. !(let $_ (fib 10 $Y) $Y) ; Example query for finding the position N of a given Fibonacci number: ; ; ?- fib(N, 55). ; ; N = 10. !(let $_ (fib $X 55) $X)