[Haskell-cafe] Re: tail recursion ?

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Mon Jun 11 15:27:10 EDT 2007


H. <h._h._h._ at hotmail.com> writes:

> Jon Fairbairn <jon.fairbairn <at> cl.cam.ac.uk> writes:
> > The idea in Haskell is not to think of stepping through the
> > array.  Look at accumArray and ixmap.
> 
> Thanks for your answer.
> 
> But I can't really see how the calc-function can be written more efficiently 
> with accumArray or ixmap, perhaps you can write it as an example?

Well, for your example, neither is needed since you could
write something like:

 upb = 10000
 listArray (1,upb) (repeat False)
           //map (\n->(2^n,True)) [1..floor (logBase 2 $ fromIntegral upb)]

or

 a = listArray (1,upb) (repeat False)
 b = a//map (\n->(2^n,not (a!(2^n)))) [1..floor (logBase 2 $ fromIntegral upb)]

but the first one is obviously better,
so it depends on what you really want to do.


-- 
Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2007-05-07)



More information about the Haskell-Cafe mailing list