[Haskell-cafe] Thompson's Exercise 9.13

Daniel Fischer daniel.is.fischer at web.de
Sun Apr 10 14:02:27 EDT 2005


Am Sonntag, 10. April 2005 19:46 schrieb Daniel Fischer:
> > so it doesn't work in all cases. The idea is nice, though:
> >
> > import Data.Maybe
> >
> > finit  = catMaybes . foldr f [Nothing] . map Just
> >   where
> >   f x ys = case ys of
> >                     [Nothing] -> []
> >                     _             -> x:ys
> >
> > will do fine.
>
> Ooooops, only for nonempty lists!

Replace catMaybes with 'map (maybe (error "init of []") id)'
and it'll work also for empty lists.


More information about the Haskell-Cafe mailing list