1

我是 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%
    ).

但是,此实现不起作用;有没有明显的方法可以做到这一点,我没有得到?

谢谢!

4

1 回答 1

2

在 Prolog 中,使用“;” for or and "," for and.

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 与 [] 不同时, append(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%
    ).
于 2013-02-18T22:10:55.263 回答