{-# LANGUAGE UnicodeSyntax #-} module StateBind where import Control.Monad.State data Tree a = Leaf | Node (Tree a) a (Tree a) deriving Show labelTree ∷ Tree a → Tree Int labelTree t = fst $ runState (labelTreeW t) 0 -- evalState = fst . runState labelTreeW ∷ Tree a → State Int (Tree Int) labelTreeW Leaf = return Leaf labelTreeW (Node l _ r) = labelTreeW l >>= \l' → get >>= \counterN → put (counterN + 1) >>= \_ → labelTreeW r >>= \r' → return $ Node l' counterN r'