4 ms·
Nice, but this: should_isolate(Person) :- has_symptoms(Person); tested_positive(Person); (
by YeGoblynQueenne 6y ago
Nice, but this:
should_isolate(Person) :- has_symptoms(Person);
tested_positive(Person);
(
lives_with_others(Person),
format(atom(LivingWithPeople), 'anyone living with ~w', Person),
(
has_symptoms(LivingWithPeople);
tested_positive(LivingWithPeople)
)
);
(
in_bubble(Person),
format(atom(BubblePeople), 'anyone in ~w\'s bubble', Person),
(
(
has_symptoms(BubblePeople),
contact_with_symptomatic_bubble_member(Person)
);
(
tested_positive(BubblePeople),
contact_with_positive_bubble_member(Person)
)
)
);
contacted_by_test_and_trace(Person);
(
arrived_from_abroad_recently(Person),
\+ has_negative_test_to_release_result(Person)
).
Is absolutely awful Prolog style :)
First of all it's hard to read, not least because you have your semocilons (semicola?) formatted inconsistently: some are at the end of a line, others at the start, so it's different to follow the program flow. More importantly, it's hard to see at a glance whether some "cases" are meant to backtrack on failure or not. Normally, if you wanted to write a Big Hairy, Horrible Case Statement in Prolog then you should at least use the control flow with ->/2 which amounts to a "soft cut" after the first <condition>:
( <if-condition>
-> <then-consequence>
; <else-alternative>
)
But the recommended thing to make your code easier to read (_and debug_) and less like they do in Java is to use separate clauses to handle different "cases" of your program's logic.
Also, bad idea to interleave messages to user with "business logic" like you do, e.g. in:
lives_with_others(Person),
format(atom(LivingWithPeople), 'anyone living with ~w', Person),
It's possible with a bit of elbow grease to completely separate the I/O of communication with the user from the rules-based logic of your program.
There are actually coding guidelines for Prolog! Here:
https://arxiv.org/abs/0911.2899 https://arxiv.org/abs/0911.2899
Really, really good source on good Prolog style that paper (and where I base my comments above).
- zvr 6y agoWould love to see a rewritten version based on this!