Created
July 24, 2017 04:46
-
-
Save junjiah/702461ad8e756d723644021f77ad9917 to your computer and use it in GitHub Desktop.
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
module FunctionEvaluator where | |
import qualified Data.Map.Strict as M | |
evaluateFunction :: Ord a => (a -> Either b ([a], [b] -> b)) -> a -> b | |
evaluateFunction f x = snd $ memoize x M.empty | |
where | |
memoize n m = case M.lookup n m of | |
Just v -> (m, v) | |
Nothing -> | |
case f n of | |
Left v -> (M.insert n v m, v) | |
Right (as, aggF) -> | |
let folder = \(accM, accBs) a -> let (m, v) = memoize a accM in (m, v:accBs) in | |
let (m', bs) = foldl folder (m, []) as in | |
let v = aggF bs in (M.insert n v m', v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment