How to get rid of duplicates in a list, but keep the order

前端 未结 8 1299
情话喂你
情话喂你 2021-02-15 15:13

I am using Intermediate Student with Lambda in DrRacket, I was wondering how one would remove the duplicates in a list, while keeping the order. For example (remo

8条回答
  •  旧时难觅i
    2021-02-15 16:07

    hmm i just had a racket exam recently, :/

    the 'standard' remove-duplicates works fine but i was using pretty-big in drRacket so it had to be loaded using (require racket/list)

    here is an alternative way :)

    using mutation (not really in the spirit of racket but.. it works.)

        (define (set l)
            (define the-set '())
                (begin (for-each
                           (lambda (x)
                                (if (member x the-set)
                                    #t
                                (set! the-set (cons x the-set))))
                           l)
                       (reverse the-set)))
    

    hope this helps... cheers!

提交回复
热议问题