[Haskell-beginners] return IO () vs. ()

Antoine Latter aslatter at gmail.com
Sun May 29 03:33:55 CEST 2011


On Sat, May 28, 2011 at 4:35 PM, Neil Jensen <neilcjensen at gmail.com> wrote:
> I've been attempting to refactor some working code and running into
> confusion about returning IO values.
>
> The basic sequence is to query a database, calculate some values, and then
> store the results back in the database.
>
> The function which does the querying of the db and calculating results has
> the following type signature:
> calcCUVs :: AccountId -> IO [((ISODateInt, ISODateInt), CUV)]
>
> This function stores the results back into the database
> saveCUVs :: AccountId -> [((ISODateInt, ISODateInt), CUV)] -> IO ()
> saveCUVs account cuvs = do
>             r' <- mapM (\x -> storeCUV (snd $ fst x) account (snd x)) cuvs
>             return ()
>
>
> I had a working variation of the below using 'do' notation, but for some
> reason when I moved to using bind, I'm getting messed up with return values.
>
> processAccountCUVs :: AccountId -> ISODateInt -> ISODateInt -> IO ()
> processAccountCUVs account prevMonthEnd monthEnd = -- do
>                    if (prevMonthEnd == 0 && monthEnd == 0)
>                        then calcCUVs account >>=  (\cuvs -> saveCUVs account
> cuvs) >>= return ()
>                        else calcCUVs account prevMonthEnd monthEnd >>=
> (\cuvs -> saveCUVs account cuvs) >>= return ()
>
>

I think you might have your parenthesis in the wrong spot.

Instead of

> x >>= (\a -> y a) >> z

you probably want:

> x >>= (\a -> y a >> z)

Does that make sense?

Antoine



More information about the Beginners mailing list