% Author:   Sallam
% http://sallamah.weebly.com
% Date: 4/11/2014

% understanding back-tracking

possible_pair(X, Y) :- boy(X), girl(Y).

boy(john).   %-----
boy(marmaduke).
boy(bertram).
boy(charles).
girl(griselda).    %&&&&&
girl(ermintrude).
girl(brunhilde).

%----------------- First use: Confirming the choice or a rule

sum_to(1,1).
sum_to(N, R):-
            N1 is N-1,
            sum_to(N1,R1),
            R is N+R1.

% what the problem, and why it doesn't work same as listlen([],0)
%-----------------
/*
sum_to(1,1).
sum_to(N, R):-
            %\+(N=<1),%N>1,
            N1 is N-1,
            sum_to(N1,R1),
            R is N+R1.
*/
%-----------------
/*
sum_to(1,1):-!.
sum_to(N, R):-
            %\+(N=<1),%N>1,
            N1 is N-1,
            sum_to(N1,R1),   % the point is every call for this line should try (sum_to(1,1) and backtrack to sum_to(N,R))
            R is N+R1.
*/

%------------------------- cut-fail

grade3_fees(X, Discount,Fees):- foreigner(X),!,fail.
grade3_fees(X,Discount, Fees):- Fees is 200-Discount.

% better way to write the cut-fail

%grade3_fees(X,Discount,Fees):- \+foreigner(X), Fees is 200-Discount.

% --------------------------- Termination
line(b(X,Y,Z,_,_,_,_,_,_), X, Y, Z).
line(b(_,_,_,X,Y,Z,_,_,_), X, Y, Z).
line(b(_,_,_,_,_,_,X,Y,Z), X, Y, Z).
line(b(X,_,_,Y,_,_,Z,_,_), X, Y, Z).
line(b(_,X,_,_,Y,_,_,Z,_), X, Y, Z).
line(b(_,_,X,_,_,Y,_,_,Z), X, Y, Z).
line(b(X,_,_,_,Y,_,_,_,Z), X, Y, Z).
line(b(_,_,X,_,Y,_,Z,_,_), X, Y, Z).

threatening(e, x, x).
threatening(x, e, x).
threatening(x, x, e).

forced_move(Board) :-
line(Board, X, Y, Z), %genrate
threatening(X, Y, Z), %test
!.

%test with forced_move(b(x,o,o,e,e,x,o,e,e)).

 % another example
 
divide(N1, N2, Result) :-
     is_integer(Result),
     Product1 is Result * N2,
     Product2 is (Result + 1) * N2,
     Product1 =< N1, Product2 > N1,
     !.
     
is_integer(0).
is_integer(X) :- is_integer(Y), X is Y + 1.

%------------------ problem with cut
append([], X, X):-!.
append([A|B], C, [A|D]) :- append(B, C, D).

% try append(X, Y, [a,b,c]).
