haskell *> 排序运算符

max*_*loo 2 haskell sequencing

我在回答我的问题时遇到了一个问题: haskell 要么和 Validation Applicative

我的代码贴在那里。

它涉及使用*>排序运算符而不是<*>应用运算符。

根据https://hackage.haskell.org/package/base-4.15.0.0/docs/Control-Applicative.html#v:-42--62-上的解释,我理解对*>动作进行排序,丢弃的值第一个论点。所以对于我的代码,我已经尝试过fail6 = fail2 *> success,它可以工作,但它不应该工作,因为第一个参数的值,即fail2,应该被丢弃。为什么fail6有效?

的输出fail6Failure [MooglesChewedWires,StackOverflow]

Wil*_*sem 5

与“丢弃结果”,它意味着一个应用程序的结果。所以对于 anEither意味着 a Right y(*>)因此相当于:

(*>) :: Applicative f => f a -> f b -> f b
(*>) f g = liftA2 (const id) f g
Run Code Online (Sandbox Code Playgroud)

或以另一种形式:

(*>) :: Applicative f => f a -> f b -> f b
(*>) f g = (const id) <$> f <*> g
Run Code Online (Sandbox Code Playgroud)

因此它运行两个动作并返回第二个结果,但这在“Applicative上下文”中。

例如对于Either,这是实现为:

instance Applicative (Either a) where
    pure = Right
    Left e <*> _ = Left e
    _ <*> Left e = Left e
    Right f <*> Right x = Right (f x)
Run Code Online (Sandbox Code Playgroud)

因此,这意味着(*>)Eitheras实现:

-- (*>) for Either
(*>) :: Either a b -> Either a c -> Either a c
Left x *> _ = Left x
_ *> Left x = Left x
Right _ *> Right x = Right x
Run Code Online (Sandbox Code Playgroud)

或同等学历:

-- (*>) for Either
(*>) :: Either a b -> Either a c -> Either a c
Left x *> _ = Left x
_ *> x = x
Run Code Online (Sandbox Code Playgroud)

如果第一个操作数是 a Right …,则返回第二个操作数,如果第一个操作数是Left x,则返回Left x