This package has some functions to turn recursive functions into dynamic programming recursive functions, for better performance:
http://hackage.haskell.org/packages/archive/data-memocombinators/0.3/doc/html/Data-MemoCombinators.html#t:Memo
Unfortunately, they only have an example for the simplest type of function, and there are no examples on how to use a function of 2 variables. Where can I find an example of how to, for example, turn [Int] -> Int -> Int
function into a dynamic-programming function? The documentation says memo2 takes two Memo
s as first arguments, but I'm not sure what that means.
Solution:
As Hammar described, instead of defining a function as:
foo :: [Int] -> Int -> Int
foo list value = ...
to use memo2:
import qualified Data.MemoCombinators as Memo
foo = Memo.memo2 (Memo.list Memo.integral) Memo.integral foo'
where ... (define foo' with recursive calls to foo.)
The library defines the type Memo a
, which is a "memoizer" for functions taking arguments of type a
. The key to understanding how to use this library is to understand how to use and compose these memoizers.
In the simple case, you have a single argument function and a simple memoizer, for example a Fibonacci function and a memoizer for Integral
arguments. In such a case, we obtain the memoized function by applying the memoizer to the function to be memoized:
fib = Memo.integral fib'
where
fib' 0 = 0
fib' 1 = 1
fib' x = fib (x-1) + fib (x-2)
Some memoizers take arguments to customize their behavior, for example arrayRange
. In the following example, fib n
will only be memoized if n
is between 1 and 100.
fib = Memo.arrayRange (1, 100) fib'
where ...
The library also provides combinators for building more complex memoizers out of simple ones. For example, list
, which turns a memoizer for a
into a memoizer for [a]
.
Finally, to memoize functions of multiple arguments there are the functions memo2
and memo3
, which take a memoizer for each argument plus a function and returns a memoized function.
So to memoize your two-argument function, we will need to use memo2
. We can use the integral
memoizer for the Int
argument, and for the [Int]
argument we can use list integral
. Putting this together, we get something like this:
memo2 (list integral) integral foo
However, you can also use more specific memoizers if you know the numbers are in some specified range. For example, if the numbers in the list are between 1 and 10 and the second argument is between 15 and 20:
memo2 (list $ arrayRange (1, 10)) (arrayRange (15, 20)) foo
Whether this makes sense or not, depends on your application.
Per types and documentation, I believe
foo :: [Int] -> Int -> Int
should be memoised per
memo2 (list integral) integral foo
(disclaimer: I haven't used data-memocombinators).