on master help sign in
Add scanl and scanl1 implementations to List (#3406)
commiteb4f161e786dc06ec5df6ab4146fd8deae841cfe authorAnthony <faineance@users.noreply.github.com> dateSat 10 Sep 2016, 08:35 (10 years ago) parent6b90f7b
1 file changed+26
libs/prelude/Prelude/List.idr+26
libs/prelude/Prelude/List.idr+26
@@ -364,6 +364,32 @@ Foldable List where
364364toList : Foldable t => t a -> List a
365365toList = foldr (::) []
366366
367+--------------------------------------------------------------------------------
368+-- Scans
369+--------------------------------------------------------------------------------
370+
371+||| The scanl function is similar to foldl, but returns all the intermediate
372+||| accumulator states in the form of a list.
373+|||
374+|||````idris example
375+|||scanl (+) 0 [1,2,3,4]
376+|||````
377+scanl : (b -> a -> b) -> b -> List a -> List b
378+scanl f q [] = [q]
379+scanl f q (x::xs) = q :: scanl f (f q x) xs
380+
381+||| The scanl1 function is a variant of scanl that doesn't require an explicit
382+||| starting value.
383+||| It assumes the first element of the list to be the starting value and then
384+||| starts the fold with the element following it.
385+|||
386+|||````idris example
387+|||scanl1 (+) [1,2,3,4]
388+|||````
389+scanl1 : (a -> a -> a) -> List a -> List a
390+scanl1 _ [] = []
391+scanl1 f (x::xs) = scanl f x xs
392+
367393--------------------------------------------------------------------------------
368394-- Transformations
369395--------------------------------------------------------------------------------