1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MonadMemoの例

Posted at
{-# 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]

こんな感じなんでしょうか…?

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?