From: Alexandre Almeida
Subject: Detele repeated in a list
Date: 
Message-ID: <3b9836f1-0393-491a-8b4c-e76198b16c6c@l64g2000hse.googlegroups.com>
Hi everyone!

I have this piece of code, that sould delete all the repeated elements
from a list :

(defun apaga-repetidos (lista)
  (let ((p (first lista))
        (r (rest lista)))
    (if (null lista)
        nil
    (if (equal p r)
        (apaga-repetidos r)
      (cons p (apaga-repetidos r))))))

> : (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4) (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4) ))


But i cant´t put this to work. Someone can help me?

Best regards,
Alexandre Almeida

From: Mariano Montone
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <e72bbe00-c041-42c5-857b-7b142e7a1d3d@z66g2000hsc.googlegroups.com>
On 10 jun, 10:31, Alexandre Almeida <·········@gmail.com> wrote:
> Hi everyone!
>
> I have this piece of code, that sould delete all the repeated elements
> from a list :
>
> (defun apaga-repetidos (lista)
>   (let ((p (first lista))
>         (r (rest lista)))
>     (if (null lista)
>         nil
>     (if (equal p r)
>         (apaga-repetidos r)
>       (cons p (apaga-repetidos r))))))
>

You are comparing an element with a list when you do (equal p r)

Try this:

(defun apaga-repetidos (lista)
   (apaga-repetidos-2 lista (make-hash-table :test #'equal)))

(defun apaga-repetidos-2 (lista vistos)
   (if (null lista)
       nil
       (let ((p (first lista))
	     (r (rest lista)))
	 (if (gethash p vistos)
	     (apaga-repetidos-2 r vistos)
	     (progn
	       (setf (gethash p vistos) t)
	       (cons p (apaga-repetidos-2 r vistos)))))))

> > : (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4) (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4) ))
>

Result: ((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4)
(7 4) (6 4) (5 4)
 (4 4) (4 5) (3 5) (3 6) (4 6))

Cheers, Mariano
From: Alexandre Almeida
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <6eaa3d95-b8f2-41dd-bd35-49445260717c@m73g2000hsh.googlegroups.com>
On Jun 10, 2:50 pm, Mariano Montone <··············@gmail.com> wrote:
> On 10 jun, 10:31, Alexandre Almeida <·········@gmail.com> wrote:
>
> > Hi everyone!
>
> > I have this piece of code, that sould delete all the repeated elements
> > from a list :
>
> > (defun apaga-repetidos (lista)
> >   (let ((p (first lista))
> >         (r (rest lista)))
> >     (if (null lista)
> >         nil
> >     (if (equal p r)
> >         (apaga-repetidos r)
> >       (cons p (apaga-repetidos r))))))
>
> You are comparing an element with a list when you do (equal p r)
>
> Try this:
>
> (defun apaga-repetidos (lista)
>    (apaga-repetidos-2 lista (make-hash-table :test #'equal)))
>
> (defun apaga-repetidos-2 (lista vistos)
>    (if (null lista)
>        nil
>        (let ((p (first lista))
>              (r (rest lista)))
>          (if (gethash p vistos)
>              (apaga-repetidos-2 r vistos)
>              (progn
>                (setf (gethash p vistos) t)
>                (cons p (apaga-repetidos-2 r vistos)))))))
>
> > > : (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4) (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4) ))
>
> Result: ((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4)
> (7 4) (6 4) (5 4)
>  (4 4) (4 5) (3 5) (3 6) (4 6))
>
> Cheers, Mariano

Thanks for the reply...

But i want a way that don't involve things like this: (make-hash-
table :test #'equal)))

Regards,
Alexandre
From: Tayssir John Gabbour
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <5e11a334-d13c-4533-8f28-a3d14dec414f@56g2000hsm.googlegroups.com>
On Jun 10, 3:31 pm, Alexandre Almeida <·········@gmail.com> wrote:
> I have this piece of code, that sould delete all the repeated
> elements from a list :

First of all, are you aware of REMOVE-DUPLICATES?

CL-USER> (remove-duplicates '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4
2) (3 2))
                            :test #'equal)
((1 2) (2 2) (5 2) (6 2) (4 2) (3 2))


All best,
Tayssir
From: Alexandre Almeida
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <8487773a-2d73-475e-859d-10de23b12090@c58g2000hsc.googlegroups.com>
On Jun 10, 3:30 pm, Tayssir John Gabbour <············@googlemail.com>
wrote:
> On Jun 10, 3:31 pm, Alexandre Almeida <·········@gmail.com> wrote:
>
> > I have this piece of code, that sould delete all the repeated
> > elements from a list :
>
> First of all, are you aware of REMOVE-DUPLICATES?
>
> CL-USER> (remove-duplicates '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4
> 2) (3 2))
>                             :test #'equal)
> ((1 2) (2 2) (5 2) (6 2) (4 2) (3 2))
>
> All best,
> Tayssir

