我对解决问题的实际解决方案或其他方法不感兴趣,这是我需要帮助的记忆:)
我需要帮助解决记忆化的帕斯卡三角问题。我想得到三角形底部的中间数字。(欧拉计划 15)
第一个例子没有被记忆(虽然名字是这样暗示的)“20 20”不可解
第二次尝试是尝试做类似的事情:http ://www.haskell.org/haskellwiki/Memoization
第三是关于 no2 的 hlints 建议,如果这对某人来说更具可读性。
我收到这个错误,但我不确定它是否正确,即使它会编译......(从 ghci 运行,以 2 2 作为参数
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)