Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 23, 2018 14:38
Show Gist options
  • Save DonaldKellett/097f62adbf29c63ba49a0c4c3f230f71 to your computer and use it in GitHub Desktop.
Save DonaldKellett/097f62adbf29c63ba49a0c4c3f230f71 to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 6 - Practical Session - Programming Exercise 2 - Create a set from a list
% set_accum/3 - A helper function using an acculumator to form a set from a given list
set_accum([H | T], Acc, Result) :- member(H, Acc), set_accum(T, Acc, Result).
set_accum([H | T], Acc, Result) :- \+ member(H, Acc), set_accum(T, [H | Acc], Result).
set_accum([], Acc, Acc).
% set/2 - Create a set of elements from a list
set(InList, OutList) :- set_accum(InList, [], Intermediate), reverse(Intermediate, OutList).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment