I'm trying to memoize this function with an array:
a n 0 = n
a n k = a (2*n - 1) (k - 1) / a (2*n) (k - 1)
I've done this:
import Data.Array
cache :: (Ix a, Real b) => Array a [(a, b)]
cache = array bounds [(i, func i) | i <- range bounds]
where bounds = ((0,0), (1000, 1000))
func elem =
let f n 0 = n
f n k = f (2*n - 1) (k - 1) / f (2*n) (k - 1)
in uncurry f elem
a n k = cache!(n,k)
GHCi fails with this error:
aarr.hs:6:16:
Could not deduce (a ~ ([(a, b)], b0))
from the context (Ix a, Real b)
bound by the type signature for
values :: (Ix a, Real b) => Array a [(a, b)]
at aarr.hs:5:11-44
‘a’ is a rigid type variable bound by
the type signature for values :: (Ix a, Real b) => Array a [(a, b)]
at aarr.hs:5:11
Expected type: (a, a)
Actual type: (([(a, b)], b0), ([(a, b)], b0))
Relevant bindings include
bounds :: (([(a, b)], b0), ([(a, b)], b0)) (bound at aarr.hs:7:11)
values :: Array a [(a, b)] (bound at aarr.hs:6:1)
In the first argument of ‘array’, namely ‘bounds’
In the expression: array bounds [(i, func i) | i <- range bounds]
My head is spinning from all the type errors (and infinite type ones) I got... I thought this could work.
rangeisrange :: Ix a => (a, a) -> [a]and you're giving it((a,a),(a,a)). Read the last 2 lines of ghci's output:In the first argument of ‘array’, namely ‘bounds’ In the expression: array bounds [(i, func i) | i <- range bounds]Prelude Data.Array> range ((0, 0), (3, 3)) [(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(1,3),(2,0),(2,1),(2,2),(2,3),(3,0),(3,1),(3,2),(3,3)]. I think(0, 0)is an instance ofIxtoo.