Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:08:16+00:00 2026-06-10T00:08:16+00:00

So I’m teaching myself functional programming using Racket Scheme, and I love it so

  • 0

So I’m teaching myself functional programming using Racket Scheme, and I love it so far. As an exercise for myself, I’ve been trying to implement a few simple tasks in a purely functional way. I know immutability is an important part of functional style, but I was wondering if there are any times when it’s okay.

I thought of a fun way for a function to strip non-unique strings from a list of strings when used with filter, shown here:

(define (make-uniquer)
  (let ([uniques '()])
    (lambda (x)
      (if (not (member x uniques))
          (set! uniques (cons x uniques))
          #f))))

(define (uniquify x)
  (let ([uniquer (make-uniquer)])
    (filter uniquer x)))

As you can see, make-uniquer returns a closure over a list of strings to compare against for uniqueness, this way it can act as a simple predicate for filter. But I’m destructively updating the closed-over list. Is this bad form, or is it okay to change local closed-over variables in such ways?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:08:17+00:00Added an answer on June 10, 2026 at 12:08 am

    In this case, I’d avoid a mutable implementation because a functional implementation can compete pretty well in terms of performance. Here are three versions (including the built-in remove-duplicates) of the function:

    #lang racket
    
    (define (make-uniquer)
      (let ([uniques (make-hash)])
        (lambda (x)
          (if (not (hash-ref uniques x #f))
              (hash-set! uniques x #t)
              #f))))
    
    (define (uniquify x)
      (let ([uniquer (make-uniquer)])
        (filter uniquer x)))
    
    (define (uniquify-2 lst)
      (define-values (_ r)
       (for/fold ([found (hash)] [result '()])
                 ([elem (in-list lst)])
         (cond [(hash-ref found elem #f)
                (values found result)]
               [else (values (hash-set found elem #t)
                             (cons elem result))])))
      (reverse r))
    
    (define randoms (build-list 100000 (λ (n) (random 10))))
    (time (for ([i 100]) (uniquify randoms)))
    (time (for ([i 100]) (uniquify-2 randoms)))
    (time (for ([i 100]) (remove-duplicates randoms)))
    
    ;; sanity check
    (require rackunit)
    (check-equal? (uniquify randoms) (uniquify-2 randoms))
    (check-equal? (uniquify randoms) (remove-duplicates randoms))
    

    The times I get for this are

    cpu time: 1348 real time: 1351 gc time: 0
    cpu time: 1016 real time: 1019 gc time: 32
    cpu time: 760 real time: 760 gc time: 0
    

    Not scientific numbers, so take this with a grain of salt. To be fair, I did tune uniquify-2 a bit since my first version was slower. I also improved the mutable version with a hash table, but maybe there are other optimizations that could be applied. Also, the built-in remove-duplicates is tuned for performance and does use a mutable data structure (though it avoids set!).

    You may also be interested in the Guide entry on performance. It points out that use of set! can harm performance, so use it with care.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.