我有一个功能
add2Maybe :: (Num a) => Maybe a -> Maybe a -> Maybe a
add2Maybe x y = do
n1 <- x
n2 <- y
return $ n1 + n2
Run Code Online (Sandbox Code Playgroud)
Just 8使用Just 3和调用时,它将成功返回Just 5。我想要的是调用此功能时,类似于C语言中的printf调试,打印
The number in x is 3. --(Or Nothing).
The number in y is 5.
Final result is 8.
Run Code Online (Sandbox Code Playgroud)
然后返回Just 8。
我得出的结论是,要定义这样的函数,我必须将Maybe monad与IO monad一起使用,但是的类型签名>>=不允许在一个函数中使用两个以上的monad。有没有办法将Maybe monad和IO monad结合起来,更一般地说,有没有办法结合两种或更多monad类型?
*Main> (negate.abs) -7
<interactive>:26:1: error:
? Non type-variable argument in the constraint: Num (c -> c)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall c. (Num (c -> c), Num c) => c -> c
Run Code Online (Sandbox Code Playgroud)
嗨,我是Haskell函数组合的一些错误和困难的提问者 ,现在我又遇到了麻烦.为什么这个简单的代码无效?这是不工作的代码列表:
(negate.abs) -7
negate (abs -7)
negate.abs -7
negate.abs $ 7 //only this works
Run Code Online (Sandbox Code Playgroud)
上面有相同的错误消息.我无法理解,所以我能得到一些帮助,说明为什么它不起作用,我该如何解决?而且我想了解错误消息的含义,以及为什么只有最后一个有效.谢谢.
f x = x + 3
g x = x * 3
Run Code Online (Sandbox Code Playgroud)
<interactive>:17:1: error:
? Non type-variable argument in the constraint: Num (a -> c)
(Use FlexibleContexts to permit this)
? When checking the inferred type
it :: forall c a. (Num (a -> c), Num c) => a -> c
Run Code Online (Sandbox Code Playgroud)
我的函数组合运算符出错了.为什么不起作用?f x工作,g x工作,甚至f(g x)工作,但f.g x不起作用.