Prolog Cookbook
Classic factorial function,
Fibonacci functions
Aggregate counting in SWI-Prolog,
Here,
popular
function checks whether given person is liked by more than one person. In the definitionY
is existentionally qualified.References - https://stackoverflow.com/questions/5930340/aggregate-3-in-swi-prolog/5930420
Higher order functions,
Checking whether a predicate exists - https://stackoverflow.com/questions/12886179/prolog-how-to-check-if-a-predicate-exists.
Define contains based on append function,
Palindrome check in Prolog is quite nice,
In a restricted set, we can even use this declarative definition to generate palindromes, as in the following example!
Fizz buzz in Prolog,
```prolog print_fizz_buzz(N) :- ( 0 is mod(N, 15) -> write("fizzbuzz"),nl ; 0 is mod(N, 3) -> write("fizz"), nl ; 0 is mod(N, 5) -> write("buzz"), nl ; write(N), nl ).
fizz_buzz(N) :- aux_fizz_buzz(0, N). aux_fizz_buzz(M, N) :- M < N, print_fizz_buzz(M), M1 is (M + 1), aux_fizz_buzz(M1, N). aux_fizz_buzz(M, N) :- M >= N, !, nl.
In true Prolog fashion, one can find out all the operators which are curently definied using something like follows,
which will list all the opeartors with their respective precedence and type.
Permutations,
Last updated