Prolog “or” operator, query

you can ‘invoke’ alternative bindings on Y this way: Note the parenthesis are required to keep the correct execution control flow. The ;/2 it’s the general or operator. For your restricted use you could as well choice the more idiomatic that on backtracking binds Y to each member of the list. edit I understood with … Read more

List Length in Prolog

You need to use an accumulator. While you could do something like this: which will recurse all the way down to the end of the list and then, as each invocation returns, add one to the length, until it gets back to the top level with the correct result. The problem with this approach is that … Read more

‘if’ in prolog?

A standard prolog predicate will do this. will evaluate to true if you call it with 5 and fail(return false) if you run it with anything else. For not equal you use \= Technically it is does not unify, but it is similar to not equal. Learn Prolog Now is a good website for learning … Read more

Reversing a List in Prolog

Your solution explained: If we reverse the empty list, we obtain the empty list. If we reverse the list [H|T] , we end up with the list obtained by reversing T and concatenating with [H] . To see that the recursive clause is correct, consider the list [a,b,c,d] . If we reverse the tail of … Read more

singleton variables in prolog

Singleton variables are useless in Prolog, and are easily introduced by editing typos. The warning is welcome to me, as it allows to easily spot such frequent cause of error. Being a warning, you can run code containing singletons, but any value these eventually will assume will be lost. I don’t think that ISO standard (never heard about ANSI) forbids such variables. You could … Read more

How do I append lists in Prolog?

The code as you’ve posted it is (almost) OK. The order of clauses just needs to be swapped (in order to make this predicate definition productive, when used in a generative fashion): This defines a relationship between the three arguments, let’s say A, B and C. Your first line says, ” C is the result of appending A and B if A and C are non-empty lists, they both have … Read more