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

need help with an argument

Status
Not open for further replies.

mental00

Programmer
Jun 22, 2010
2
GR
Hello all, I need an argument correct(L, X, NL) where L is a list of names, X is a name that exists twice in L and NL is the new list with the name X once.

I wrote this:

correct([], _, _).
correct([H|T], X, NL):-
NL= [H|NL1],
not(member(X, NL)),
correct(T, X, NL1).

correct([H|T], X, NL):-
member(X, [H|NL]),
correct(T, X, NL).

but it doesnt work, any ideas?

thanks in advance.
 
First of all, the first clause is incorrect:

Code:
correct([], _, _)

On an empty list, no matter what the name is, the result is the empty list, not _

Then:
Code:
correct([H|T], X, NL):-
  NL= [H|NL1],
  not(member(X, NL)),
  correct(T, X, NL1).

NL is to be computed. You can't test if X is member in NL while NL is still unbound

A solution to your problem would be:

Code:
% empty list
correct([], _, []).

correct([H | T], X, R) :-
    ... what to do if H = X and member(H, T)

correct([H | T], X, [H | R]) :-
    ... what to do if H = X and not(member(H, T))

correct([H | T], X, [H | R]) :-
    ... what to do if H \= X

How about you work on the solution a little and maybe we'll help later on, if you still don't succeed?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top