2013-02-18 181 views
1

我是Prolog的新手,我試圖編寫帶有「或」條件的if/else語句。因此,要證明,我想是這樣的:Prolog if/else語句帶「或」條件

gothrough([H|T], B, C):- 
    ( T == [] or H == 'then' %if either the tail is an empty list or if H == "then", do the following% 
    -> append(H,B,B), outputs(B,C) 
    ; append(H,B,B), gothrough(T, B, C) %else% 
    ). 

此實現但不工作;有沒有一種明顯的方式來做到這一點,我沒有得到?

謝謝!

回答

1

在Prolog中,使用「;」爲或和「,」爲和。

gothrough([H|T], B, C):- 
    ( (T == [] ; H == 'then') %if either the tail is an empty list or if H == "then", do the following% 
    -> append(H,B,B), outputs(B,C) 
    ; append(H,B,B), gothrough(T, B, C) %else% 
    ). 

當H爲[]不同注意追加(H,B,B)總是失敗。

你可以寫

gothrough([H|T], B, C):- 
    append(H,B,B), 
    ( (T == [] ; H == 'then') %if either the tail is an empty list or if H == "then", do the following% 
    -> outputs(B,C) 
    ; gothrough(T, B, C) %else% 
    ). 
+0

非常感謝您的評論!當H不同於[]時,你能否澄清你的意思總是失敗? – pauliwago 2013-02-18 22:15:40

+0

附加鏈表,例如'append([1],[2],B)''給出B = [1,2],所以當第一個參數是第二個參數時,append的參數總是不同的不同的空列表。 – joel76 2013-02-18 22:20:51