pauliwago
pauliwago

Reputation: 6665

Prolog check if function "returned" no?

I'm new to prolog. This is a hypothetical question - I realize it has no practical use. But I'm trying to figure out how to get Prolog to behave one way if a "no" return is received, and another if a "yes" is received.

I'm trying to write a function gothrough that takes a list, checks to see if the head of the list is equal to ',' and if it is, passes it to another function checkit. checkit checks to see if that passed variable is equal to ';'. If it is, it returns yes. If not, it returns no. I want gothrough to understand this return value and act accordingly. I understand that prolog doesn't actually return values, but I don't really know how else to describe what I want to do.... So I have this so far:

 checkit(H):- H==';'.
 gothrough([H|T]):- H==',', checkit(H), /*what do put here?  if/else statement depending on return value of checkit*/

Thank you!

Upvotes: 1

Views: 570

Answers (1)

gusbro
gusbro

Reputation: 22585

First, note that the way you are calling checkit/1 will always fail, as H is already bound to ',' so it clearly cannot unify with ';' at the same time.

Now, regarding your if-then-else question, use ->/2 control predicate:

gothrough([H|T]):- 
   H==',', 
   (checkit(H) -> 
         writeln(true_part) ; 
         writeln(false_part)
   ).

Upvotes: 3

Related Questions