### List Patterns: MonadState and Traverse

There's an even more concise and general way to solve for these patterns:

```
-- result and state are the same, just the running sum.
sumState :: (Num b, MonadState b m) => b -> m b
sumState y = get >>= \x -> let z = x + y in put z >> return z
-- traverse container answering new container with sum of values traversed so far.
stateSum :: (Num a, Traversable t) => t a -> t a
stateSum xs = evalState (traverse sumState xs) 0
$ stateSum [1..10]
[1,3,6,10,15,21,28,36,45,55]
-- state is pair, (sum, count), answer is (sum / count)
avgTotState :: (Fractional b, MonadState (b,b) m) => b -> m b
avgTotState y = get >>= \(s,n) -> let s' = s + y in put (s',n+1) >> return (s'/(n+1))
cumAvgState :: Fractional a => [a] -> [a]
cumAvgState [] = []
cumAvgState (x:xs) = evalState (traverse avgTotState xs) (x,1)
$ cumAvgState [1..10]
[1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5]
-- state is window of 'n' items to average over, answer is average of items in window
avgRunState :: (Fractional b, MonadState [b] m) => Int -> b -> m b
avgRunState c y = get >>= \rs -> let rs' = take c (y:rs) in put rs' >> return (sum rs' / fromIntegral c)
runAvgState :: Fractional a => Int -> [a] -> [a]
runAvgState 0 _ = []
runAvgState _ [] = []
runAvgState c xss@(_:_)
| length xss < c = []
| otherwise = evalState (traverse (avgRunState c) (drop c xss)) (take c xss)
$ runAvgState 4 [1..20]
[2.75,3.5,4.75,6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5]
```

Rather than managing state and anser explicitly, as we did above with `foldl`

, you can delegate that to the `MonadState`

typeclass. You have to remap ordinary Haskell from the `foldl`

function to the `get`

and `put`

operations in the `State`

class and the way you combine them with `Monad`

type class functions `>>=`

, `>>`

, and `return`

. That sounds like more work than it actually turns out to be, especially if you keep simple templates like these in your back pocket.

Note finally we generalize over the `Num`

and `Fractional`

typeclasses. This greately widens the surface of these functions as represented by all the types with `Num`

, `Fractional`

, and `Traversable`

instances, vs. just working over lists. Reading about the burning bridges upgrade to the GHC compiler got me interested in how I might follow this example to better generalize my code.

Coming up with those type signatures wasn't just a question of reading the documentation and typing in the correct answer. I find `ghci`

a valuable assistant, and when I don't get my type signatures right the first time, I often figure out where I went wrong by temporarily commenting them out, loading the code into `ghci`

and asking it to tell me type for the function. That an be a powerful way to explore new features, for example `(head Control.Arrow.&&& length)`

in the example below, which I came across working on a parser for a binary file:

```
-- For a a sortable list, answer list of items by frequency of occurence, sorted in descending order by count
frequencyCount :: Ord a => [a] -> [(a,Int)]
frequencyCount xs = sortBy (compare `on` snd) $ map (head Control.Arrow.&&& length) $ (group . sort) xs
```