This question already has an answer here:
- Prolog if-statement 1 answer
What I'm trying to do is to write something on the console if any predicate is true.An example would be:
write('hello') IF member('a',['a','b']).
This question already has an answer here:
What I'm trying to do is to write something on the console if any predicate is true.An example would be:
write('hello') IF member('a',['a','b']).
A conjunction
a, b
is true (succeeds) ifa
is true, andb
is true. The order is significant:The if-then-else construct
Condition -> Then ; Else
is something slightly different and based on your question alone, I don't know if you really need it. For example, to avoid the extra choice point in these examples, you might want to write:Of course, you might actually need to look at each member. For example:
You should look at this carefully. There is no if-then-else, nor printing. I'd almost say that you are trying to solve a non-problem. Very generally speaking, you shouldn't need if-then-else or printing with
write
orformat
too often.The if-then-else can be avoided by using predicates that do it for you, as
member/2
vs.memberchk/2
shown above.The printing is done by the top level. I have trouble coming up with valid uses of
write
andformat
, unless you need to write to a file/stream. Even then, it is cleaner and easier to first get the final result ready, then write it to a file in one go.You could write:
or define a clause:
and query: