当我想在 haskell 中应用函数组合(使用 monads)时,如何使用 monad 中的 <=< 运算符?



我是Haskell语言的新手。 我刚刚开始使用Monads,O根本无法弄清楚它是如何工作的。 我试着从 也许像这样称呼<=<monad :(x->Just (x+1)) <=< (y -> Just (y *2)) $8期待结果Just 17. 但它显示了错误:

<interactive>:45:19: error:
* Variable not in scope:
(<=>)
:: (Integer -> Maybe Integer)
-> (Integer -> Maybe Integer) -> Integer -> t
* Perhaps you meant one of these:
`<>' (imported from Prelude), `<*>' (imported from Prelude),
`<=' (imported from Prelude)

这是代码:

(<=<) :: (a -> Maybe b) -> (c -> Maybe a) -> c -> Maybe b
f <=< g = ( x -> g x >>= f)

该运算符在Prelude中不可用,您需要从Control.Monad导入。例如,将其添加到文件顶部:

import Control.Monad ((<=<))

相关内容

最新更新