dragonwasrobot / learn-prolog-now-exercises

My solutions to the exercises and practical sessions of the book 'Learn Prolog Now!' by Patrick Blackburn, Johan Bos, and Kristina Striegnitz.
288 stars 81 forks source link

Chapter 11 Subset check fix #48

Open nicoabie opened 5 years ago

nicoabie commented 5 years ago

Hi, The solution provided will asset that subset([a,b,c,a], [a, b, c]). is true. It is missing checking that the first parameter is a set itself. Here I propose a fix using the set definition implemented in a previous chapter.

set([], []).
set([H|T], X):- member(H, T), !, set(T, X).
set([H|T], [H|X]):- set(T, X).

subset_check(SS, S):-
  set(SS, SS),
  all_members(SS, S).

all_members([], _).
all_members([H|T], S):-
  member(H, S),
  !,
  all_members(T, S)

Thanks a lot for making this repository.