A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValueScore.hs 972B

12345678910111213141516171819202122232425262728
  1. import Data.Map as Map
  2. import Data.Maybe
  3. import Control.Applicative
  4. data Element = A | B | C | D
  5. deriving (Eq, Ord, Show)
  6. addElement :: Element -> Map Element Int -> Map Element Int
  7. addElement = alter ((<|> Just 1) . fmap (+1))
  8. getScore :: Element -> Map Element Int -> Float
  9. getScore e m = fromMaybe 1.0 $ ((1.0/) . fromIntegral) <$> Map.lookup e m
  10. data BinaryTree a = Empty | Node a (BinaryTree a) (BinaryTree a) deriving Show
  11. type ElementTree = BinaryTree Element
  12. type ScoredElementTree = BinaryTree (Element, Float)
  13. assignScores :: ElementTree -> Map Element Int -> (Map Element Int, ScoredElementTree)
  14. assignScores Empty m = (Map.empty, Empty)
  15. assignScores (Node e t1 t2) m = (m', Node (e, getScore e m) t1' t2')
  16. where
  17. (m1, t1') = assignScores t1 m
  18. (m2, t2') = assignScores t2 m
  19. m' = addElement e $ unionWith (+) m1 m2
  20. doAssignScores :: ElementTree -> ScoredElementTree
  21. doAssignScores t = t'
  22. where (m, t') = assignScores t m