Previous topic  Top  Next topic  Print this Topic
 

Examples

 

For list operations you may use the built-in features concat and inlist (see chapter "Built-in Features").

Define a fact with a new list:

p([a,b,c]).

Separate a list:

?- p([?Head | ?Tail]).

The result will be:

?Head=a, ?Tail=[b,c]

All elements of the list:

?- _member([a,b,c],?X).

The result will be:

?X=a, ?X=b, ?X=c

Merge lists:

?- _concat([a,b],[c,d],?X).

The result will be:

?X=[a,b,c,d]

 

This is an extended example calculating a graph using lists:

// the edges of a graph between two nodes

edge(a,b).

edge(b,c).

edge(a,d).

edge(d,e).

edge(e,f).

 

// add each edge to a path containing two nodes

path([?Y,?X]) :- edge(?X,?Y).

 

// add every new edge to the appropriate path

path([?H1|?L]) :- path(?L) and _unify(?L,[?H2,?T]) and edge(?H2,?H1).

This query outputs all paths of the graph:

?- path(?L).