In BSL: 

;; -----------------------------------------------------------------------------
;; [Listof X] -> [Listof X]
;; remove duplicates ...

;; -----------------------------------------------------------------------------
;; ... keeping the copy on the left 
(require racket/base) ;; to import remove*, a variant of which should be in *SL

(check-expect (unique-l (list 1 2 1 3 1 2)) (list 1 2 3))

(define (unique-l l)
  (cond
    [(empty? l) '()]
    [else (cons (first l) (remove* (list (first l)) (unique-l (rest l))))]))

;; -----------------------------------------------------------------------------
;; ... keeping the copy on the right
(check-expect (unique-r (list 1 2 1 3 1 2)) (list 3 1 2))

(define (unique-r l)
  (cond
    [(empty? l) '()]
    [else (if (member? (first l) (rest l))
              (unique-r (rest l))
              (cons (first l) (unique-r (rest l))))]))



On Jun 3, 2015, at 1:25 AM, Paul Bian <paulb...@gmail.com> wrote:

> Hi all,
> 
> Trying to learn a bit about recursion in racket.
> The question is to remove duplicates from a list while maintaining the order 
> of the list.
> 
> 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
> 
> I've gotten the code to work by writing some helper functions, but my 
> question is, is it possible to solve these problems WITHOUT:
> 
> - local definition (i.e. beginner student language in dr racket)
> - loops
> - built in 'remove', 'reverse', 'member?'
> - writing your own 'remove', 'reverse', 'member?' functions
> 
> OR if 'member?' is allowed... I can figure out the right side case without 
> additional functions, but not for the left case, is there a way to do this 
> without the remove duplicates function that i wrote?
> 
> ---
> Code:
> 
> http://pastebin.com/h0dBdUaR
> 
> (define (RemoveElement element lst)
> ;if unique-left is applied to (list 1 4 2 1 5 4), the result would be (list 1 
> 4 2 5)
>  (cond
>    [(empty? lst) empty]
>    [(= element (first lst)) (RemoveElement element (rest lst))]
>    [else (cons (first lst) (RemoveElement element (rest lst)))]))
> 
> (define (unique-left lst)
>  (cond
>    [(empty? lst) empty]
>    [(member? (first lst) (rest lst)) (cons (first lst) (unique-left 
> (RemoveElement (first lst) (rest lst))))] 
>    [else (cons (first lst) (unique-left (rest lst)))]
>    )
>  )
> 
> (define (unique-right lst)
> ;the result of applying it to (list 1 4 2 1 5 4) would be (list 2 1 5 4)
>  (cond
>    [(empty? lst) empty]
>    [(not (member? (first lst) (rest lst))) (cons (first lst) (unique-right 
> (rest lst)))]
>    [else (unique-right (rest lst))]))
> 
> -- 
> 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.

-- 
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