Haskell Language

zipWithM

Introduction#

zipWithM is to zipWith as mapM is to map: it lets you combine two lists using a monadic function.

From the module Control.Monad

Syntax#

  • zipWithM :: Applicative m => (a -> b -> m c) -> [a] -> [b] -> m [c]

Calculatings sales prices

Suppose you want to see if a certain set of sales prices makes sense for a store.

The items originally cost $5, so you don’t want to accept the sale if the sales price is less for any of them, but you do want to know what the new price is otherwise.

Calculating one price is easy: you calculate the sales price, and return Nothing if you don’t get a profit:

calculateOne :: Double -> Double -> Maybe Double
calculateOne price percent = let newPrice = price*(percent/100)
                             in if newPrice < 5 then Nothing else Just newPrice

To calculate it for the entire sale, zipWithM makes it really simple:

calculateAllPrices :: [Double] -> [Double] -> Maybe [Double]
calculateAllPrices prices percents = zipWithM calculateOne prices percents

This will return Nothing if any of the sales prices are below $5.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow