是否有一个「;“的;Haskell中的语法?
在最后一行是否有一个「;“的;Haskell中的语法?,haskell,syntax,Haskell,Syntax,在最后一行 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) = StateO
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))
的语法让我感到困惑。它似乎不是case
语法的一部分。正如@melpomene所评论的,“of”是case表达式的一部分。有关更多参考,请参阅。此外,case表达式包含在lambda表达式中,我将向您指出该表达式以供参考。LYAH过去是,现在仍然是一个伟大的来源(至少对我来说)。但它是案例的一部分。为什么你不这么认为?下面是所有关键字的列表:全括号,它是{(s1,a)->(s1,fa)}
的case(stfs0)。可能会更清楚一点。我投票结束这个问题,因为它与基本语言语法有关。阅读文档可能是一个很好的开始。顺便说一下,您可以对新类型使用记录语法:newtype State sa=StateOf{deState::s->(s,a)}
。然后您不需要单独定义deState
。
fmap f (StateOf stf) = StateOf (\s0 -> case stf s0 of (s1, a) -> (s1, f a))