Yes, I have... But i need to build that function, for a school work :(

Regards,
Alexandre
From: danb
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <4c114079-fb4b-49fe-9aaf-75c170dc02fd@59g2000hsb.googlegroups.com>
On Jun 10, 9:36 am, Alexandre Almeida <·········@gmail.com> wrote:
> i need to build that function, for a school work :(

So you're cheating?

--Dan

------------------------------------------------
http://www.prairienet.org/~dsb/

cl-match:  expressive pattern matching in Lisp
http://common-lisp.net/project/cl-match/
From: Alexandre Almeida
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <d0e23e2e-c8ef-4104-be99-0d00040796e8@m44g2000hsc.googlegroups.com>
On Jun 10, 3:47 pm, danb <·········@gmail.com> wrote:
> On Jun 10, 9:36 am, Alexandre Almeida <·········@gmail.com> wrote:
>
> > i need to build that function, for a school work :(
>
> So you're cheating?
>
> --Dan
>
> ------------------------------------------------http://www.prairienet.org/~dsb/
>
> cl-match:  expressive pattern matching in Lisphttp://common-lisp.net/project/cl-match/

No I'm not cheating... If i'm cheating i have post all of my work
here!!

But i'm going to use the remove-duplicates...

Tanks all for the help!!

Regards,
Alexandre
From: Pascal J. Bourguignon
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <87hcc19llo.fsf@hubble.informatimago.com>
Alexandre Almeida <·········@gmail.com> writes:

> On Jun 10, 3:47 pm, danb <·········@gmail.com> wrote:
>> On Jun 10, 9:36 am, Alexandre Almeida <·········@gmail.com> wrote:
>>
>> > i need to build that function, for a school work :(
>>
>> So you're cheating?
>
> No I'm not cheating... If i'm cheating i have post all of my work
> here!!
>
> But i'm going to use the remove-duplicates...

Well if the purpose of your exercise is to implement
remove-duplicates, it won't do to use cl:remove-duplicates.  This
would be cheating.


First, while I understand English is not your native tongue, you must
be clearer on the concepts and terminology.

When you say that you want to remove duplicate entries, it's not the
same thing than when you say that you want to remove the repeated
elements.

In the sequence:

    (1 2 3 2 3 2)


the repeated elements are 2 and 3.  When we remove them, we're left with:

    (1)

But the duplicate elements are the 2, 3 and 2 that follow 1 2 3.  When
you remove those duplicates, you're left with:

    (1 2 3)

If you can express what you want more precisely, you should be able to
come with a formal definition of your function, and then translating
this formal definition in lisp should be trivial.


And what reason would there be to refuse a solution using hash tables?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"What is this talk of "release"?  Klingons do not make software
"releases".  Our software "escapes" leaving a bloody trail of
designers and quality assurance people in its wake."
From: Pascal Costanza
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <6b7obmF3ahqnqU1@mid.individual.net>
Alexandre Almeida wrote:
> On Jun 10, 3:30 pm, Tayssir John Gabbour <············@googlemail.com>
> wrote:
>> On Jun 10, 3:31 pm, Alexandre Almeida <·········@gmail.com> wrote:
>>
>>> I have this piece of code, that sould delete all the repeated
>>> elements from a list :
>> First of all, are you aware of REMOVE-DUPLICATES?
>>
>> CL-USER> (remove-duplicates '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4
>> 2) (3 2))
>>                             :test #'equal)
>> ((1 2) (2 2) (5 2) (6 2) (4 2) (3 2))
>>
>> All best,
>> Tayssir
> 
> Yes, I have... But i need to build that function, for a school work :(

(defun rem-duplicates (list)
   (loop for (first . rest) on (append list list)
         unless (member first (reverse rest) :test #'equal)
         collect first))


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: danb
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <0300753e-811e-4aa9-ac29-d1299fa084d3@x41g2000hsb.googlegroups.com>
On Jun 10, 11:27 am, Pascal Costanza <····@p-cos.net> wrote:
> ... (append list list) ... (reverse rest) ...

What's that supposed to do?  If you're trying to
order elements by first occurance:

(defun rem-dupes (orig &key (test #'eql))
  (let* ((list (copy-list orig))
         (cell list))
    (loop
     (unless cell (return list))
     (setf (cdr cell) (delete (car cell) (cdr cell) :test test)
           cell (cdr cell)))))

--Dan

------------------------------------------------
http://www.prairienet.org/~dsb/

cl-match:  expressive pattern matching in Lisp
http://common-lisp.net/project/cl-match/
From: Pascal Costanza
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <6b82s9F3bgtlaU1@mid.individual.net>
danb wrote:
> On Jun 10, 11:27 am, Pascal Costanza <····@p-cos.net> wrote:
>> ... (append list list) ... (reverse rest) ...
> 
> What's that supposed to do? 

Adding some overhead to make things more inefficient. ;)

Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Thomas A. Russ
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <ymilk1d6q29.fsf@blackcat.isi.edu>
Pascal Costanza <··@p-cos.net> writes:

> Alexandre Almeida wrote:
> > Yes, I have... But i need to build that function, for a school work :(
> 
> (defun rem-duplicates (list)
>    (loop for (first . rest) on (append list list)
>          unless (member first (reverse rest) :test #'equal)
>          collect first))

That's slightly evil.
<insert maniacal laughter>

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Tayssir John Gabbour
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <7d4dd955-9e8b-4834-9090-8bcdac323a5c@y21g2000hsf.googlegroups.com>
On Jun 10, 6:27 pm, Pascal Costanza <····@p-cos.net> wrote:
> > Yes, I have... But i need to build that function, for a school work :(
>
> (defun rem-duplicates (list)
>    (loop for (first . rest) on (append list list)
>          unless (member first (reverse rest) :test #'equal)
>          collect first))

Isn't it terrible to crap all over a nice short LOOP solution?

;)

Tayssir
From: Raffael Cavallaro
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <2008061010123275249-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2008-06-10 09:31:16 -0400, Alexandre Almeida <·········@gmail.com> said:

Assuming that your data will always take the form of a list of two 
element lists:

> (defun apaga-repetidos (lista)
>   (let ((p (first lista))
>         (r (rest lista)))
>     (if (null lista)
>         nil
>     (if (equal p r)

^^^^^^^^^^^^^^^^^^^^^
are these really what you want to compare here?
Don't you really want to compare (first p) and (second p)?

>         (apaga-repetidos r)
>       (cons p (apaga-repetidos r))))))
From: Alexandre Almeida
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <3d87743d-1a9f-460e-a0b0-2783c2e773d4@k30g2000hse.googlegroups.com>
On Jun 10, 3:12 pm, Raffael Cavallaro <················@pas-d'espam-
s'il-vous-plait-mac.com> wrote:
> On 2008-06-10 09:31:16 -0400, Alexandre Almeida <·········@gmail.com> said:
>
> Assuming that your data will always take the form of a list of two
> element lists:
>
> > (defun apaga-repetidos (lista)
> >   (let ((p (first lista))
> >         (r (rest lista)))
> >     (if (null lista)
> >         nil
> >     (if (equal p r)
>
> ^^^^^^^^^^^^^^^^^^^^^
> are these really what you want to compare here?

I want to compare a list that is always like this:

'((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4 2) (3 2))

And i want to remove the repeated elements

> Don't you really want to compare (first p) and (second p)?
>
> >         (apaga-repetidos r)
> >       (cons p (apaga-repetidos r))))))
From: Alexandre Almeida
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <a7a10004-66b1-40f2-85b0-8688f15f4292@34g2000hsf.googlegroups.com>
On Jun 10, 3:19 pm, Alexandre Almeida <·········@gmail.com> wrote:
> On Jun 10, 3:12 pm, Raffael Cavallaro <················@pas-d'espam-
>
> s'il-vous-plait-mac.com> wrote:
> > On 2008-06-10 09:31:16 -0400, Alexandre Almeida <·········@gmail.com> said:
>
> > Assuming that your data will always take the form of a list of two
> > element lists:
>
> > > (defun apaga-repetidos (lista)
> > >   (let ((p (first lista))
> > >         (r (rest lista)))
> > >     (if (null lista)
> > >         nil
> > >     (if (equal p r)
>
> > ^^^^^^^^^^^^^^^^^^^^^
> > are these really what you want to compare here?
>
> I want to compare a list that is always like this:
>
> '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4 2) (3 2))
>
> And i want to remove the repeated elements
>
> > Don't you really want to compare (first p) and (second p)?
>
> > >         (apaga-repetidos r)
> > >       (cons p (apaga-repetidos r))))))

Sorry...

Big mess my reply...

I have this list --> '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4 2) (3
2))
and i want to remove duplicate entries.

I have this function:

(defun iguais(x y)
    (if (and (equal (car x) (car y)) (equal (cadr x) (cadr y)))
        t
      nil
      )
    )

That give T if x and y are equals (x = '(1 2), y = '(1 2) --> t)

But i'm with difficult to build a function that remove the repeated
elements!!

I have something like this:

(defun apaga-repetidos-2 (lista x)
   (if (null lista)
       nil
       (let ((p (first lista))
             (r (rest lista)))
         (if (iguais p (car r))
             (apaga-repetidos-2 r x)
               (cons p (apaga-repetidos-2 lista x))))))

But don't work!!

Best regards,
Alexandre Almeida
From: Tayssir John Gabbour
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <56bc1cec-5cde-44bd-b7db-24d1d5a9f0e0@e53g2000hsa.googlegroups.com>
On Jun 10, 4:29 pm, Alexandre Almeida <·········@gmail.com> wrote:
> I have something like this:
>
> (defun apaga-repetidos-2 (lista x)
>    (if (null lista)
>        nil
>        (let ((p (first lista))
>              (r (rest lista)))
>          (if (iguais p (car r))
>              (apaga-repetidos-2 r x)
>                (cons p (apaga-repetidos-2 lista x))))))

You're close.

For one thing, let's get rid of X:

(defun apaga-repetidos (lista)
  (if (null lista)
      nil
      (let ((p (first lista))
            (r (rest lista)))
        (if (equal p (car r))
            (apaga-repetidos r)
            (cons p (apaga-repetidos list))))))


Now note that there's a small error. On the very last line, you call
    (apaga-repetidos list)
which will infinitely recurse.

Also,
    (equal p (car r))
will only work if all the duplicates are next to each other. Consider
using the MEMBER function, giving the right :TEST argument.


(BTW, I don't consider it "cheating," nor is it my place to enforce
absurd school policy even if it were. ;) If you want, I can send the
solution, but perhaps you'll prefer to figure it out...)


Tayssir
From: Raffael Cavallaro
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <2008061019331650073-raffaelcavallaro@pasdespamsilvousplaitmaccom>
On 2008-06-10 10:29:31 -0400, Alexandre Almeida <·········@gmail.com> said:

> Sorry...
> 
> Big mess my reply...
> 
> I have this list --> '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (4 2) (3
> 2))
> and i want to remove duplicate entries.

Ok, I thought when you said "repeated entries" you meant sublists like 
(2 2) or (3 3). I didn't think you meant remove-duplicates since, as 
others have pointed out, that's part of the standard language.
From: Thomas A. Russ
Subject: Re: Detele repeated in a list
Date: 
Message-ID: <ymiprqp6q5v.fsf@blackcat.isi.edu>
Alexandre Almeida <·········@gmail.com> writes:

> Hi everyone!
> 
> I have this piece of code, that sould delete all the repeated elements
> from a list :
> 
> (defun apaga-repetidos (lista)
>   (let ((p (first lista))
>         (r (rest lista)))
>     (if (null lista)
>         nil
>     (if (equal p r)
>         (apaga-repetidos r)
>       (cons p (apaga-repetidos r))))))
> 
> > : (apaga-repetidos '((1 2) (2 2) (3 2) (4 2) (5 2) (6 2) (7 2) (8 2) (8 3) (8 4) (7 4) (6 4) (5 4) (4 4) (4 5) (3 5) (3 6) (4 6) (4 5) (4 4) (5 4) ))
> 
> 
> But i cant��t put this to work. Someone can help me?

Well, there's always

  (remove-duplicates lista :test #'equal)

This typically isn't very smart and uses an N^2 algorithm, but it is the
simplest solution to your problem.


However, it will be a worthwhile learning experience to build your own.

-- 
Thomas A. Russ,  USC/Information Sciences Institute