We clearly see the recursive pattern repeating itself and can easily see that, for example, the result of the 100th step in recursion would look like: L1 = ,
Swi prolog tutorial examples free#
Notice that _A is already the head of L1, which perfectly matches the rule, so now, through recursion, Prolog puts _A in front of a new free variable and we get L1 = and 元 = Once more, Prolog tries to proof that some element placed in the head of L1, concatenated with L2 is 元 with that same element in its head.
Ī new recursive call is made, now with L1 =. To do so, a new free variable _A is unified with the head of L1 and 元 is proven to now be.
In the first answer, the base case was pattern matched and Prolog instantiated L1 to the empty list and unified L2 and 元 proving that 元 is the concatenation of the empty list and L2.Īt answer #2, through chronological backtracking, the recursive clause comes into play and Prolog tries to proof that some element in the head of L1 concatenated with L2 is 元 with that same element in its list head. Let's replace the free variable _G1162-like notation with alphabetical letters to get a better overview: ?- append(L1,L2,元). Think of it: because of Prolog's unification, we're not required to provide test data, we just hand it free variables! ?- append(L1,L2,元). It's always a good idea to initially test your predicate with the most general query rather than providing it with a specific scenario test case. “Assuming is the concatenation of and, then is also the concatenation of Then is also the concatenation of and L2. In plain English this simply translates to:Īssuming 元 is the concatenation of L1 and L2, Then so does the head: “then so does append(,L2,)” Now we say that if the body holds: “assuming that append(L1,L2,元) holds” The base case declaratively states "any L appended to the empty list is L", note that this says nothing about L being empty – or even being a list (remember, in Prolog everything boils down to terms): ?- append(X,some_term(a,b),Z).įor describing the recursive rule, although Prolog executes rules left-to-right, we omit the head for a second and look at the body first – reading the rule right-to-left: append(,L2,) :- append(L1,L2,元).
The difficulty here lies in trying to avoid any step-by-step recurring details while still keeping in mind the procedural behaviour the predicate should exhibit. When we try to figure out the declarative meaning of a predicate, we try to describe solutions for which the predicate holds. Viewed declaratively, append(L1,L2,元) holds when the list 元 is the result of appending lists L1 and L2. Recursive (continuing) clause: Contains any required logic including a call to itself, continuing recursion.Īs an example we shall define the well-known predicate append/3. It non-recursively describes the base of the recursive process. When writing such predicates in Prolog, a standard recursive pattern always has at least two parts:īase (non-recursive) clause: Typically the base-case rule(s) will represent the smallest possible example(s) of the problem that you are trying to solve - a list with no members, or just one member, or if you're working with a tree structure, it might deal with an empty tree, or a tree with just one node in it, etc. Recursion appears when a predicate contains a goal that refers to itself. Prolog doesn't have iteration, but all iteration can be rewritten using recursion.