Reputation: 21
I'm trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don't want it to say "true", i want it to say : "yes, the word hello is contained here", any ideas?
Here's my code :
contains_hello([hello|_]).
contains_hello([Head|Tail]):- Head \= hello, contains_hello(Tail).
Upvotes: 2
Views: 2671
Reputation: 61469
It is clear form your code that you only want the message to be printed once. Currently you solve this by adding Head \= hello
to the second clause. This is not how one usually solves this in Prolog.
Observe that your second clause excludes precisely that situation which is matched by the first clause. So, it is much more convenient to cut in the first clause. This stops backtracking. I.e., if the first clause matches, then the second clause will not be considered.
A cut is signified by the exclamation mark (!
). Thus:
contains_hello([hello|_]) :-
write('yes, the word hello is contained here'), nl, !.
contains_hello([_|T]) :- contains_hello(T).
Upvotes: 4
Reputation: 10064
This is untested but try:
contains_hello([hello|_]) :-
write('yes, the word hello is contained here'),
nl.
contains_hello([Head|Tail]) :- Head \= hello, contains_hello(Tail).
Upvotes: 3