Untitled

mail@pastecode.io avatar
unknown
haskell
3 years ago
752 B
3
Indexable
Never
data BalanceState = BalanceState{getActions::[Action]
                                ,getBalances::[Difference]
                                ,getWorld::World} deriving (Eq,Show)

instance Ord BalanceState where
  compare = compare `on` evaluate'
    where
      evaluate' st = (dist st,cost st)
      dist st = evaluate (getWorld st) (getBalances st)
      cost st = totalCost (getWorld st) (getActions st)

evaluate world balances = sum $ map evaluate' balances
  where
    evaluate' diff = abs $ getAmount diff * getMultiplier diff
    getMultiplier diff | isDefaultCurrency diff = 1 / 2
                       | otherwise              = runReader (toDefaultCurrency diff) world

totalCost world = sum . map ((`runReader` world) . getActionCost)