I am trying to implement Applicative instance for such a type:
newtype State s a = State {runState :: s -> (a, s)}
I have some different ideas for (<*>) function. One way to implement it that comes to my mind is
(<*>) :: 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 does even any of them) is correct and why?
They both typecheck and differ only by "state" transformations order. I cannot find any good reason to prefer one over another...