如何将二进制函数提升到单子变换器?

huangapple go评论60阅读模式
英文:

How do you lift a binary function to monad transformers?

问题

我知道你可以使用liftM将函数提升到单子中,但似乎对于二元函数不起作用。

我尝试提升 (+),但它不起作用。

a = return 1 :: ExceptT String Maybe Int
b = return 2 :: ExceptT String Maybe Int
liftM (+) a b

 无法匹配预期类型 ExceptT String Maybe Int -> t
                  实际类型 ExceptT String Maybe (Int -> Int)
英文:

I know you can lift functions to monads with liftM, but I it doesn't seem to work with binary functions.

I tried to lift (+) but it didn't work

a = return 1 :: ExceptT String Maybe Int
b = return 2 :: ExceptT String Maybe Int
liftM (+) a b

• Couldn't match expected type ‘ExceptT String Maybe Int -> t’
                  with actual type ‘ExceptT String Maybe (Int -> Int)’

答案1

得分: 1

liftM2 (+) a b
liftA2 (+) a b
(+) <$> a <*> b

英文:

You can use one of these:

liftM2 (+) a b
liftA2 (+) a b
(+) &lt;$&gt; a &lt;*&gt; b

huangapple
  • 本文由 发表于 2023年1月9日 01:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定