[Haskell-beginners] dropWhile returning last dropped element

Michael Orlitzky michael at orlitzky.com
Sat Apr 18 23:21:24 UTC 2015


On 04/18/2015 01:10 PM, martin wrote:
> Am 04/17/2015 um 11:18 PM schrieb Michael Orlitzky:
> 
>> Why do you want to avoid recursion? You could rewrite this to use list indices if you really wanted to, but anything
>> you come up with is going to be essentially recursive, only less safe
> 
> Thanks for the code sample and pointing out that there may not be any last dropped element.
> 
> I was wondering if there is  to achive the desired behavior by plugging together higher-order functions. This was the
> only reason why I wanted to avoid explicit recursion.
> 

Sure. Whenever you're processing a list and building up a return value,
it's probably a (left) fold. But a fold would pointlessly process the
rest of the list after it had stopped dropping elements, violating one
of your criteria. And foldl is of course implemented recursively =)

A "short-circuiting" version of foldl might exist in some library, but
there are a few ways I can think of to implement it, so it might be hard
to find.


More information about the Beginners mailing list