Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving logic puzzle in Prolog [closed]

I am reading "Learn Prolog Now" and one of its exercises I haven't been able to solve myself is the following:

There is a street with three neighboring houses that all have a different color. They are red, blue, and green. People of different nationalities live in the different houses and they all have a different pet. Here are some more facts about them:

  • The Englishman lives in the red house.
  • The jaguar is the pet of the Spanish family.
  • The Japanese lives to the right of the snail keeper.
  • The snail keeper lives to the left of the blue house.

Who keeps the zebra?

Define a predicate zebra/1 that tells you the nationality of the owner of the zebra.

Hint: Think of a representation for the houses and the street. Code the four constraints in Prolog. member and sublist might be useful predicates.

Any ideas how to code it under Prolog? Thanks.

like image 872
nunos Avatar asked Jan 17 '11 18:01

nunos


1 Answers

neigh(Left, Right, List) :- 
        List = [Left | [Right | _]];
        List = [_ | [Left | [Right]]].

zebraowner(Houses, ZebraOwner):-
        member([englishman, _, red], Houses),
        member([spanish, jaguar, _], Houses),
        neigh([_, snail, _], [japanese, _, _], Houses),
        neigh([_, snail, _], [_, _, blue], Houses),
        member([ZebraOwner, zebra, _], Houses),
        member([_, _, green], Houses).


zebra(X) :- zebraowner([_, _, _], X).
like image 200
ДМИТРИЙ МАЛИКОВ Avatar answered Oct 29 '22 14:10

ДМИТРИЙ МАЛИКОВ