Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
itemlost_becauseof(shoe, nail).
itemlost_becauseof(horse, shoe).
itemlost_becauseof(rider, horse).
itemlost_becauseof(battle, rider).
itemlost_becauseof(kingdom, battle).
write_poem(ItemLost) :-
write_poem_aux(ItemLost);
write_last_line(ItemLost).
write_poem_aux(ItemLost) :-
itemlost_becauseof(Impacted, ItemLost),
write_line(ItemLost, Impacted),
write_poem_aux(Impacted).
write_line(Item, Impacted) :-
write("For want of a "), write(Item), write(" a "), write(Impacted), write(" was lost;"), nl.
write_last_line(Item) :-
write("And all for the want of a "), write(Item), write("."), nl.
%% Example poem (try in a Prolog repl)
?- write_poem(horse).
%% Root cause and impact
loss_rootcause(ItemLost, Cause) :-
itemlost_becauseof(ItemLost, Z), loss_rootcause(Z, Cause), !.
loss_rootcause(ItemLost, Cause) :-
itemlost_becauseof(ItemLost, Cause).
item_impacted(Cause, ItemLost) :-
itemlost_becauseof(ItemLost, Cause).
item_impacted(Cause, ItemLost) :-
itemlost_becauseof(ItemLost, X),
item_impacted(Cause, X).
%% To find the root cause on kindom's downfall
?- loss_rootcause(kingdom, RootCause).
%% To find the impact of a missing nail
?- findall(X, item_impacted(nail, X), NailImpacted).
%% Annotated names
annotated_name(nail, horse-nail).
annotated_name(X, X).
write_annotated(X) :-
annotated_name(X, Annotated), write(Annotated).
% write_last_line(Item) :-
% write("And all for the want of a "), write_annotated(Item), write("."), nl.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.