Reputation: 6665
I'm new to Prolog, and I'm trying to write an if/else statement with an "or" condition. So to demonstrate, I want something like:
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%
).
This implementation doesn't work however; is there an obvious way to do this that I'm not getting?
Thanks!
Upvotes: 1
Views: 13452
Reputation: 5645
In Prolog, use ";" 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%
).
Notice that append(H, B, B) always fails when H is different of [].
You can write
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%
).
Upvotes: 2