on master help sign in
Tail recrusive foldr for List
commit5e134bd6f02f11c9887c6abe9cac3c4885095d61 authorJan Bessai <Jan.Bessai@tu-dortmund.de> dateTue 11 Mar 2014, 18:41 (13 years ago) parent191ad06
1 file changed+5−2
libs/prelude/Prelude/List.idr+5−2
libs/prelude/Prelude/List.idr+5−2
@@ -236,9 +236,12 @@ mapMaybe f (x::xs) =
236236-- Folds
237237--------------------------------------------------------------------------------
238238
239+total foldrImpl : (t -> acc -> acc) -> acc -> (acc -> acc) -> List t -> acc
240+foldrImpl f e go [] = go e
241+foldrImpl f e go (x::xs) = foldrImpl f e (go . (f x)) xs
242+
239243instance Foldable List where
240− foldr f e [] = e
241− foldr f e (x::xs) = f x (foldr f e xs)
244+ foldr f e xs = foldrImpl f e id xs
242245
243246--------------------------------------------------------------------------------
244247-- Special folds