79.4k views
5 votes
Define a function in Scheme (or relation in Prolog) that checks whether a set of elements (represented as a list) is a subset of another set (represented as a list).

User Cactusbone
by
5.1k points

1 Answer

5 votes

Answer:

subset([],[]).

subset([X|L],[X|S]) :-

subset(L,S).

subset(L, [_|S]) :-

subset(L,S).

Success:

subset([1,3], [1,2,3]).

subset(X, [1,3,4]). % error handling to compare sets in a given order

Fail:

subset([2,1], [1,2,3]). % compares in a different order from the first.

Step-by-step explanation:

The function "Subset" in the source code above accepts two sets, then checks if the first set is a subset of the second. The code returns true if the condition is met.

User Yuby
by
4.6k points