Le 03/06/2015 07:25, Paul Bian a écrit :
Hi all,

one function to remove duplicates from the left,

i.e. 1 2 1 3 2 4 5 -> 1 2 3 4 5

and one from the right.

i.e. 1 2 1 3 2 4 5 -> 1 3 2 4 5

Hello,

For the first function, I think you can't do it without a local define or by adding a second parameter (for result). Here is my solution :

(define (right L)
  (cond ((empty? L) (list))
        ((member? (car L) (cdr L)) (right (cdr L)))
        (else (cons (car L) (right (cdr L))))))

(define (left L res)
  (cond ((empty? L) res)
((not (member? (car L) res)) (left (cdr L) (append res (list(car L)))))
        (else (left (cdr L) res))))


For left, I use an empty list at initialisation (res), and I can check if an element have already been added. I didn't use reverse but it is recommanded instead of append cause it cost less of computing time.

append = reverse = O(n) . But i use append more than one time.

In practise I never use remove, cause it cost a lot of computing time too O(pos) (cause have to reindex each element of the list each time), except if you remove the first element, but (cdr) does it ;) .


--
You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to