// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample solves the classic Houses logical puzzle, "who owns the zebra and who drinks water?":

1) Five colored houses in a row, each with an owner, a pet, cigarettes, and a drink.
2) The English lives in the red house.
3) The Spanish has a dog.
4) They drink coffee in the green house.
5) The Ukrainian drinks tea.
6) The green house is next to the white house.
7) The Winston smoker has a serpent.
8) In the yellow house they smoke Kool.
9) In the middle house they drink milk.
10) The Norwegian lives in the first house from the left.
11) The Chesterfield smoker lives near the man with the fox.
12) In the house near the house with the horse they smoke Kool.
13) The Lucky Strike smoker drinks juice.
14) The Japanese smokes Kent.
15) The Norwegian lives near the blue house.

depending on your computer (speed, memory) solving it may take a while ...

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #zebra_owner(:Owner)
-> ( japanese ) := 1.00 (7.147) 1
?- #water_drinker(:Drinker)
-> ( norwegian ) := 1.00 (7.913) 1
?- #houses(:Hs)
-> ( [h(norwegian, fox, kool, water, yellow), h(ukrainian, horse, chesterfield, tea, blue), h(english, snake,
winston, milk, red), h(spanish, dog, lucky, juice, white), h(japanese, zebra, kent, coffee, green)] ) := 1.00 (6.200) 1

-> ( [h(norwegian, fox, kool, water, yellow), h(ukrainian, horse, chesterfield, tea, blue), h(english, snake,
winston, milk, red), h(japanese, zebra, kent, coffee, green), h(spanish, dog, lucky, juice, white)] ) := 1.00 (6.250) 2

 
// Code ----------------------------------------------------------------------------------------------------------------------------------

append {

    ([],:X,:X);
    ([:X|:L1],:L2,[:X|:L3]) :- #append(:L1,:L2,:L3);

}

next {

    (:A,:B,:L) :- #append(_, [:A,:B|_], :L);
    (:A,:B,:L) :- #append(_, [:B,:A|_], :L);

}

are.same {

    (:X,:X);

}

houses {

    (:Hs) :-    lst.length(:Hs, 5),
                lst.member(h(english,_,_,_,red), :Hs),
                lst.member(h(spanish,dog,_,_,_), :Hs),
                lst.member(h(_,_,_,coffee,green), :Hs),
                lst.member(h(ukrainian,_,_,tea,_), :Hs),
                #next(h(_,_,_,_,green), h(_,_,_,_,white), :Hs),
                lst.member(h(_,snake,winston,_,_), :Hs),
                lst.member(h(_,_,kool,_,yellow), :Hs),
                are.same([_,_,h(_,_,_,milk,_),_,_],:Hs),
                are.same([h(norwegian,_,_,_,_)|_],:Hs),
                #next(h(_,fox,_,_,_), h(_,_,chesterfield,_,_), :Hs),
                #next(h(_,_,kool,_,_), h(_,horse,_,_,_), :Hs),
                lst.member(h(_,_,lucky,juice,_), :Hs),
                lst.member(h(japanese,_,kent,_,_), :Hs),
                #next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), :Hs),
                lst.member(h(_,_,_,water,_), :Hs),
                lst.member(h(_,zebra,_,_,_), :Hs);

}

zebra_owner {

    (:Owner) :- #houses(:Hs), lst.member(h(:Owner,zebra,_,_,_), :Hs);

}

water_drinker {

    (:Drinker) :- #houses(:Hs), lst.member(h(:Drinker,_,_,water,_), :Hs);

}

//----------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]