Haskell IO function type mismatch -
what problem following code:
nextmatch :: (a -> bool) -> [io a] -> (io a, [io a]) nextmatch f (x:xs) = s <- x if f s (return x, xs) else nextmatch f xs the compile error says:
src\main.hs:364:10: couldn't match expected type `(io a, t0)' actual type `io a' in stmt of 'do' block: s <- x in expression: { s <- x; if f s (return x, xs) else nextmatch f xs } in equation `nextmatch': nextmatch f (x : xs) = { s <- x; if f s (return x, xs) else nextmatch f xs } i want function searches list match , returns matched element plus remaining list, tuple.
i still quite new haskell, problem might quite simple...
thanks! chris
as x if of type io a , no need re-return it, contrary tuple need injected monad.
if f s return $ (x, xs) but, not issue, io monad , signature doesn't reflect it, return type (io a, [io a]) should io (io a, [io a]). code should follow.
nextmatch :: (a -> bool) -> [io a] -> io (io a, [io a]) nextmatch f (x:xs) = done <- fmap f x if done return $ (x, xs) else nextmatch f xs anyway don't know trying signature of function pretty awkward. should more nextmatch :: (a -> bool) -> [a] -> (a, [a]), using return enough build monadic version of it. nextmatchio = return $ nextmatch , plug computation control flow using other function, provided control.monad.
Comments
Post a Comment