Prolog - 计算两个列表中的匹配项
Prolog - Counting matches in two lists
我正在尝试理解序言的语言(使用程序Visual Prolog 5.1/5.2)
有一个任务-实现游戏"Bulls and Cows"(或杰出的头脑)
玩家做一个数字(由0到9的数字组成,不重复),例如1458,输入到程序中。
计算机试图猜测这个数字,提出假设。然后他问玩家有多少“公牛”(完全匹配),以及多少“牛”(匹配但位置不对的数字)。
我写了一个程序来做到这一点。有效。
现在要电脑统计牛和牛的数量。怎么做才对?
这是一个有两个列表的作品。
DOMAINS
list = integer*
list1 - Key (hidden number), list2 - assumption (computer)
我试图写一个计算公牛的谓词,但是程序产生了"No solution"(可能是因为下面的原因(converting))
comparison(_Key, Assemption, Bulls /*, Cows*/):-
find_bulls(_Key, Assemption, Bulls),
%find_cows(_Key, Assemption, Cows).
find_bulls(Key, Assemption, Bulls):-
find_bulls(Key, Assemption, Bulls, 0).
find_bulls([],[], Bulls, Amount):- Bulls=Amount,!.
find_bulls([A1|L1],[A2|L2], Bull, Amount):-
A1 = A2,
Am = Amount + 1,
find_bulls(L1, L2, Bull, Am).
我也通过readint(_Key)
读取了数字,但是_Key不是一个列表,是吗? 如何将整数转换为列表以提供谓词?
question(Assemption,_Key):-
assemption(Assemption),
not(contradictory(Assemption)),
%------------------------------------------------------------
comparaison(_Key,Assemption, Bulls /*,Cows*/),
%------------------------------------------------------------
write(" My assemption: ", Assemption), nl,
write("How much bulls? -----> ", Bulls), nl, %readint(Bulls),
write("How much cows? -----> "), /*Cows)*/ readint(Cows), nl,
Bulls + Cows <= size,
assert(request(Assemption, Bulls, Cows)),
Bulls = size,
!.
谢谢!
/------------------------------------ --------------------------------------/
new 添加(从键盘读取列表,输入字符时停止):
domains
list=integer*
predicates
readlist(list)
clauses
readlist([H|T]):-
write("> "),
readint(H),!,
readlist(T).
readlist([ ]).
[已解决]
find_bull_and_cows(OldAssemption,Assemption,Bulls,Cows):-
exact_match(OldAssemption,Assemption,Bulls),
common_numbers(OldAssemption,Assemption,N2),
Cows= N2 - Bulls.
%bulls finding
exact_match(X,Y,N):- exact_match(X,Y,0,N).
exact_match([Digit|OldPin],[Digit|NewPin],K,N):- !,
K1=K+1,
exact_match(OldPin,NewPin,K1,N).
exact_match([_|OldPin],[_|NewPin],K,N):- exact_match(OldPin,NewPin,K,N).
exact_match([],[],N,N).
%cows finding
common_numbers(X,Y,N):- common_numbers(X,Y,0,N).
common_numbers([Digit|OldPin],NewPin,K,N):-
member(Digit,NewPin), !,
K1=K+1,
common_numbers(OldPin,NewPin,K1,N).
common_numbers([_|OldPin],NewPin,K,N):- common_numbers(OldPin,NewPin,K,N).
common_numbers([],_,N,N).
%member of list
member(A,[A|_]):- !.
member(A,[_|L]):- member(A,L).
我正在尝试理解序言的语言(使用程序Visual Prolog 5.1/5.2)
有一个任务-实现游戏"Bulls and Cows"(或杰出的头脑)
玩家做一个数字(由0到9的数字组成,不重复),例如1458,输入到程序中。 计算机试图猜测这个数字,提出假设。然后他问玩家有多少“公牛”(完全匹配),以及多少“牛”(匹配但位置不对的数字)。 我写了一个程序来做到这一点。有效。
现在要电脑统计牛和牛的数量。怎么做才对?
这是一个有两个列表的作品。
DOMAINS
list = integer*
list1 - Key (hidden number), list2 - assumption (computer)
我试图写一个计算公牛的谓词,但是程序产生了"No solution"(可能是因为下面的原因(converting))
comparison(_Key, Assemption, Bulls /*, Cows*/):-
find_bulls(_Key, Assemption, Bulls),
%find_cows(_Key, Assemption, Cows).
find_bulls(Key, Assemption, Bulls):-
find_bulls(Key, Assemption, Bulls, 0).
find_bulls([],[], Bulls, Amount):- Bulls=Amount,!.
find_bulls([A1|L1],[A2|L2], Bull, Amount):-
A1 = A2,
Am = Amount + 1,
find_bulls(L1, L2, Bull, Am).
我也通过readint(_Key)
读取了数字,但是_Key不是一个列表,是吗? 如何将整数转换为列表以提供谓词?
question(Assemption,_Key):-
assemption(Assemption),
not(contradictory(Assemption)),
%------------------------------------------------------------
comparaison(_Key,Assemption, Bulls /*,Cows*/),
%------------------------------------------------------------
write(" My assemption: ", Assemption), nl,
write("How much bulls? -----> ", Bulls), nl, %readint(Bulls),
write("How much cows? -----> "), /*Cows)*/ readint(Cows), nl,
Bulls + Cows <= size,
assert(request(Assemption, Bulls, Cows)),
Bulls = size,
!.
谢谢!
/------------------------------------ --------------------------------------/
new 添加(从键盘读取列表,输入字符时停止):
domains
list=integer*
predicates
readlist(list)
clauses
readlist([H|T]):-
write("> "),
readint(H),!,
readlist(T).
readlist([ ]).
[已解决]
find_bull_and_cows(OldAssemption,Assemption,Bulls,Cows):-
exact_match(OldAssemption,Assemption,Bulls),
common_numbers(OldAssemption,Assemption,N2),
Cows= N2 - Bulls.
%bulls finding
exact_match(X,Y,N):- exact_match(X,Y,0,N).
exact_match([Digit|OldPin],[Digit|NewPin],K,N):- !,
K1=K+1,
exact_match(OldPin,NewPin,K1,N).
exact_match([_|OldPin],[_|NewPin],K,N):- exact_match(OldPin,NewPin,K,N).
exact_match([],[],N,N).
%cows finding
common_numbers(X,Y,N):- common_numbers(X,Y,0,N).
common_numbers([Digit|OldPin],NewPin,K,N):-
member(Digit,NewPin), !,
K1=K+1,
common_numbers(OldPin,NewPin,K1,N).
common_numbers([_|OldPin],NewPin,K,N):- common_numbers(OldPin,NewPin,K,N).
common_numbers([],_,N,N).
%member of list
member(A,[A|_]):- !.
member(A,[_|L]):- member(A,L).