Created
December 23, 2018 14:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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