Created
April 28, 2012 18:59
-
-
Save tanakh/2521319 to your computer and use it in GitHub Desktop.
MonadMemoの例 ref: http://qiita.com/items/019f6c4e5629c9e27e59
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment