Adding scanl'

Henning Thielemann lemming at henning-thielemann.de
Tue Sep 11 20:43:42 CEST 2012


On Tue, 11 Sep 2012, Niklas Hambüchen wrote:

> I would like to propose adding a the functions scanl' and scanl1' to
> Data.List.
>
> The presence and regular use of foldl' and foldl1' suggests that
> corresponding scan functions should be available.
>
>> scanl'                   :: (a -> b -> a) -> a -> [b] -> [a]
>> scanl' f q ls            =  q `seq` (q : (case ls of
>>                                      []   -> []
>>                                      x:xs -> scanl' f (f q x) xs))
>
> What do you think?

I don't know whether this "scanl'" is of much use. "foldl'" is required 
because we can access its accumulator only after "foldl'" finished. But in 
"scanl'" you can and usually should access the interim accumulator values 
that are contained in the result list.



More information about the Libraries mailing list