[Haskell-beginners] Manipulate list, one element at a time

martin martin.drautzburg at web.de
Tue Oct 24 10:18:10 UTC 2017


Hello all,

How can I do something to each element of a list, but only modify one element at at time, so the result is a list of
lists, where each sublist is the original list with one element altered.

Something like

type Each a = (a->a) -> [a] -> [[a]]

I came up with:

each :: Each a
each f []     = []
each f (x:xs) = (f x : xs) : (map (x:) $  each f xs)

λ> each (*10) [1..3]
[[10,2,3],[1,20,3],[1,2,30]]


but I wonder if there is a more standard way of doing this


More information about the Beginners mailing list