I see that you asked regarding this in two other posts:
https://groups.google.com/g/racket-users/c/TydSKVT5cqo (On which I provided
a function for removing duplicates from the right)
https://groups.google.com/g/racket-users/c/GMrf063HPwg
You just have to follow the design recipe for beginning student languages
and you will get these for removing duplicates from the left:
; List<Any> -> List<Any>
; returns a list which has only the left most occurrences of the elements
of lst
(check-expect (unique-left '()) '())
(check-expect (unique-left (list "a")) (list "a"))
(check-expect (unique-left (list "a" "b" "a")) (list "a" "b"))
(check-expect (unique-left (list "a" "b" "a" "c" "d" "c")) (list "a" "b"
"c" "d"))
(define (unique-left lst)
(cond
[(empty? lst) '()]
[else (if (mymember? (first lst) (unique-left (rest lst)))
(cons (first lst) (remove-member (first lst) (unique-left
(rest lst))))
(cons (first lst) (unique-left (rest lst))))]))
; remove-member
; Any List<Any> -> List<Any>
; remove first occurrence of elt in lst
(check-expect (remove-member "a" '()) '())
(check-expect (remove-member "a" (list "a")) '())
(check-expect (remove-member "a" (list "a" "b" "a")) (list "b" "a"))
(check-expect (remove-member "c" (list "a" "b" "a")) (list "a" "b" "a"))
(define (remove-member elt lst)
(cond
[(empty? lst) '()]
[else (if (equal? (first lst) elt)
(rest lst)
(cons (first lst) (remove-member elt (rest lst))))]))
If it is allowed to design your own reverse function then this will also
work:
(define (unique-left lst)
(reverse (unique-right (reverse lst))))
On Tuesday, July 21, 2020 at 12:04:45 AM UTC+8, JJ C wrote:
>
> 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.
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/269dcb18-8cdf-4b79-a83d-30c8eb9f7533o%40googlegroups.com.