Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
%% %% %% %% FILE kakuro.pl SYSTEM LOGRES CREATED TA-070911 REVISED TA-070912 %% This program describes a small Prolog program %% for solving a Kakuro inspired problem %% Together with it follows a set of useful list manipulation predicates :-prolog_flag(single_var_warnings,_,off). %% Problem /* Make a Prolog predicate kakuro(L,S)} that tests whether L is a list of different integers between 1 and 9, and S is the sum of the integers in L. Example: kakuro([1,3],4) is true kakuro([1,3],5) is false kakuro([1,3,0],4) is false kakuro([1,1,3],4) is false For that purpose, we need a Prolog predicate listsum(L,S) where L is a list of integers , and sum of the numbers in L. S is the Examples: listsum([1,3,6],Z) is true with Z=10 listsum([],Z) is true with Z=0 We also need a Prolog predicate alldifferent(L) where L is a list of integers, and all the elements of X are different. Example alldifferent([]) is true alldifferent([3,2,1]) is true alldifferent([3,2,1,3]) is false */ %% Main programs kakuro(L,S) :alldigits(L), alldifferent(L), listsum(L,S). alldigits([]). alldigits([X|Y]) :digit1(X), alldigits(Y). alldifferent([]). alldifferent([X|Y]):alldifferent(Y), notmember(X,Y). listsum([],0). listsum([X|Y],S):listsum(Y,S1), S is S1 + X. %% Sub programs digit1(X) :member(X,[1,2,3,4,5,6,7,8,9]). %% List predicates car([Head|Tail],Head). cdr([Head|Tail],Tail). cons(Head,Tail,[Head|Tail]). member(X,[X|_]). member(X,[U|V]):member(X,V). append([],X,X). append([U|V],Y,[U|Z]):append(V,Y,Z). last(Elt,[Elt]). last(Elt,[_|Xs]):last(Elt,Xs). listlength([],0). %% length/2 is predefined listlength([X|Y],N) :listlength(Y,N1), N is N1 + 1. notmember(X,[]). notmember(X,[U|V]):differ(X,U), notmember(X,V). /* notmember(X,L) :- %% Alternative \+ member(X,L). %% Negation as failure %% (\+ means not provable). */ %% Utility ans(X) :write(X),nl,fail. differ(X,Y) :- X \== Y. %%%%%%%%%%%%%%%%%%%%%%%%%% /* % Example Run. % sicstus ?-[kakuro]. ?-kakuro([1,3],4). yes ?- kakuro([1,3],5). no ?-kakuro([1,3,0],4). no ?-kakuro([1,1,3],4). no ?- kakuro([1,2,3],H). H = 6 ? yes %% Inverting Input/Output is possible here | ?- kakuro([X,Y,Z],7). X = 1, Y = 2, Z = 4 ? X = 1, Y = 4, Z = 2 ? X = 2, Y = 1, Z = 4 ? X = 2, Y = 4, Z = 1 ? X = 4, Y = 1, Z = 2 ? X = 4, Y = 2, Z = 1 ? no | ?*/ ; ; ; ; ; ;