This is a nice explanation of the State Monad https://wiki.haskell.org/State_Monad #Haskell I've basically cut and paste the example more than once before to solve problems.