【发布时间】:2013-05-04 04:12:33
【问题描述】:
以下代码有什么问题:
nextMatch :: (a -> Bool) -> [IO a] -> (IO a, [IO a])
nextMatch f (x:xs) = do
s <- x
if f s then (return x, xs)
else nextMatch f xs
编译错误说:
src\Main.hs:364:10:
Couldn't match expected type `(IO a, t0)' with actual type `IO a'
In a stmt of a 'do' block: s <- x
In the expression:
do { s <- x;
if f s then (return x, xs) else nextMatch f xs }
In an equation for `nextMatch':
nextMatch f (x : xs)
= do { s <- x;
if f s then (return x, xs) else nextMatch f xs }
我想要一个函数,它在列表中搜索匹配项并将匹配的元素和剩余的列表作为元组返回。
我对haskell还是很陌生,所以这个问题可能很简单......
谢谢! 克里斯
【问题讨论】:
-
一般性评论——一元论点(例如
IO [a],或您的[IO a])几乎总是一个坏主意。不纯代码通常采用纯参数,使用 do-notation 或>>=将它们应用到一元值。
标签: haskell io functional-programming tuples monads