%%
%% The final version of fact/2 (factorial) we wrote using if-then-else.
%%

fact(N, F) :-
    ( N > 0 -> N1 is N-1, fact(N1, F1), F is F1 * N
    ; N =:= 0, F = 1
    ).

%%===========================================================================
%% Solution to the zebra puzzle (https://en.wikipedia.org/wiki/Zebra_Puzzle)
%%===========================================================================

/*
** The representation we use is a structured term of the form:
**    house(Nationality, Color, Pet, Smoke, Drink)
*/

zebra(Houses) :- 
    Houses = [house(norwegian, _, _, _, _),
	      house(_, blue, _, _, _),
	      house(_,_, _, _, milk), _H4, _H5],
    member(house(englishman, red, _, _, _), Houses),
    member(house(spaniard, _, dog, _, _), Houses),
    member(house(_, green, _, _, coffee), Houses),
    member(house(ukranian, _, _, _, tea), Houses),
    rightof(house(_, ivory, _, _, _), house(_, green, _, _, _), Houses),
    member(house(_, _, snails, old_gold, _), Houses),
    % member(house(_, yellow, _, kools, _), Houses),  % put in nextto below
    nextto(house(_, _, _, chesterfields, _), house(_, _, fox, _, _), Houses),
    nextto(house(_, yellow, _, kools, _), house(_, _, horse, _, _), Houses),
    member(house(_, _, _, lucky_strike, orange_juice), Houses),
    member(house(japanese, _, _, parliaments, _), Houses).

rightof(L, R, [L,R,_,_,_]).
rightof(L, R, [_,L,R,_,_]).
rightof(L, R, [_,_,L,R,_]).
rightof(L, R, [_,_,_,L,R]).

nextto(H1, H2, Houses) :- rightof(H1, H2, Houses).
nextto(H1, H2, Houses) :- rightof(H2, H1, Houses).

owner_zebra(Owner) :-
    zebra(Houses),
    member(house(Owner, _, zebra, _, _), Houses).
