Home > Archive > Prolog > December 2006 > delete
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Ricardo Nuno 2006-12-21, 7:04 pm |
| I need some help with this. delete from a list a group of elements
example:
deleteList([1,2,3,4],[3,4],L).
Ricardo
| |
| bart demoen 2006-12-21, 7:04 pm |
| On Thu, 21 Dec 2006 10:05:35 -0800, Ricardo Nuno wrote:
> I need some help with this. delete from a list a group of elements
> example:
>
> deleteList([1,2,3,4],[3,4],L).
>
>
> Ricardo
Generalizing a bit conservatively from your example:
deleteList(List1,List2,_) :- append(_,List2,List1).
Depending on what you meant by "a group of elements", this
might be really close to what you need for finishing your
homework !
Cheers
Bart Demoen
| |
|
| Ricardo Nuno wrote:
> I need some help with this. delete from a list a group of elements
> example:
>
> deleteList([1,2,3,4],[3,4],L).
>
>
> Ricardo
Hi Ricardo,
I assume that the second list doesn't need to be a sublist of the first
one but just a set of terms, which are being filtered out. That being
the case, here's a possible solution:
:- use_module(library(lists),[member/2]).
deleteList([],_,[]).
deleteList([X|Xs],Ys,L) :- member(X,Ys), !, deleteList(Xs,Ys,L).
deleteList([X|Xs],Ys,[X|L]) :- deleteList(Xs,Ys,L).
-- Martin
| |
|
| On 21 Dec 2006 12:30:18 -0800, "void" <martinlaz@gmail.com> wrote:
>Ricardo Nuno wrote:
>
>Hi Ricardo,
>
>I assume that the second list doesn't need to be a sublist of the first
>one but just a set of terms, which are being filtered out. That being
>the case, here's a possible solution:
>
>:- use_module(library(lists),[member/2]).
>
>deleteList([],_,[]).
>deleteList([X|Xs],Ys,L) :- member(X,Ys), !, deleteList(Xs,Ys,L).
>deleteList([X|Xs],Ys,[X|L]) :- deleteList(Xs,Ys,L).
>
Congratulate you for joining the group of volunteers doing other
people's homeworks. With more people like you the number of ...
ehem... experts will substantially increase!
A.L>
| |
|
| A.L. wrote:
> Congratulate you for joining the group of volunteers doing other
> people's homeworks. With more people like you the number of ...
> ehem... experts will substantially increase!
>
> A.L>
Well, isn't that much better than having a dead newsgroup? Besides, I
don't really mind doing other people's homeworks as long I find them
interesting enough and provided I've got the time. Programming in
Prolog is always fun, right?
-- Martin
| |
| student 2006-12-21, 7:04 pm |
| Ricardo Nuno wrote:
> I need some help with this. delete from a list a group of elements
> example:
>
> deleteList([1,2,3,4],[3,4],L).
>
>
That's it?
What is the resulting value of L supposed to be?
For example, if the resulting value of L was supposed to be
L = [1,2]
one might be tempted to assume that a full specification of "the given
problem" was
deleteList(GivenList,MembersToBeDeleted,
ResultingList)
if and only if
every member of ResultingList is a member of GivenList &
no member of MembersToBeDeleted is a member of ResultingList.
in which case an instance of the relation deleteList could be
deleteList([q,w,e,r,t,y],[t,w,a,d,d,l,e]
,[y,r,q]).
but I am only guessing.
Generally speaking, I find it helpful to have a complete
if-and-only-if definition of the predicate I intend to implement,
independent of Prolog, before I try to implement it "in" Prolog.
--
billh
| |
| student 2006-12-22, 4:08 am |
| student wrote:
> Ricardo Nuno wrote:
>
> That's it?
>
> What is the resulting value of L supposed to be?
>
> For example, if the resulting value of L was supposed to be
>
> L = [1,2]
>
> one might be tempted to assume that a full specification of "the given
> problem" was
>
> deleteList(GivenList,MembersToBeDeleted,
ResultingList)
>
> if and only if
>
> every member of ResultingList is a member of GivenList &
> no member of MembersToBeDeleted is a member of ResultingList.
>
> in which case an instance of the relation deleteList could be
>
> deleteList([q,w,e,r,t,y],[t,w,a,d,d,l,e]
,[y,r,q]).
>
As would
deleteList([q,w,e,r,t,y],[t,w,a,d,d,l,e]
,[]).
I should have said
deleteList(GivenList,MembersToBeDeleted,
ResultingList)
if and only if
every member of GivenList
is a member of MembersToBeDeleted
or
is a member of ResultingList &
every member of ResultingList is a member of GivenList &
no member of MembersToBeDeleted is a member of ResultingList.
--
|
|
|
|
|