Removing duplicates in a list in Dr. Racket

89 views
Skip to first unread message

JJ C

unread,
Jul 20, 2020, 12:07:43 PM7/20/20
to Racket Users

In Beginning Student with List Abbreviations

I am struggling to come up with functions 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

What are the functions for removing duplicates from each left and right side? If you need to, use helper functions and append, cond, cons, equal?, etc but not using reverse or any built-in functions.


For example, with unique-left I came up with the below code but it only removes any consecutive duplicates 

e. g. (list 2 3 4 4 5 3) -> (list 2 3 4 5 3)


;(define (unique-left lst)

 ; (cond

  ; [(empty? lst) 0]

   ;[(empty? (rest lst)) lst]

   ;[else

    ;  (if (equal? (first lst) (first (rest lst)))

     ;     (unique-left (rest lst))

      ;    (cons (first lst) (unique-left (rest lst))))]))


So the code does not get me the unique-left like I want to show as the top example. 

Reply all
Reply to author
Forward
0 new messages