I want to compute the recursively-defined function values r(i,j), which are defined by
r i j | i<0 || j<0 = 0
| i==0 && j==0 = 1
| otherwise = (i-1) * r (i-2) j + r (i-1) (j-1)
Obviously, the NxN table of these coefficients can be computed in O(N^2).
Unfortunately, the straightforward evaluation, like
[[r i j | j <-[0..50]]| i <- [0..50]]
is performed in screamingly ineffective way (exponential complexity). Apparently, Haskell builds the entire recursion tree for each r i j and ignores the values of previously computed r (i-1) (j-1) etc.
What is the elegant and effective way to compute such a table?