Created
May 22, 2013 22:27
-
-
Save nobsun/5631452 to your computer and use it in GitHub Desktop.
複数の関数のメモ化はできるか ref: http://qiita.com/items/93e2110ddb80e1e6f2c1
This file contains 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 TupleSections #-} | |
module Main where | |
import Data.Function.YaMemo (Memo, memo) | |
import Data.Map (Map) | |
main :: IO () | |
main = undefined | |
data FunID = A | B | C | D | E deriving (Eq, Ord, Enum) | |
usCoin :: (Ord a, Num a, Num b) => ((FunID, a) -> b) -> ((FunID, a) -> b) | |
usCoin _ (_,n) | n < 0 = 0 | |
usCoin _ (A,_) = 1 | |
usCoin f (B,n) = f (A,n) + f (B,n-5) | |
usCoin f (C,n) = f (B,n) + f (C,n-10) | |
usCoin f (D,n) = f (C,n) + f (D,n-25) | |
usCoin f (E,n) = f (D,n) + f (E,n-50) | |
memoTable :: (Memo Map) (FunID, Integer) Integer | |
memoTable = undefined | |
usCoinMemo :: Integer -> Integer | |
usCoinMemo = (memo memoTable usCoin) . (E,) |
This file contains 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
usCoinA,usCoinB,usCoinC,usCoinD,usCoinE :: Integer -> Integer | |
usCoinA n | n < 0 = 0 | |
usCoinA _ = 1 | |
usCoinB n | n < 0 = 0 | |
usCoinB n = usCoinA n + usCoinB (n-5) | |
usCoinC n | n < 0 = 0 | |
usCoinC n = usCoinB n + usCoinC (n-10) | |
usCoinD n | n < 0 = 0 | |
usCoinD n = usCoinC n + usCoinD (n-25) | |
usCoinE n | n < 0 = 0 | |
usCoinE n = usCoinD n + usCoinE (n-50) |
This file contains 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
data FunID = A | B | C | D | E deriving (Eq,Ord,Enum) | |
usCoin :: FunID -> Integer -> Integer | |
usCoin _ n | n < 0 = 0 | |
usCoin A _ = 1 | |
usCoin B n = usCoin A n + usCoin B (n-5) | |
usCoin C n = usCoin B n + usCoin C (n-10) | |
usCoin D n = usCoin C n + usCoin D (n-25) | |
usCoin E n = usCoin D n + usCoin E (n-50) |
This file contains 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
usCoin :: (FunID, Integer) -> Integer | |
usCoin (_,n) | n < 0 = 0 | |
usCoin (A,_) = 1 | |
usCoin (B,n) = usCoin (A,n) + usCoin (B,n-5) | |
usCoin (C,n) = usCoin (B,n) + usCoin (C,n-10) | |
usCoin (D,n) = usCoin (C,n) + usCoin (D,n-25) | |
usCoin (E,n) = usCoin (D,n) + usCoin (E,n-50) |
This file contains 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
usCoin _ (_,n) | n < 0 = 0 | |
usCoin _ (A,_) = 1 | |
usCoin f (B,n) = f (A,n) + f (B,n-5) | |
usCoin f (C,n) = f (B,n) + f (C,n-10) | |
usCoin f (D,n) = f (C,n) + f (D,n-25) | |
usCoin f (E,n) = f (D,n) + f (E,n-50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment