I am reading the 2nd edition of the (great) book from Graham Hutton "Progamming in Haskell" (Cambridge Press).
Reading the State Monad section, I stumbled on a small task that I gave myself.
How could you rewrite the following using where instead of let?
type State = Int
newtype ST a = S (State -> (a, State))
instance Functor ST where
-- fmap :: (a -> b) -> ST a -> ST b
fmap g st = S (\state -> let (x, s') = app st state in (g x, s'))
I tried variations around this code but it doesn't work:
instance Functor ST where
-- fmap :: (a -> b) -> ST a -> ST b
fmap g st = S (\state -> (g x, newstate))
where (x, newstate) = app st state
I know it's not useful per se, but I would like to know if and how it is possible.