[Haskell-cafe] How to think about this? (profiling)
emax at chalmers.se
Tue Dec 16 08:47:32 EST 2008
This is actually a perfect case for lazy immutable arrays, if you use a circular
> import Data.Array
> foo' :: Array Int Int -> Int -> Int
> foo' arr 0 = 0
> foo' arr 1 = 1
> foo' arr 2 = 2
> foo' arr n = arr!(n-1) + arr!(n-2) + arr!(n-3)
> foo :: Int -> Int
> foo n = arr ! n
> assocs = [(i, foo' arr i) | i <- [0..n]]
> arr = array (0,n) assocs
But I haven't checked its performance against your version, so I don't know how
good it is.
Magnus Therning skrev:
> On Tue, Dec 16, 2008 at 12:14 PM, Lemmih <lemmih at gmail.com> wrote:
>> You could use a Map or a mutable array. However, this kind of problem
>> comes up a lot less often than you'd think.
> Well, I happen to have a problem just like it right now, hence my
> interest :-) In order to better understand the different options I
> thought I'd look at solving simpler problems with similar "shape".
> Thanks for pointing me in the direction of mutable arrays, I haven't
> explored those before.
More information about the Haskell-Cafe