1 Answer
1

What this says is Ys is a prefix of Xs if Ys appended with some other list (I don't care what it is) is Xs.

Now in prolog, when you issue a query, such as append, and the predicate is defined relationally, it will attempt to find all solutions that make the query TRUE. If I have the query, append(Ys, _, [1,2,3]), prolog can make this true by instantiating Ys with [] and _ with [1,2,3] (thus yielding the solution, Ys = []. It will then backtrack and find another solution: Ys is [1] and _ is [2,3]. And so on...

Remember, Prolog seeks to find solutions by checking facts and rules and instantiating variables. If there's more than one way to instantiate the variables, it will backtrack. If backtracking finds another set of variable instantiations that makes the query true, Prolog will show it as a solution. If more backtracking is available, it prompts you for more (and entering ; says to give me the next one).

why have you changed the order of Xs and Ys in append(Ys, _, Xs).?
–
ShiranApr 3 '14 at 16:42

@Shiran Because that's the way append works. You are looking for a list (Ys) which when appended to another list (_) yields your given list Xs. In append, based upon how you defined how you want prefix(Xs, Ys) to work, Xs must be the result of the append operation, and Ys must be the first list being appended. Therefore, they are reverse order. The example description I gave illustrates that. I could do prefix(Xs, Ys) :- append(Xs, _, Ys)., but then I'd have to query as, prefix(X, [1,2,3]). which is not the way you asked for it to work.
–
lurkerApr 3 '14 at 16:45