{-# LANGUAGE FlexibleContexts #-}
import Control.Applicative
import Control.Monad.Memo
f :: (Functor m, Applicative m, MonadMemo Int Float m)
=> (Int -> m Float)
-> (Int -> m Float)
-> (Int -> m Float)
f a b 0 = (/) <$> memo a 0 <*> memo b 0
f a b n = do
an1 <- memo a (n+1)
b0 <- memo b 0
cs <- mapM (memo c) [0..(n-1)]
bs <- mapM (memo b) $ reverse [1..n]
return $ (an1 - sum (zipWith (*) cs bs)) / b0
where
c = f a b
{-
f a b 0 = (a 0)/(b 0)
f a b n = ((a (n+1)) - sum (zipWith (*) cs bs)) / (b 0)
where
c = f a b
cs = map c [0..(n-1)]
bs = map b $ reverse[1..n]
-}
main :: IO ()
main = do
let a n = return $ fromIntegral n
b n = return $ fromIntegral n + 100
print $ sum $ map (startEvalMemo . f a b) [1..100]
こんな感じなんでしょうか…?