/* A first version of factorial with two clauses 
fact(0, 1).
fact(N, F) :- N > 0, N1 is N - 1, fact(N1, F1), F is F1 * N.
*/

/* A second version of factorial with an if-then-else 
fact(N, F) :-
    ( N =:= 0 -> F = 1
    ; N > 0, N1 is N - 1, fact(N1, F1), F is F1 * N
    ).
*/

/* A tail-recursive version of factorial with an if-then-else */
fact(N, F) :- fact(N, 1, F).

fact(N, Fin, Fout) :-
    ( N =:= 0 -> Fout is Fin
    ; N > 0, N1 is N - 1, Fmid is Fin * N, fact(N1, Fmid, Fout)
    ).


/*======================================================*/
/*   Winning Tic-Tac-Toe				*/
/*======================================================*/

winner(L) :- L = [_, _, _], triple(L, Elem), is_winning(Elem).

triple(L, E) :- member(E, L).	                 % get the rows
triple(L, E) :- transpose(L, LT), member(E, LT). % get the columns
triple([[A,_,_],[_,B,_],[_,_,C]], [A,B,C]).
triple([[_,_,A],[_,B,_],[C,_,_]], [A,B,C]).

transpose([[A,B,C],[D,E,F],[G,H,I]], [[A,D,G],[B,E,H],[C,F,I]]).

is_winning([x,x,x]).
is_winning([x,x,b]).
is_winning([x,b,x]).
is_winning([b,x,x]).
