Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can anyone give me some tips on how

Status
Not open for further replies.

nquarishi

Programmer
Dec 3, 2003
1
0
0
US
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
 
You probably want to start with some simpler predicates you can use in constructing the more complex ones, such as:

/ member(X,Ys) returns true if X is a member of Ys

member(X,[X|_Xs]).
member(X,[_Y|Ys]):-member(X,Ys).

/ select(X,Ys,Zs) returns true if Zs is Ys with the first occurence of X removed (X must appear in Ys)

select(X,[X|Ys],Ys).
select(X,[Y|Ys],[Y|Zs]):-
X /== Y, select(X,Ys,Zs).

/ select_all(X,Ys,Zs) returns true if Zs is Ys with all occurences of X removed (X does not have to appear in Ys)

select_all(X,Ys,Zs):-
select(X,Ys,Ws),
select_all(X,Ws,Zs).
select_all(X,Ys,Ys):-
not member(X,Ys).

so now remove:

remove(Xs,[],Xs).
remove(Xs,[Y|Ys],Zs):-
select_all(Y,Xs,Ws),
remove(Ws,Ys,Zs).

2) even_length is fairly simple:

even_length([]).
even_length([_X,_Y|Xs]:-
even_length(Xs).

3)

matches(Xs,Ys):-
match(Xs,Ys),
match(Ys,Xs).
match([],_).
match([X|Xs],Ys):-
member(X,Ys),
match(Xs,Zs).

4) uses select_all

remove_duplicates([],[]).
remove_duplicates([X|Xs],[X|Ys]):-
select_all(X,Xs,Zs),
remove_duplicates(Zs,Ys).

5) uses member

members_in_common([],Ys,[]).
members_in_common([X|Xs],Ys,[X|Zs]):-
member(X,Ys),
members_in_common(Xs,Ys,Zs).
members_in_common([X|Xs],Ys,Zs):-
not member(X,Ys),
members_in_common(Xs,Ys,Zs).

* Note, these haven't been tested or debugged and are in no way the most efficient implementations of the above. In fact, they may even be wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top