There is more to say about Prolog than can ever be said. At the same time, you also need to collect your own experiences with the language, and ponder certain aspects for yourself. Here are a few

Video: |

For example, consider again

list_list_together([], Bs, Bs). list_list_together([A|As], Bs, [A|Cs]) :- list_list_together(As, Bs, Cs).It is easy to see that

(defun append (x y) (if x (This version ofcons(car x) (append (cdr x) y)) y))

It is somewhat remarkable that such a basic function is

In Prolog, many more predicates are

However, take into account that many Prolog programs are

For example, let us define the relation

list_element_rest([L|Ls], L, Ls). list_element_rest([L|Ls0], E, [L|Ls]) :- list_element_rest(Ls0, E, Ls).

- The relation quite obviously holds for the
list
`[L|Ls]`, its*first*element`L`, and the remainder`Ls`. *If*the relation holds for the list`Ls0`, one of its elements*E*, and the remainder`Ls`,*then*the relation also holds for`[L|Ls0]`and`E`, and the remainder`[L|Ls]`. This rule is clearly*tail recursive*, because the recursive call is its only goal.

This predicate is quite versatile. Operationally, we can use it to

?- list_element_rest("ab", E, Rest). E = a, Rest = "b" ; E = b, Rest = "a" ; false.And also:

?- list_element_rest(Ls, c, "ab"). Ls = "cab" ; Ls = "acb" ; Ls = "abc" ; false.In the Prologue for Prolog draft and several Prolog systems, an almost identical predicate is available under the name

Using

list_permutation1([], []). list_permutation1(Ls, [E|Ps]) :- list_element_rest(Ls, E, Rs),Note that that this predicate islist_permutation1(Rs, Ps).

Let us now run a few benchmarks. We generate?- list_permutation1("abc", Ps).Ps = "abc" ; Ps = "acb" ; Ps = "bac" ; Ps = "bca" ; Ps = "cab" ; Ps = "cba" ; false.

?- L in 9..11, indomain(L), portray_clause(L), length(Ls, L),Now consider an alternative definition of this relation, which we calltime((list_permutation1(Ls,_),false)).9.% CPU time: 1.662s10.% CPU time: 16.620s11.% CPU time: 187.939s

list_permutation2([], []). list_permutation2([L|Ls0], Ps) :-This version islist_permutation2(Ls0, Ls), list_element_rest(Ps, L, Ls).

?- L in 9..11, indomain(L), portray_clause(L), length(Ls, L),Note that this version istime((list_permutation2(Ls,_),false)).9.% CPU time: 0.344s10.% CPU time: 3.342s11.% CPU time: 36.651s

Together with the previous section, this example illustrates that tail recursion

This is a tough truth to accept for most Prolog programmers. There always seems hope that we can somehow outsmart the system and get away with

For example, many beginners can correctly write a Prolog predicate that describes a

list([]). list([_|Ls]) :- list(Ls).This is a very general relation that works in all directions. For example:

?- list("abc"). true. ?- list(Ls). Ls = [] ; Ls = [_A] ; Ls = [_A,_B] ; ... . ?- list(true). false.After seeing they can actually affect the control flow of Prolog with

For example, from a quick first glance, it may appear that the two clauses are

list([]) :-A quick test case "confirms" that!. %incorrect! list([_|Ls]) :- list(Ls).

?- list("abc"). true.The

In Prolog, there are

?- list(Ls). Ls = []. %Thus, instead of anincompleteness!

To truly benefit from declarative programming, stay in the pure subset. Use

In such cases, consider the following query:

?- flatten(Xs, Ys).What is a valid answer in such situations? Suppose your predicate answers as follows:

Ys = [Xs].From a declarative point of view, this answer is

?- flatten(Xs, Ys), Xs = "a". Xs = "a", Ys = ["a"].Thus,

?- Xs = "a", flatten(Xs, Ys). Xs = Ys, Ys = "a".This means that exchanging the order of goals

Your instructor should be able to understand this fundamental declarative shortcoming if you point it out. In practice, use

From a quick first glance, this may seem very inefficient to you, because we are visiting the same solutions over and over, although we need each of them only

Now consider a search tree of depth

Let us now sum this up to calculate the total number of visits:

Now the point: This sum is asymptotically

It also shows that people usually

Note that iterative deepening requires