Skip to main content
Post Made Community Wiki by Dennis
added 880 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162

###Length 12

length(L,3).

length/2 is a built-in predicate of Prolog which is true if its first argument is a list of length its second argument. Here, we are setting the second argument as a ground integer, and the first as a variable. Thus, we get the following behavior:

?- length(L,3).
L = [_G1500, _G1503, _G1506].

That is, for that query to be true, L must be a list of 3 elements; those elements are _G1500, _G1503 and _G1506 which are variables (those names are internal variable names).

We can also run that predicate with both arguments as variables, and get the following very powerful behavior:

?- length(L,M).
L = [],
M = 0 ;
L = [_G1512],
M = 1 ;
L = [_G1512, _G1515],
M = 2 ;
L = [_G1512, _G1515, _G1518],
M = 3 ;
L = [_G1512, _G1515, _G1518, _G1521],
M = 4 ;
…

###Length 12

length(L,3).

length/2 is a built-in predicate of Prolog which is true if its first argument is a list of length its second argument. Here, we are setting the second argument as a ground integer, and the first as a variable. Thus, we get the following behavior:

?- length(L,3).
L = [_G1500, _G1503, _G1506].

That is, for that query to be true, L must be a list of 3 elements; those elements are _G1500, _G1503 and _G1506 which are variables (those names are internal variable names).

We can also run that predicate with both arguments as variables, and get the following very powerful behavior:

?- length(L,M).
L = [],
M = 0 ;
L = [_G1512],
M = 1 ;
L = [_G1512, _G1515],
M = 2 ;
L = [_G1512, _G1515, _G1518],
M = 3 ;
L = [_G1512, _G1515, _G1518, _G1521],
M = 4 ;
…
added 399 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162

###Length 9

a(ax+b).

Here is a fact, that constitutes your source code. We can make some interesting queries on that fact, to check out Prolog's pattern matching mechanism (remember that identifiers starting with an uppercase letter are free variables):

?- a(X).
X = ax+b.

Cooler:

?- a(X+b).
X = ax.

Even cooler:

?- a(X+Y).
X = ax,
Y = b.

###Length 9

a(ax+b).

Here is a fact, that constitutes your source code. We can make some interesting queries on that fact, to check out Prolog's pattern matching mechanism (remember that identifiers starting with an uppercase letter are free variables):

?- a(X).
X = ax+b.

Cooler:

?- a(X+b).
X = ax.

Even cooler:

?- a(X+Y).
X = ax,
Y = b.
added 804 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162

###Length 8

X=2;X=9.

Prolog is a logic programming language, and as such you expect to be able to use disjunctions. This is exactly what ; (Or) does: X unifies with 2 OR X unifies with 5. When running this in Prolog's REPL, we get the following:

?- X=3;X=9.
X = 3

The REPL halts after printing you this answer, instead of finishing and waiting for a new query. Indeed, Prolog thinks there are other possible answers (and there is one in this case), and so it is waiting to see if you are interested in those. By pressing ;, it will give you another answer:

?- X=3;X=9.
X = 3 ;
X = 9.

which is what we expected to get. Here, Prolog knows there are no other possible answers for what we queried, so it will finish and wait for another query.

The mechanism behind this is called backtracking: when Prolog choses to unify X with 2, it remembers that it made that choice when in fact there are other possibilities (here, unifying X with 9). When we ask for another answer (or if the query had failed after that choice), it will backtrack to the last choice it made and try another one, until no other choice is available.

###Length 8

X=2;X=9.

Prolog is a logic programming language, and as such you expect to be able to use disjunctions. This is exactly what ; (Or) does: X unifies with 2 OR X unifies with 5. When running this in Prolog's REPL, we get the following:

?- X=3;X=9.
X = 3

The REPL halts after printing you this answer, instead of finishing and waiting for a new query. Indeed, Prolog thinks there are other possible answers (and there is one in this case), and so it is waiting to see if you are interested in those. By pressing ;, it will give you another answer:

?- X=3;X=9.
X = 3 ;
X = 9.

which is what we expected to get. Here, Prolog knows there are no other possible answers for what we queried, so it will finish and wait for another query.

The mechanism behind this is called backtracking: when Prolog choses to unify X with 2, it remembers that it made that choice when in fact there are other possibilities (here, unifying X with 9). When we ask for another answer (or if the query had failed after that choice), it will backtrack to the last choice it made and try another one, until no other choice is available.

added 924 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 426 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
edited body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 291 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 167 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 513 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 732 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 41 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
added 482 characters in body
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading
Source Link
Fatalize
  • 39.3k
  • 5
  • 70
  • 162
Loading