It seems simple just to fold on a part of list, then cons it to the rest of the list:
foldlN :: Int -> (a -> a -> a) -> [a] -> [a]
foldlN n f xs = (foldl1 f $ take n xs) : drop n xs
Result in REPL:
*Main> foldlN 3 (+) [1..5]
[6,4,5]
If you worry that taking and then dropping from the same list is a double job, you can rewrite with splitAt
:
foldlN n f xs = (foldl1 f $ fst t) : snd t
where t = splitAt n xs
Note the type of the function, you can't base your fold on foldl
, because you can't have a list [[1,2,3],4,5,6]
in Haskell.