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

  • Home
  • SEARCH
  • 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 8279929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:29:20+00:00 2026-06-08T09:29:20+00:00

I’d like to figure out in general how to use mutable state in the

  • 0

I’d like to figure out in general how to use mutable state in the computation of lazy lists.

For instance, here is a naive Sieve of Eratosthenes implemented using a mutable array (source):

import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed
import Control.Monad
import Data.List

prime :: Int -> UArray Int Bool
prime n = runSTUArray $ do
    arr <- newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool )
    forM_ ( takeWhile ( \x -> x*x <= n ) [ 2 .. n ] ) $ \i -> do
        ai <- readArray arr i
        when ( ai  ) $ forM_ [ i^2 , i^2 + i .. n ] $ \j -> do
            writeArray arr j False
            -- yield i ???

prime n returns an array of booleans which denote which numbers are prime.

Is there a way to use this approach to create a lazy-list of those primes? It would be like adding a yield i right after the writeArray statement.

  • 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-08T09:29:22+00:00Added an answer on June 8, 2026 at 9:29 am

    The smallest modification of your program to achieve lazyness is probably to switch to the lazy ST monad (http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad-ST-Lazy.html), where this code would work:

    import Control.Monad.ST.Lazy
    import Data.Array.ST
    import Data.Array.Unboxed
    import Control.Monad
    import Data.List
    import Data.Maybe
    
    prime :: Int -> [Int]
    prime n = catMaybes $ runST $ do
        arr <- strictToLazyST $ newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool )
        forM ( takeWhile ( \x -> x <= n ) [ 2 .. n ] ) $ \i -> do
            if i == 83 then error "Reached 83" else return ()
            ai <- strictToLazyST $ readArray arr i
            if ai
              then do
                strictToLazyST $ forM_ [ i^2 , i^2 + i .. n ] $
                     \j -> writeArray arr j False
                return (Just i)
              else return Nothing
    

    The error call is just to demonstrate the true lazy nature of the result:

    *Main> prime 10000
    [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79*** Exception: Reached 83
    

    If you want to avoid the intermediate list of Maybes, you can, for example, use this code:

    import Control.Monad.ST.Lazy
    import Data.Array.ST
    import Data.Array.Unboxed
    import Control.Monad
    import Data.List
    import Data.Functor
    
    prime :: Int -> [Int]
    prime n = runST $ do
        arr <- strictToLazyST $ newArray ( 2 , n ) True :: ST s ( STUArray s Int Bool )
        let primesFrom i | i > n = return []
                         | otherwise = do
                ai <- strictToLazyST $ readArray arr i
                if ai then do
                    strictToLazyST $ forM_ [ i^2 , i^2 + i .. n ] $
                       \j -> writeArray arr j False
                    (i:) <$> primesFrom (i + 1)
                  else primesFrom (i + 1)
        primesFrom 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.