首页 > 解决方案 > Haskell 中是否有“of”语法?

问题描述

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))

在最后一行

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

of语法让我感到困惑。它似乎不是case语法的一部分。

标签: haskellsyntax

解决方案


正如@melpomene 评论的那样,“of”是 case 表达式的一部分。请参阅http://learnyouahaskell.com/syntax-in-functions#case-expressions以获取更多参考。case 表达式也包含在一个 lambda 表达式中,我会指向http://learnyouahaskell.com/higher-order-functions#lambdas以供参考。LYAH 过去和现在都是一个很好的来源(至少对我来说)。


推荐阅读