Commit 6d554cd9 authored by martin.hecker's avatar martin.hecker
Browse files

some ECLiPSe CLP examples

parent 2f2303b2
:- lib(ic).
sendmore([S,E,N,D,M,O,R,Y]) :-
[S,E,N,D,M,O,R,Y] :: [0..9],
alldifferent([S,E,N,D,M,O,R,Y]),
S #\= 0, M #\= 0,
1000*S + 100*E + 10*N + D
+ 1000*M + 100*O + 10*R + E
#= 10000*M + 1000*O + 100*N + 10*E + Y.
solve([S,E,N,D,M,O,R,Y]) :-
[S,E,N,D,M,O,R,Y] :: [0..9],
S #\= 0, M #\= 0, alldifferent([S,E,N,D,M,O,R,Y]),
D + E #= Y + 10*C1,
C1 + N + R #= E + 10*C2,
C2 + E + O #= N + 10*C3,
C3 + S + M #= O + 10*M.
% solve([S,E,N,D,M,O,R,Y]) :-
% [S,E,N,D,M,O,R,Y] :: [0..9],
% S #\= 0, M #\= 0, alldifferent([S,E,N,D,M,O,R,Y]),
% C3 + S + M #= O + 10*M+O,
% C2 + E + O #= N + 10*C3,
% C1 + N + R #= E + 10*C2,
% D + E #= Y + 10*C1.
qsort([],[]).
qsort([X|R],Y) :- split(X,R,R1,R2),
qsort(R1,Y1),
qsort(R2,Y2),
append(Y1,[X|Y2], Y).
split(X,[],[],[]).
split(X,[H|T],[H|R],Y) :- X#>H, split(X,T,R,Y).
split(X,[H|T],R,[H|Y]) :- X#=<H, split(X,T,R,Y).
fibn(0,0).
fibn(1,1).
fibn(X,Y) :- X>1,
X1 is X-1, X2 is X-2,
fibn(X1,Y1), fibn(X2,Y2),
Y is Y1+Y2.
fib(0,0).
fib(1,1).
fib(X,Y) :- X#>1, X1 #= X-1, X2 #= X-2,
fib(X1,Y1), fib(X2,Y2),
Y #= Y1+Y2.
% fib(N,0) :- N#=0.
% fib(N,1) :- N#=1.
% fib(X,Y) :- X#>1,fib(X-1,Y1), fib(X-1,Y2),
% Y #= Y1+Y2.
% N>1, fib(N-1,Y1), fib(N-2,Y2), 2=Y1+Y2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment