I am trying to implement an applicative instance for this type:
newtype State s a = State {runState :: s -> (a, s)}
I have different ideas for the function (<*>). One way to realize it that comes to my mind is to
(<*>) :: State s (a -> b) -> State s a -> State s b
State f <*> State s = State $ do
(fa, fs) <- f
let (sa, ss) = s fs
return (fa sa, ss)
or
(<*>) :: State s (a -> b) -> State s a -> State s b
State f <*> State s = State $ do
(sa, ss) <- s
let (fa, fs) = f ss
return (fa sa, fs)
Which one (or even any of them) is correct and why?
They are both typecheck and differ only in the order of state conversion. I can not find good reason to prefer one after another ...
source
share