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
-- 「つまずきの記憶 モナド(3)」のソースコード | |
-- Stateモナドを使ったメモ化 [statememo.hs] | |
import Control.Monad.State -- Stateモナドを使えるようにする。 | |
import qualified Data.Map as M | |
-- 状態操作関連 | |
type Memo a b = M.Map a b | |
getResult :: Ord a => a -> Memo a b -> Maybe b | |
getResult = M.lookup |
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
-- 「つまずきの記憶 メモ化(4)」のソースコード | |
-- メモ機能をモナド化する [mfib-3.hs] | |
-- 状態操作関連 | |
type Memo a b = [(a,b)] | |
getResult :: Ord a => a -> Memo a b -> Maybe b | |
getResult = lookup | |
putResult :: Ord a => (a, b) -> Memo a b -> Memo a b |
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
-- 「つまずきの記憶 メモ化(3)」のソースコード | |
-- メモ付きのFibonacci関数 [mfib-2.hs] | |
-- 型の定義 | |
-- ここでは型変数を用いてどの型にも対応できるようにしておき、 | |
-- 実体を定義するときに具体型を与える。 | |
type Memo a b = [(a,b)] | |
newtype Mc s b = McD { runMc :: s -> (b, s) } | |
-- メモ付きFibonacci関数 |
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
-- 「つまずきの記憶 メモ化(2)」のソースコード | |
-- メモ付きのFibonacci関数 [mfib-1.hs] | |
-- 型の定義 | |
-- 「メモ化(1)」の `a`,`b`,`s` がここの`A`,`B`,`S`に対応している。 | |
type A = Int | |
type B = Integer | |
type S = [(A,B)] | |
type St = S -> (B, S) |