[Haskell-cafe] lazy logging (was: Are there standard idioms for lazy, pure error handling?)

Daniel Fischer daniel.is.fischer at web.de
Tue Dec 1 15:00:13 EST 2009


Am Dienstag 01 Dezember 2009 20:21:27 schrieb Evan Laforge:
> This is only peripherally related, but I also have a lot of list
> functions that can possibly be an error, but usually processing can
> continue.  So they tend to return [Either Error Result].  I have
> another function thus:
>
> -- A foldr version is not lazy enough and overflows the stack.

try

foldr (\e ~(ls,rs) -> case e of { Left l -> (l:ls,rs); Right r -> (ls,r:rs) }) ([],[])

with the lazy pattern, it should be lazy enough.

> partition_either [] = ([], [])
> partition_either (x:xs) =
>     let (ls, rs) = partition_either xs
>     in case x of
>         Left l -> (l:ls, rs)
>         Right r -> (ls, r:rs)
>
> I was a little surprised I couldn't find this in the standard
> library... or maybe it is?

Data.Either.partitionEithers

Data.List.partition isLeft

isLeft (Left _) = True
isLeft _ = False




More information about the Haskell-Cafe mailing list