Re: Learning prolog



"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/
.



Relevant Pages

  • OT: Humor
    ... yet the plural of house is houses, ... English is a crazy language. ... English muffins weren't invented in England. ... your house can burn up as it burns down; ...
    (sci.med.transcription)
  • Re: Electric bills thread new question
    ... consider using an English to Spanish ... translator on your browser to make up a sign to hold up. ... have some bottled water or plastic cups inside the house so ...
    (alt.vacation.las-vegas)
  • Re: Electric bills thread new question
    ... consider using an English to Spanish ... translator on your browser to make up a sign to hold up. ... have some bottled water or plastic cups inside the house so ...
    (alt.vacation.las-vegas)
  • Re: Electric bills thread new question
    ... consider using an English to Spanish translator on your browser to make up a sign to hold up. ... have some bottled water or plastic cups inside the house so they can have water to drink and be sure to let them know it's OK to use the bathroom. ...
    (alt.vacation.las-vegas)
  • Re: "House of Rest" and "Mental science"
    ... > and can be nursed (English "rest home", ... "House of Rest" is not a common phrase. ... Google does come up with plenty of occurrences. ... It is definitely not a common phrase today. ...
    (alt.usage.english)