Dos parámetros de memorización en Haskell
Estoy tratando de memorizar la siguiente función:
gridwalk x y
| x == 0 = 1
| y == 0 = 1
| otherwise = (gridwalk (x - 1) y) + (gridwalk x (y - 1))
Mirando aest Se me ocurrió la siguiente solución:
gw :: (Int -> Int -> Int) -> Int -> Int -> Int
gw f x y
| x == 0 = 1
| y == 0 = 1
| otherwise = (f (x - 1) y) + (f x (y - 1))
gwlist :: [Int]
gwlist = map (\i -> gw fastgw (i `mod` 20) (i `div` 20)) [0..]
fastgw :: Int -> Int -> Int
fastgw x y = gwlist !! (x + y * 20)
Que entonces puedo llamar así:
gw fastgw 20 20
Hay una forma más fácil, más concisa y general (observe cómo tuve que codificar las dimensiones máximas de la cuadrícula en lagwlist
para convertir de 2D a 1D para poder acceder a la lista de memorización) para memorizar funciones con múltiples parámetros en Haskell?