Information Technology Reference
In-Depth Information
The predicate write (
)
is considered to always succeed, and it has the effect
of printing the value of the term .
term
The predicate nl (for newline ) with no arguments is considered to always
succeed, and it has the effect of going to a new line of output.
To see how these work, look at how the following queries are answered:
?- X=joe, write('variable X is '), write(X), write('!!').
variable X is joe!!
X = joe
Yes
?- write('Paris in the'), nl, write('
spring.').
Paris in the
spring.
Yes
So with these predicates, the solution to the map-coloring problem can be displayed
better by adding the following clause to the program of figure 5.2:
print_colors :-
solution(A,B,C,D,E), nl,
write('Country A is colored '), write(A), nl,
write('Country B is colored '), write(B), nl,
write('Country C is colored '), write(C), nl,
write('Country D is colored '), write(D), nl,
write('Country E is colored '), write(E), nl.
The
predicate print_colors first
finds
a
solution
to
the
constraint
satisfaction
problem using solution , and then prints it in a readable form:
?- print_colors.
Country A is colored red
Country B is colored white
Country C is colored blue
Country D is colored white
Country E is colored blue
Note that the query in this case no longer has variables. All the variables are within
the clause for the print_colors predicate and are displayed explicitly using write
and nl . This does not change the thinking involved in producing a solution. But it
results in a much more convenient program, since anyone running the program does
not have to know in advance how to use or interpret the variables.
 
Search WWH ::




Custom Search