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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nodouble and max

Status
Not open for further replies.

Terry007

Programmer
Joined
Jun 9, 2010
Messages
2
hello I have two easy examples, but I'm greenhorn in prolog.

nodouble (x, list1, list2) - predicate delete duplicate values in List 1 and save them to List2

max (list, x) - predicate find max value from List and save it to X.

I find max from list as:
maxList([]).
maxList([E],E).
maxList([E,E2|T],L) :- maxList([E2|T],L1), max(L1,E,L).
but how to save it to x?

and for nodouble:
Dupli([],[]).
Dupli([X|Xs],[X,X|Ys]) :- dupli(Xs,Ys).
but it is not what I need..
 
Now I need only one.. In second example - how to save duplicate values into List 2???
 
maxList([E], E) :- !.
maxList([E|T], R) :-
maxList(T, X),
(E > X, !, R is E; R is X).
 
% i don't understand why noDouble takes 3 arguments:
% noDouble(X, List1, List2) ????

noDouble([], []).
noDouble([E|T], R) :-
noDouble(T, R1),
(member(E, R1), !, R = R1; R = [E|R1]).
 
nodouble (x, list1, list2) - predicate delete duplicate values in List 1 and save them to List2
I suppose that it would be useful to have the list without duplicates ! nodouble(L, L1, L2) !
 
kahleen > your code doesn't save duplicates.
 
my bad ... i read the specification superficially and the name nodouble also misled me :) ... this is exactly why i didn't see the point of it having 3 parameters :)

the simplest idea to solve that is this:

noDouble(L, L1, L2) :-
reverse(L, LR),
noDouble(LR, [], L1, [], L2).

noDouble([], L1, L1, L2, L2).
noDouble([E|T], L1P, L1, L2P, L2) :-
member(E, T), !,
(
member(E, L2P), !,
noDouble(T, L1P, L1, L2P, L2);
noDouble(T, L1P, L1, [E|L2P], L2)
).
noDouble([E|T], L1P, L1, L2P, L2) :-
member(E, L2P), !,
noDouble(T, L1P, L1, L2P, L2);
noDouble(T, [E|L1P], L1, L2P, L2).

joel, I didn't make heavy use of these forums, how do you make some text to represent code? are there some <code> code </code> marks available to use?
 
i meant, the simplest idea that i had :)
 
are there some <code> code </code> marks
Yes, options are explained when you click on Preview Post
For code it's between [ code] [ /code] (I must put a space between '[' and 'c' of code !!)
Code:
 my code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top