2012-02-08 47 views
0

我想在兩個列表(相同長度)中計算相同且位於相同位置的元素數。例如: 假設我們有列表A = [3,6,7,9]和B = 」。序言,計數器

到目前爲止,我有此:

bulls([],[]). 
bulls([Ha|Ta],[Hb|Tb]) :- 
    Ha = Hb, 
    writeln('bull found'), 
    bulls(Ta,Tb); 
    bulls(Ta,Tb). 

每當存在於兩個列表中的相同位置的元素,消息「公牛找到」被打印出來。 在我心目中,我想讓這樣的事情:

bulls([],[],_). 
bulls([Ha|Ta],[Hb|Tb],Counter) :- 
    Ha = Hb, 
    NewCounter is Counter + 1, 
    bulls(Ta,Tb,NewCounter); 
    bulls(Ta,Tb,NewCounter). 

bulls(List1,List2):- bulls(List1,List2,0). 

bulls是從通過排行榜中它的另一個規則調用。 我如何製作它,以便將「公牛」的值打印到屏幕上。任何幫助?


編輯 所以經過蘇基的後,我做了這個測試程序測試2所列出:

bulls([],[],X), write(X), write('bulls found'),fail. 
bulls([Ha|Ta],[Hb|Tb],Counter) :- 
    Ha = Hb, 
    NewCounter is Counter + 1, 
    bulls(Ta,Tb,NewCounter); 
    bulls(Ta,Tb,NewCounter). 

check(List1,List2):- 
    bulls(List1,List2,0). 


start:- 
    A=[1,1,1,1], 
    B=[2,1,2,1], 
    writeln(A),writeln(B), 
    check(A,B). 

,我得到這樣的結果

1 ?- start. 
[1,1,1,1] 
[2,1,2,1] 
ERROR: bulls/3: Arguments are not sufficiently instantiated 

我在做什麼錯誤?

回答

1

關於您編輯的程序:

第一個子句不是一個子句,它是一個目標!它應該看起來像這樣:

bulls([],[],X) :- write(X), write(' bulls found'). 

您應該丟棄fail,順便說一句。

在第二句話,你需要使用 「IF-THEN-ELSE」,並在 「其他」 分枝使用Counter代替NewCounter

bulls([Ha|Ta],[Hb|Tb],Counter) :- 
    (
    Ha == Hb 
    -> 
    NewCounter is Counter + 1, 
    bulls(Ta,Tb,NewCounter) 
    ; 
    bulls(Ta,Tb,Counter) 
). 
+0

謝謝您的幫助。不,我沒有得到一個錯誤,但輸出不是我所期待的。我開玩笑地說'1' - 開始。 [1,1,1,1] [2,1,2,1] false.' – 2012-02-09 11:24:47

+0

非常感謝。它工作正常! – 2012-02-09 11:30:28

0

假設謂詞的findAll,長度, nth0存在於你的序言實現:
(下面的記錄是從SWI-序言)

A = [3,6,7,9], 
B = [2,6,4,9], 
findall(Y, ((nth0(X, A, Y), nth0(X, B, Y))), Y), 
length(Y, LenY), 
write(LenY), write(' Bulls Found'). 

[trace] ?- A = [3,6,7,9], 
B = [2,6,4,9], 
findall(Y, ((nth0(X, A, Y), nth0(X, B, Y))), Y), 
length(Y, LenY), 
write(LenY), write(' Bulls Found'). 
    Call: (7) _G2814=[3, 6, 7, 9] ? creep 
    Exit: (7) [3, 6, 7, 9]=[3, 6, 7, 9] ? creep 
    Call: (7) _G2829=[2, 6, 4, 9] ? creep 
    Exit: (7) [2, 6, 4, 9]=[2, 6, 4, 9] ? creep 
^ Call: (7) findall(_G2834, (nth0(_G2832, [3, 6, 7, 9], _G2834), nth0(_G2832, [2, 6, 4, 9], _G2834)), _G2834) ? creep 
    Call: (13) lists:nth0(_G2832, [3, 6, 7, 9], _G2834) ? creep 
    Exit: (13) lists:nth0(0, [3, 6, 7, 9], 3) ? creep 
    Call: (13) lists:nth0(0, [2, 6, 4, 9], 3) ? creep 
    Fail: (13) lists:nth0(0, [2, 6, 4, 9], 3) ? creep 
    Redo: (13) lists:nth0(_G2832, [3, 6, 7, 9], _G2834) ? creep 
    Exit: (13) lists:nth0(1, [3, 6, 7, 9], 6) ? creep 
    Call: (13) lists:nth0(1, [2, 6, 4, 9], 6) ? creep 
    Exit: (13) lists:nth0(1, [2, 6, 4, 9], 6) ? creep 
    Redo: (13) lists:nth0(_G2832, [3, 6, 7, 9], _G2834) ? creep 
    Exit: (13) lists:nth0(2, [3, 6, 7, 9], 7) ? creep 
    Call: (13) lists:nth0(2, [2, 6, 4, 9], 7) ? creep 
    Fail: (13) lists:nth0(2, [2, 6, 4, 9], 7) ? creep 
    Redo: (13) lists:nth0(_G2832, [3, 6, 7, 9], _G2834) ? creep 
    Exit: (13) lists:nth0(3, [3, 6, 7, 9], 9) ? creep 
    Call: (13) lists:nth0(3, [2, 6, 4, 9], 9) ? creep 
    Exit: (13) lists:nth0(3, [2, 6, 4, 9], 9) ? creep 
^ Exit: (7) findall([6, 9], user: (nth0(_G2832, [3, 6, 7, 9], [6, 9]), nth0(_G2832, [2, 6, 4, 9], [6, 9])), [6, 9]) ? creep 
    Call: (7) length([6, 9], _G2848) ? creep 
    Exit: (7) length([6, 9], 2) ? creep 
    Call: (7) write(2) ? creep 
2 
    Exit: (7) write(2) ? creep 
    Call: (7) write(' Bulls Found') ? creep 
Bulls Found 
    Exit: (7) write(' Bulls Found') ? creep 
A = [3, 6, 7, 9], 
B = [2, 6, 4, 9], 
Y = [6, 9], 
LenY = 2.