haskell - Can mapEvery be implemented with foldr -
For a function that maps the function to each NTH element in a list:
< Code> mapEvery :: int - & gt; (A -> A) - & gt; [A] - & gt; [A] mapEvery nf = zipWith ($) (drop 1 cycle. N $ f le: repeat id)Is it possible to implement it with
foldr Like normal
map ?
Edit: In the title, 'folder' has been changed to 'folder' Autocorrect ...
Here's a solution
mapEvery :: Int - & gt; (A -> A) - & gt; [A] - & gt; [A] The map in the form of every F = go plateau (const []) as 1 as an M go m == n = f a: as 1. Otherwise, as a = A: (M + 1)
it uses "
foldl as
foldr " as On the right side of the list as you fold Basically, if we type the
foldr to
(a -> gt - r -> gt) - & gt; R - & gt; [A] - & gt; R then we do
r as
int -> gt; [A] Where the integer we pass is the current number of elements which we have passed without calling the function.
Comments
Post a Comment