[Haskell-cafe] How to incrementally update list

Branimir Maksimovic bmaxa at hotmail.com
Wed Nov 28 14:38:25 CET 2012


Thank you very much! That solved it ;)I had to put explicit type signature in front of advance in order to compile
From: cgaebel at uwaterloo.ca
Date: Wed, 28 Nov 2012 08:01:38 -0500
Subject: Re: [Haskell-cafe] How to incrementally update list
To: edwards.benj at gmail.com
CC: bmaxa at hotmail.com; haskell-cafe at haskell.org

Here's a version that works:

import Control.DeepSeq

list = [1,2,3,4,5]
advance l = force $ map (\x -> x+1) l


run 0 s = srun n s = run (n-1) $ advance s
main = do        let s =  run 50000000 list        putStrLn $ show s

The problem is that you build of a huge chain of updates to the list. If we just "commit" each update as it happens, we'll use a constant amount of memory.



Haskell's laziness is tricky to understand coming from imperative languages, but once you figure out its evaluation rules, you'll begin to see the elegance.

Ηope this helps,
  - Clark




On Wed, Nov 28, 2012 at 7:07 AM, Benjamin Edwards <edwards.benj at gmail.com> wrote:


TCO + strictnesses annotations should take care of your problem.
On 28 Nov 2012 11:44, "Branimir Maksimovic" <bmaxa at hotmail.com> wrote:







Problem is following short program:list = [1,2,3,4,5]
advance l = map (\x -> x+1) l
run 0 s = srun n s = run (n-1) $ advance s



main = do        let s =  run 50000000 list        putStrLn $ show s
I want to incrementally update list lot of times, but don't knowhow to do this.


Since Haskell does not have loops I have to use recursion,but problem is that recursive calls keep previous/state parameterleading to excessive stack.and memory usage.I don't know how to tell Haskell not to keep previous


state rather to release so memory consumption becomesmanagable.
Is there some solution to this problem as I think it is rathercommon?
 		 	   		  




_______________________________________________

Haskell-Cafe mailing list

Haskell-Cafe at haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe




_______________________________________________

Haskell-Cafe mailing list

Haskell-Cafe at haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe



 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121128/fc8a5cbd/attachment.htm>


More information about the Haskell-Cafe mailing list