Skip to content

Instantly share code, notes, and snippets.

@dewaka
Last active October 6, 2019 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dewaka/36f24c3c1d527af30ae3944a994274c3 to your computer and use it in GitHub Desktop.
Save dewaka/36f24c3c1d527af30ae3944a994274c3 to your computer and use it in GitHub Desktop.
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