Haskell map function with predicate?

If you don't want to define separate function, then use lambda map (\x -> if (even x) then (x `div` 2) else x) 1,2,3,4 Or instead of a map, list comprehension, bit more readable I think if (even x) then (x `div` 2) else x | x.

If you don't want to define separate function, then use lambda. Map (\x -> if (even x) then (x `div` 2) else x) 1,2,3,4 Or instead of a map, list comprehension, bit more readable I think. If (even x) then (x `div` 2) else x | x.

Thanks, didn't think of using if/then/else and lambda. – Paul Feb 12 at 19:43 @PPiotrLegnica: In this case, list comprehension might be bad for performance, because it translate to concatMap which causes the creation of a superfluous layer of lists. – FUZxxl Feb 12 at 19:49 2 @FUZxxl: Run ghc -ddump-simpl, you'll see that no intermediary lists are created.

The default rewrite rules and other optimization passes are smarter than that. – ephemient Feb 12 at 20:07 Yes. I forgot about that.

GHC is damned smart, some times even so smart to remove the roadblocks I set to block his optimization... :) – FUZxxl Feb 12 at 20:09.

MapIf p f = map (\x -> if p x then f x else x).

1 Note that this can be made into a more general utility for Functor s, not just lists, if you change map to fmap in the above. – pelotom Feb 12 at 19:49.

In addition to the answer of PiotrLegnica: Often, it's easier to read if you declare a helper function instead of using a lambda. Consider this: map helper 1..4 where helper x | even x = x `div` 2 | otherwise = x (1..4 is sugar for 1,2,3,4) If you want to remove all the other elements instead, consider using filter. Filter removes all elements that don't satisfy the predicate: filter even 1..4 -> 2,4 So you can build a pipe of mapand filter than or use list-comprehension instead: map (`div` 2) $ filter even 1..4 x `div` 2 | x.

Make your own helper function maker: ifP pred f x = if pred x then f x else x custom_f = ifP even f map custom_f .. (caveat: I don't have access to a compiler right now. I guess this works OK...).

1 Personally I'd call this whenP, since if makes me think there should be an else case as well, but I like this solution. – ephemient Feb 12 at 20:08.

Mostly a rip off of existing answers, but according to my biased definition of "readable" (I like guards more than ifs, and where more than let): mapIf p f = map f' where f' x | p x = f x | otherwise = x ghci says it probably works ghci> let mapIf p f = map f' where f' x | p x = f x | otherwise = x ghci> mapIf even (+1) 1..10 1,3,3,5,5,7,7,9,9,11.

I like the other, more general solutions, but in your very special case you can get away with map (\x -> x `div` (2 - x `mod` 2)) 1..4.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions