This is actually a perfect case for lazy immutable arrays, if you use a circular program:

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
  where
    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.

/ Emil



Magnus Therning skrev:
On Tue, Dec 16, 2008 at 12:14 PM, Lemmih <lem...@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.

/M


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to