Haskell map function with predicate
Posted
by
Paul
on Stack Overflow
See other posts from Stack Overflow
or by Paul
Published on 2011-02-12T19:29:17Z
Indexed on
2011/02/12
23:25 UTC
Read the original article
Hit count: 206
I feel like this should be fairly obvious, or easy, but I just can't get it. What I want to do is apply a function to a list (using map) but only if a condition is held. Imagine you only wanted to divide the numbers which were even:
map (`div` 2) (even) [1,2,3,4]
And that would give out [1,1,3,2] since only the even numbers would have the function applied to them. Obviously this doesn't work, but is there a way to make this work without having to write a seperate function that you can give to map? Filter is almost there, except I also want to keep the elements which the condition doesn't hold for, and just not apply the function to them.
Thanks
© Stack Overflow or respective owner