Reputation: 11
I have seen similar previous posts, but I seem to still be stuck. My current if statement, even when it fails, outputs the "if true" code. So I'd like to ask what the proper syntax is if I don't want to do anything else if the condition turns to be false.
My code that fails:
(Disease == malaria
-> (patient_age(y)
-> writeln('Since patient is young, likelihood of malaria increases.')
; true)
;
Disease == tuberculosis
-> (patient_age(e)
-> writeln('Since patient is old, likelihood of tuberculosis increases.')
; true)
).
What happens is that, even if patient_age() is false, the writeln() statement is still executed. I have attempted replacing the "false code" with another writeln() statement, which did not display. I apologize that this has been asked before. Thank you very much.
Upvotes: 0
Views: 435
Reputation: 18683
It would be more declarative, idiomatic, and clear to rewrite the code to avoid the use of the if-then-else control construct. For example:
% likelihood(Disease, PatientAge)
likelihood(malaria, y) :-
writeln('Since patient is young, likelihood of malaria increases.').
likelihood(tuberculosis, e) :-
writeln('Since patient is old, likelihood of tuberculosis increases.').
Still, it would likely be best to move the written messages to a predicate that calls the likelihood/2
predicate. For example:
print_diagnostic(PatientAge) :-
likelihood(Disease, PatientAge),
write('Increased likelihood of '), write(Disease), nl.
Upvotes: 1