[Haskell-cafe] Generalizing takeWhile

Jason Dagit dagit at codersbase.com
Wed Jul 22 12:45:16 EDT 2009


2009/7/22 Matthias Görgens <matthias.goergens at googlemail.com>

> I need to take some elements from the front of a list.  However the
> criteria are somewhat complex.
>
> > walk f [] = []
> > walk f (x:xs) = case f x
> >                 of Just g -> x : walk g xs
> >                    Nothing -> []
>
> For each item the `predicate' f either returns Nothing, when it thinks
> we should not take any more elements, or return Just another
> `predicate' to apply to the next element.
>
> However the type system does not like my function.  How can I mollify it?


At a quick glance it looks to me like the type of f is infinite.  Something
like:
f :: a -> Maybe (a -> Maybe (a -> ...))

When I've seen people solve similar problems in the past they have
introduced a data type to hide the infinite type.  See for example, the
solution to defining fix:
http://blog.plover.com/prog/springschool95-2.html

I didn't actual read that article, but it appears to explain the technique.

Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090722/f287490e/attachment.html


More information about the Haskell-Cafe mailing list