[Haskell-cafe] Re: tail recursion ?
Jon Fairbairn
jon.fairbairn at cl.cam.ac.uk
Mon Jun 11 12:48:16 EDT 2007
H. <h._h._h._ at hotmail.com> writes:
> Hello @ all,
>
> Sometimes one has an imperative algorithm, and wants to write a program in
> Haskell which do has the same effect...
>
> So the question is - how a construct as the following can efficiently be
> written?
>
> --
> Pseudo code:
> n[1..10000] = false
> for (1..10000) |i|
> for (i,2*i..10000) |j|
> n[j] = not n[j]
> --
>
> Certainly it is in this special case equivalent to (True where the index is):
> map (^2) [1..100]
>
> But I mean the destructive updates in the imperative code in Haskell without
> filling the (many times more than in imperative languages) memory with
> recursively called functions...
The idea in Haskell is not to think of stepping through the
array. Look at accumArray and ixmap.
--
Jón Fairbairn Jon.Fairbairn at cl.cam.ac.uk
More information about the Haskell-Cafe
mailing list