Table of contents
Open Table of contents
Intro
Like any other programming language, also in Prolog we have data structures, here we will talk about 3 of them:
- lists
- numbers (primitive values)
- graphs
Thinking in Prolog
Before starting with the data structure let’s set the right mindset with a step-by-step list:
- Describe relations, not algorithms.
- Prototype queries to discover the needed facts/rules.
- Ensure predicates are fully relational unless you really only need one mode.
- Test every mode and watch for non-termination.
- Use the library for numbers & lists; write your own only when learning.
- Add cuts only after the logic is correct.
The cut is a special, zero-argument goal written as !.
max(A, B, A) :- A >= B, !. % once this succeeds, stop exploring
max(_, B, B). % otherwise choose the second clause
what_is_cut.pl
Lists
Syntax
The concrete syntax:
[H1, H2|T]
[a, b, c]
[]
Built-ins for Lists
The number after the slash is not part of the predicate’s name, it states the predicate’s arity (the count of arguments that predicate takes).
Predicate (Arity) | Purpose | Typical Use |
---|---|---|
member/2 | true if Element is in List | member(X,[a,b,c]). |
append/3 | concatenates or splits lists | append([a,b],[c,d],L). |
length/2 | List ⇄ Length (integer) | length(L,3). |
reverse/2 | List ⇄ ReversedList | reverse([a,b],R). |
select/3 | remove/insert single element | select(X,L,R). |
nth0/3 / nth1/3 | 0- or 1-based indexing | nth1(2,[a,b,c],E). |
maplist/3.. | apply Goal to each pair of lists | maplist(succ,In,Out). |
All of these are relational: any argument may be a variable (though length/2
requires its second arg ground when generating).
Practice Lists
Build a list which search for 2 consecutive occurencies of an element.
- The predicate uses pattern matching and recursion
search2(Elem, [Elem, Elem | _]).
search2(Elem, [_ | Rest]) :- search2(Elem, Rest).