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 606259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:14:08+00:00 2026-05-13T17:14:08+00:00

I am looking for a mutable (balanced) tree/map/hash table in Haskell or a way

  • 0

I am looking for a mutable (balanced) tree/map/hash table in Haskell or a way how to simulate it inside a function. I.e. when I call the same function several times, the structure is preserved. So far I have tried Data.HashTable (which is OK, but somewhat slow) and tried Data.Array.Judy but I was unable to make it work with GHC 6.10.4. Are there any other options?

  • 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-05-13T17:14:08+00:00Added an answer on May 13, 2026 at 5:14 pm

    If you want mutable state, you can have it. Just keep passing the updated map around, or keep it in a state monad (which turns out to be the same thing).

    import qualified Data.Map as Map
    import Control.Monad.ST
    import Data.STRef
    memoize :: Ord k => (k -> ST s a) -> ST s (k -> ST s a)
    memoize f = do
        mc <- newSTRef Map.empty
        return $ \k -> do
            c <- readSTRef mc
            case Map.lookup k c of
                Just a -> return a
                Nothing -> do a <- f k
                              writeSTRef mc (Map.insert k a c) >> return a
    

    You can use this like so. (In practice, you might want to add a way to clear items from the cache, too.)

    import Control.Monad
    main :: IO ()
    main = do
        fib <- stToIO $ fixST $ \fib -> memoize $ \n ->
            if n < 2 then return n else liftM2 (+) (fib (n-1)) (fib (n-2))
        mapM_ (print <=< stToIO . fib) [1..10000]
    

    At your own risk, you can unsafely escape from the requirement of threading state through everything that needs it.

    import System.IO.Unsafe
    unsafeMemoize :: Ord k => (k -> a) -> k -> a
    unsafeMemoize f = unsafePerformIO $ do
        f' <- stToIO $ memoize $ return . f
        return $ unsafePerformIO . stToIO . f'
    
    fib :: Integer -> Integer
    fib = unsafeMemoize $ \n -> if n < 2 then n else fib (n-1) + fib (n-2)
    
    main :: IO ()
    main = mapM_ (print . fib) [1..1000]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at some code that creates a mutable data object and puts a
Looking for a way to get path of file (and then process it in
I'm looking for a single SQL query to run on an oracle table that
If I have several mutable properties in an object that will be acted upon
I'm looking to loop through the rows of a table and checking to see
I have several databases that have tables with the same name. I mean, I
I'm looking for a way to parse c++ code to retrieve some basic information
I am looking for a way to restrict invocations of certain objects. Given a
We hit an extremely surprising exception today. Inside of a synchronized block, we call
Is there a way in SQL Server to create a table with a primary

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.