虽然我可以应用一个函数两次并将结果绑定到一个元组中:
let foo :: Num a => a -> a
foo x = x + 1
let (x,y) = (foo 10, foo 20)
do
这不能在一个块内完成(至少我不知道如何正确地做到这一点) :
let bar :: Num a => a -> IO a
bar x = do
let y = x + 1
return y
let test :: Num a => IO a
test = do
(x,y) <- (bar 10, bar 20)
return y
输入 GHCI REPL 时出现以下错误:
:29:15:
Couldn't match expected type ‘IO a1’ with actual type ‘(t0, a)’
Relevant bindings include
test :: IO a (bound at :28:5)
In the pattern: (x, y)
In a stmt of a 'do' block: (x, y) <- (bar 10, bar 20)
In the expression:
do { (x, y) <- (bar 10, bar 20);
return y }
:29:24:
Couldn't match type ‘(,) (IO a0)’ with ‘IO’
Expected type: IO (IO a1)
Actual type: (IO a0, IO a1)
In a stmt of a 'do' block: (x, y) <- (bar 10, bar 20)
In the expression:
do { (x, y) <- (bar 10, bar 20);
return y }
In an equation for ‘test’:
test
= do { (x, y) <- (bar 10, bar 20);
return y }
我显然可以用更详细的方法来解决它:
let test' :: Num a => IO a
test' = do
x <- bar 10
y <- bar 20
return y
有没有一种正确的表达方式test
而不是让它喜欢test'
?