Re: Learning prolog
- From: Markus Triska <triska@xxxxxxxx>
- Date: Fri, 25 Jan 2008 18:15:58 +0100
"John Thingstad" <jpthing@xxxxxxxxx> writes:
member(X, [H | T]) :- X = H; member(X, T).
member/2 should be among your Prolog's library predicates.
color(C) :- member(C, [red, blue, green]).
inhabitant(I) :- member(I, [english, spanish, japanese]).
pet(P) :- member(P, [snail, jaguar, zebra]).
This makes sense.
house(X, Y, Z) :- color(X), inhabitant(Y), pet(Z).
This is OK. You could use a structure of the form h(C,I,P) or
house(C,I,P) to represent a house:
house(house(C,I,P)) :- color(C), inhabitant(I), pet(P).
This will make it easier to refer to a whole house at once.
house(color(red), inhabitant(english), pet(_)).
house(color(_), inhabitant(spanish), pet(jaguar)).
These are alternative clauses for house/3. But you already said quite
clearly what you mean with a house. There's also no need to use wrappers
like color/1 inside houses - you simply adopt the convention that the
first argument of the structure house/3 denotes that house's colour.
street([X, Y, Z]) :-
house(X,_,_), house(Y,_,_), house(Z,_,_),
X \= Y, X \= Z, Y \= Z.
Here you would enforce the additional constraints on the houses. For
example, one of the houses must be of the form:
house(red, english, _)
another one must be of the form:
house(_, spanish, jaguar)
So the body could look a bit like:
house(House1), house(House2), house(House3),
Hint1 = house(red, english, _),
( House1 = Hint1 ; House2 = Hint1 ; House3 = Hint1 ),
Hint2 = house(_, spanish, jaguar),
( House1 = Hint2 ; House2 = Hint2 ; House3 = Hint2),
...
In addition, the houses must be lined up in the right order etc.
Not much is missing to turn your code into a complete solution. You can
then compare it with the following quite different approach:
same_pos([X|_], [Y|_], X, Y).
same_pos([_|Rs1], [_|Rs2], X, Y) :- same_pos(Rs1, Rs2, X, Y).
street(Cs, Ns, Ps) :-
permutation([red,blue,green], Cs),
permutation([english,japanese,spanish], Ns),
permutation([jaguar,snail,zebra], Ps),
same_pos(Ns, Cs, english, red), % hint 1
same_pos(Ns, Ps, spanish, jaguar), % hint 2
same_pos([+|Ps], Ns, snail, japanese), % hint 3
same_pos(Cs, [+|Ps], blue, snail). % hint 4
Usage:
?- street(Cs, Ns, Ps), same_pos(Ps, Ns, zebra, N).
--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/
.
- Follow-Ups:
- Re: Learning prolog
- From: John Thingstad
- Re: Learning prolog
- References:
- Learning prolog
- From: John Thingstad
- Learning prolog
- Prev by Date: Re: thanks
- Next by Date: Prolog, memory management and memory leaks
- Previous by thread: Re: Learning prolog
- Next by thread: Re: Learning prolog
- Index(es):
Relevant Pages
|
|