I'm not interested in the actual solution or other methods solving the problem, it's the memoization i need help with :)
I need help doing a pascals triangle problem with memoization. I want to get the middle number in the base of the triangle. (Project Euler 15)
The first example is not memoized (although the name suggests so) "20 20" not solvable
Second attempt is an attempt on doing something similar to: http://www.haskell.org/haskellwiki/Memoization
Third is hlints suggestion on no2 if that is more readable for someone.
I get this error, but i'm not sure its right even if it would compile... (run from ghci with 2 2 as params
no instance for (Num [a0])
arising from a use of `zmemopascals'
Possible fix: add an instance declaration for (Num [a0])
In the expression: zmemopascals 2 2
In an equation for `it': it = zmemopascals 2 2
.
Code:
--1
memopascals r c = [[pascals a b | b<-[1..c]] | a<-[1..r]] !! (r-1) !! (c-1)
where pascals 1 _ = 1
pascals _ 1 = 1
pascals x y = memopascals (x-1) y + memopascals x (y-1)
--2
--xmemopascals :: Int -> Int -> Int
xmemopascals r c = map (uncurry pascals) (zip [1..] [1..]) !! (r-1) !! (c-1)
where pascals 1 _ = 1
pascals _ 1 = 1
pascals x y = memopascals (x-1) y + memopascals x (y-1)
--3
zmemopascals r c = zipWith pascals [1 ..] [1 ..] !! (r-1) !! (c-1)
where pascals 1 _ = 1
pascals _ 1 = 1
pascals x y = memopascals (x-1) y + memopascals x (y-1)