【发布时间】:2011-11-18 23:12:45
【问题描述】:
我已经简化了相关功能。我在单子中构建列表时遇到问题。我怀疑有优先级问题。
newtype Boundary = MkBoundary Integer
testFunc :: [Boundary] -> [Maybe Integer]
testFunc (MkBoundary x:xs)
| (even x) = Just x : testFunc xs
| otherwise = Nothing : testFunc xs
testFunc _ = []
这按预期工作。但我需要在单子中工作。我将在此示例中使用 IO
testFunc :: [Boundary] -> IO [Maybe Integer]
testFunc (MkBoundary x:xs)
| (even x) = return $ Just x : testFunc xs
| otherwise = return $ Nothing : testFunc xs
testFunc _ = []
无论我如何尝试操纵优先级,这都会中断。
test.hs:6:35:
Couldn't match expected type `[Maybe Integer]'
with actual type `IO [Maybe Integer]'
In the return type of a call of `testFunc'
In the second argument of `(:)', namely `testFunc xs'
In the second argument of `($)', namely `Just x : testFunc xs'
Failed, modules loaded: none.
我想要完成的是构建一个列表,然后将其返回给 IO。我做错了什么?
【问题讨论】: