my_append(L1,L2,Res) :- L1 = [], Res = L2. my_append(L1,L2,Res) :- L1 = [H|T], my_append(T,L2,R), Res = [H|R]. inc0(X,N) :- N = X+1. inc1(X,N) :- N is X+1. len([],0). len([_|T], N) :- len(T,A), N is A+1. max_of_list([X],X). max_of_list([H|T], Max) :- max_of_list(T,M), H > M, Max = H. max_of_list([H|T], Max) :- max_of_list(T,M), H =< M, Max = M. ?- use_module(library(clpfd)). puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :- Vars = [S,E,N,D,M,O,R,Y], Vars ins 0..9, all_different(Vars), S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E #= M*10000 + O*1000 + N*100 + E*10 + Y, M #\= 0, S #\= 0. p(a). p(b). q(x). q(y). r(0,0). r(X,Y) :- p(X), q(Y). r(1,2). p1(X,Y) :- X > Y. p2(X,Y) :- [X,Y] ins -1000 .. 1000, X #> Y. all_sum_equal([], _). all_sum_equal([H|T], N) :- sum_equal(H,N), all_sum_equal(T,N). sum_equal([],0). sum_equal([H|T], N) :- sum_equal(T,M), N #= H+M. carre_magique(Vars,N) :- Vars = [A,B,C, D,E,F, G,H,I], Rows = [[A,B,C], [D,E,F], [G,H,I]], Cols = [[A,D,G], [B,E,H], [C,F,I]], Diag = [[A,E,I], [G,E,C]], Vars ins 1 .. 9, all_different(Vars), all_sum_equal(Rows,N), all_sum_equal(Cols,N), all_sum_equal(Diag,N).