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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:42:09+00:00 2026-06-07T09:42:09+00:00

I implemented a small function bruteforce , using lazy evaluation to find the first

  • 0

I implemented a small function bruteforce, using lazy evaluation to find the first valid solution for a problem:

import Data.Maybe

bruteforce :: (a -> Bool) -> [a] -> Maybe a
bruteforce f xs
  | null result = Nothing
  | otherwise = Just $ head result
  where
    result = mapMaybe bruteforce' xs
    -- test one instance
    bruteforce' x
      | f x = Just x
      | otherwise = Nothing

generatorString :: Int -> [String]
generatorString 0 = [""]
generatorString deep = concatMap (\x -> map (\ys -> (x:ys)) nextgen) ['a'..'z']
  where nextgen = generatorString (deep - 1)


main :: IO ()
main = do
  putStrLn $ fromJust $ bruteforce ((==) "zabcde") (generatorString 6)

also avaiable as a gist. yes, the test function is stupid, but it’s enough to show what’s the problem. It works as expected, however memory consumption is quite high:

$ ghc --make -O2 brute.hs -o brute -rtsopts
$ ./brute +RTS -s
zabcde
  24,752,866,120 bytes allocated in the heap
  15,748,430,224 bytes copied during GC
     581,741,352 bytes maximum residency (22 sample(s))
      18,464,056 bytes maximum slop
            1719 MB total memory in use (0 MB lost due to fragmentation)
[...]

so I tried to force the RTS using less memory (i.e. call the GC more often). 200 MB should be enough?

$ ./brute +RTS -s -M200M
Heap exhausted;
Current maximum heap size is 209715200 bytes (200 MB);
use `+RTS -M<size>' to increase it.

well, nope (I wouldn’t like this approach anyway…)

Any pointers how one could rewrite bruteforce to be more memory friendly?

  • 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-07T09:42:10+00:00Added an answer on June 7, 2026 at 9:42 am

    If the memory consumption is the primary concern, stop sharing nextgen. That’s a huge list.

    So replace

    generatorString deep = concatMap (\x -> map (\ys -> (x:ys)) nextgen) ['a'..'z']
      where nextgen = generatorString (deep - 1)
    

    with

    generatorString deep = concatMap (\x -> map (\ys -> (x:ys)) $ generatorString (deep - 1)) ['a'..'z']
    

    and tell the compiler that you’re serious about not sharing it:

    $ ghc -O2 -fno-full-laziness -rtsopts bruteforce
    

    That runs in

     $ ./bruteforce +RTS -s
     zabcde
     189,448,835,904 bytes allocated in the heap
      18,301,350,520 bytes copied during GC
              29,504 bytes maximum residency (16901 sample(s))
              37,248 bytes maximum slop
                   2 MB total memory in use (0 MB lost due to fragmentation)
    

    small resident memory. Of course the recomputation means the total allocation is much higher, and it also takes much longer to compute the result.

    A better algorithm could reduce space and time consumption.

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

Sidebar

Related Questions

I want to animate a ball using html5 and i implemented this small script
I implemented a small function, which parses an SQL INSERT statement and highlights a
I have implemented a small Windows Service which runs every 9 minutes and writes
I have a small dynamic site implemented in mod_python . I inherited this, and
I have a small application in C#, and so far I have implemented it
I'm using smoothscroll.js on my website, that uses the 'click' function. It works great,
I implemented a gradient descent algorithm to minimize a cost function in order to
I implemented a small test that is based on Scott Guthrie article http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx But
I wrote a small quicksort implementation in matlab to sort some custom data. Because
I've implemented a camera using the AVFoundation framework provided in iOS 4 & 5,

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.