[GHC] #3474: Add a strict variant of iterate to Data.List
GHC
ghc-devs at haskell.org
Fri Aug 18 14:26:57 UTC 2017
#3474: Add a strict variant of iterate to Data.List
-------------------------------------+-------------------------------------
Reporter: mux | Owner: (none)
Type: proposal | Status: patch
Priority: normal | Milestone: 8.4.1
Component: libraries/base | Version: 6.10.4
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s): Phab:D3870
Wiki Page: |
-------------------------------------+-------------------------------------
Description changed by bgamari:
Old description:
> I suggest adding a strict variant of the iterate function to the
> Data.List module, as others seem to have had a need for it too. It is
> useful when you want to repeatedly apply a function a large number of
> times and get the final result. Using the standard iterate function in
> this way results in the whole list being held in memory, as exemplified
> in the following GHCi session (code compiled with -O2 behaves similarly):
>
> > let f = (+1) in iterate f 0 !! 10000000
> *** Exception: stack overflow
>
> Using a strict variant of iterate seems to be sufficient for this code to
> run in O(1) memory:
>
> > let iterate' f x = x `seq` x : iterate' f (f x)
> > let f = (+1) in iterate' f 0 !! 10000000
> 10000000
>
> I have no idea if this is something that could/should be detected by the
> strictness analyzer; that would obviously be preferable if it is indeed
> possible.
New description:
I suggest adding a strict variant of the iterate function to the Data.List
module, as others seem to have had a need for it too. It is useful when
you want to repeatedly apply a function a large number of times and get
the final result. Using the standard iterate function in this way results
in the whole list being held in memory, as exemplified in the following
GHCi session (code compiled with -O2 behaves similarly):
{{{
> let f = (+1) in iterate f 0 !! 10000000
*** Exception: stack overflow
}}}
Using a strict variant of iterate seems to be sufficient for this code to
run in O(1) memory:
{{{
> let iterate' f x = x `seq` x : iterate' f (f x)
> let f = (+1) in iterate' f 0 !! 10000000
10000000
}}}
I have no idea if this is something that could/should be detected by the
strictness analyzer; that would obviously be preferable if it is indeed
possible.
--
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/3474#comment:8>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list