简体   繁体   中英

Is there an “of” syntax in Haskell?

newtype State s a = StateOf (s -> (s, a))

deState :: State s a -> (s -> (s, a))
deState (StateOf stf) = stf

instance Functor (State s) where
    -- fmap :: (a -> b) -> State s a -> State s b
    fmap f (StateOf stf) = StateOf (\s0 -> case stf s0 of (s1, a) -> (s1, f a))

In the last line

fmap f (StateOf stf) = StateOf (\s0 -> case stf s0 of (s1, a) -> (s1, f a))

The of syntax makes me confused. It seems not part of the case syntax.

As @melpomene commented, the "of" is part of the case expression. See http://learnyouahaskell.com/syntax-in-functions#case-expressions for further reference. Also the case expression is contained in a lambda expression which I would point you to http://learnyouahaskell.com/higher-order-functions#lambdas for reference. LYAH was and continues to be a great source (at least for me).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM