Can anyone give me some tips on how to write the following prolog functions? I am new to it, and have very little clue.
===========================================================
1) remove/3. Removes from the list given by the first parameter, all elements appearing in the list given by the second parameter, and returns the result in the third parameter.
Examples:
31 ?- remove([a, b, c, d, e, f], [a, d], X).
X = [b, c, e, f] ;
No
32 ?- remove([a, b, c, d, e, f], [], X).
X = [a, b, c, d, e, f] ;
No
33 ?- remove([a, b, c, d, e, f, a, a, d, e], [a, e], X).
X = [b, c, d, f, d] ;
No
2) even_length/1. Succeeds (true) if the list given by the parameter has even number of elements. Do not use arithmetic.
Examples:
34 ?- even_length([a, b, c]).
No
35 ?- even_length([a, b, c, c]).
Yes
36 ?- even_length([]).
Yes
3) matches/2. Succeeds(true) if the two parameters, both lists, contain the same elements but not necessarily in the same order.
Examples:
37 ?- matches([], []).
Yes
38 ?- matches([a, b, c, a, b], [b, a, c, a, b]).
Yes
39 ?- matches([a, b, c, a, b], [b, a, c]).
Yes
53 ?- matches([a, b, c], [a, b, p, q]).
No
4) remove_duplicates/2. Removes from the list given in first parameter, any duplicate appearances of elements, and returns the result in the second parameter.
Examples:
43 ?- remove_duplicates([a, b, a, c, d, c, c, d, c, e, a], X).
X = [b, d, c, e, a]
Yes
44 ?- remove_duplicates([a, b, c], X).
X = [a, b, c]
Yes
45 ?- remove_duplicates([], X).
X = []
Yes
5) members_in_common/3. Given two lists through the first two parameters, it returns the list of common elements in the third parameter.
Examples:
46 ?- members_in_common([a, b, c, d, a, b, a], [a, d, f, f, c, c, f], X).
X = [a, c, d, a, a]
Yes
47 ?- members_in_common([a, b, c, d, a, b, a], [p, q, r, s], X).
X = []
Yes
48 ?- members_in_common([a, b, c, d, a, b, a], [], X).
X = []
Yes
===========================================================
1) remove/3. Removes from the list given by the first parameter, all elements appearing in the list given by the second parameter, and returns the result in the third parameter.
Examples:
31 ?- remove([a, b, c, d, e, f], [a, d], X).
X = [b, c, e, f] ;
No
32 ?- remove([a, b, c, d, e, f], [], X).
X = [a, b, c, d, e, f] ;
No
33 ?- remove([a, b, c, d, e, f, a, a, d, e], [a, e], X).
X = [b, c, d, f, d] ;
No
2) even_length/1. Succeeds (true) if the list given by the parameter has even number of elements. Do not use arithmetic.
Examples:
34 ?- even_length([a, b, c]).
No
35 ?- even_length([a, b, c, c]).
Yes
36 ?- even_length([]).
Yes
3) matches/2. Succeeds(true) if the two parameters, both lists, contain the same elements but not necessarily in the same order.
Examples:
37 ?- matches([], []).
Yes
38 ?- matches([a, b, c, a, b], [b, a, c, a, b]).
Yes
39 ?- matches([a, b, c, a, b], [b, a, c]).
Yes
53 ?- matches([a, b, c], [a, b, p, q]).
No
4) remove_duplicates/2. Removes from the list given in first parameter, any duplicate appearances of elements, and returns the result in the second parameter.
Examples:
43 ?- remove_duplicates([a, b, a, c, d, c, c, d, c, e, a], X).
X = [b, d, c, e, a]
Yes
44 ?- remove_duplicates([a, b, c], X).
X = [a, b, c]
Yes
45 ?- remove_duplicates([], X).
X = []
Yes
5) members_in_common/3. Given two lists through the first two parameters, it returns the list of common elements in the third parameter.
Examples:
46 ?- members_in_common([a, b, c, d, a, b, a], [a, d, f, f, c, c, f], X).
X = [a, c, d, a, a]
Yes
47 ?- members_in_common([a, b, c, d, a, b, a], [p, q, r, s], X).
X = []
Yes
48 ?- members_in_common([a, b, c, d, a, b, a], [], X).
X = []
Yes