英文:
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
(+) <$> a <*> b
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论