Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: return IO () vs. () (Neil Jensen)
2. Re: return IO () vs. () (Daniel Fischer)
3. Re: return IO () vs. () (Neil Jensen)
4. Re: return IO () vs. () (Antoine Latter)
5. question about round function (Prashant Shah)
----------------------------------------------------------------------
Message: 1
Date: Sat, 28 May 2011 15:04:36 -0700
From: Neil Jensen <[email protected]>
Subject: Re: [Haskell-beginners] return IO () vs. ()
To: Alex Rozenshteyn <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi Alex, thanks, I tried that, as well as removing the 'return' entirely,
but in both cases I ended up with the following:
Couldn't match expected type `ISODateInt -> ISODateInt -> IO a'
against inferred type `IO [((ISODateInt, ISODateInt), CUV)]'
In the first argument of `(>>=)', namely
`calcCUVs account prevMonthEnd monthEnd'
In the first argument of `(>>)', namely
`calcCUVs account prevMonthEnd monthEnd
>>=
(\ cuvs -> saveCUVs account cuvs)'
In the expression:
calcCUVs account prevMonthEnd monthEnd
>>=
(\ cuvs -> saveCUVs account cuvs)
>>
return ()
On Sat, May 28, 2011 at 2:50 PM, Alex Rozenshteyn <[email protected]>wrote:
> The thing that I see as weird is (>>= return ()), since the right argument
> to (>>=) is supposed to be a function.
>
> Maybe you want (>>)
>
> On Sat, May 28, 2011 at 5:35 PM, Neil Jensen <[email protected]>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 ()
>>
>>
>> The compiler gives the following error message:
>>
>> Couldn't match expected type `IO ()' against inferred type `()'
>> In the first argument of `return', namely `()'
>> In the second argument of `(>>=)', namely `return ()'
>> In the expression:
>> calcCUVs account >>= (\ cuvs -> saveCUVs account cuvs) >>=
>> return ()
>>
>>
>> I thought the last return () would correctly return us IO () as we are in
>> the IO monad... what am I missing?
>>
>> Thanks for any input you can provide.
>> Neil
>>
>>
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
>
> --
> Alex R
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110528/ddc2e8db/attachment-0001.htm>
------------------------------
Message: 2
Date: Sun, 29 May 2011 00:11:16 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] return IO () vs. ()
To: [email protected]
Cc: Neil Jensen <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Saturday 28 May 2011 23:35:11, Neil Jensen 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 ()
You're not using the result of mapM, so you should use mapM_ here, if the
list is long or the results of storeCUV are large, it's also more
efficient.
saveCUVs account cuvs
= mapM_ (\x -> storeCUV (snd $ fst x) account (snd x)) cuvs
or, avoiding the snd and fst by taking the argument apart via pattern-
matching,
saveCUVs account cuvs
= mapM_ (\((_,date),cuv) -> storeCUV date account cuv) cuvs
>
>
> 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 ()
The second argument of (>>=) must be a function, here of type (a -> IO b),
... >> return () or ... >>= return would typecheck, but the latter doesn't
make any sense, since 'action >>= return' is the same as plain 'action'.
The latter is only of use to finally get the right result type, which is
what you're doing here.
> else calcCUVs account prevMonthEnd monthEnd >>=
No, that can't be. calcCUVs takes one argument and returns an IO [...],
here you pass it three.
> (\cuvs -> saveCUVs account cuvs) >>= return ()
>
>
> The compiler gives the following error message:
>
> Couldn't match expected type `IO ()' against inferred type `()'
> In the first argument of `return', namely `()'
> In the second argument of `(>>=)', namely `return ()'
> In the expression:
> calcCUVs account >>= (\ cuvs -> saveCUVs account cuvs) >>=
> return ()
>
>
> I thought the last return () would correctly return us IO () as we are
> in the IO monad... what am I missing?
>
> Thanks for any input you can provide.
> Neil
------------------------------
Message: 3
Date: Sat, 28 May 2011 16:37:58 -0700
From: Neil Jensen <[email protected]>
Subject: Re: [Haskell-beginners] return IO () vs. ()
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks Daniel, correcting the function call to calcCUVs did the trick.
On Sat, May 28, 2011 at 3:11 PM, Daniel Fischer <
[email protected]> wrote:
> On Saturday 28 May 2011 23:35:11, Neil Jensen 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 ()
>
> You're not using the result of mapM, so you should use mapM_ here, if the
> list is long or the results of storeCUV are large, it's also more
> efficient.
>
> saveCUVs account cuvs
> = mapM_ (\x -> storeCUV (snd $ fst x) account (snd x)) cuvs
>
> or, avoiding the snd and fst by taking the argument apart via pattern-
> matching,
>
> saveCUVs account cuvs
> = mapM_ (\((_,date),cuv) -> storeCUV date account cuv) cuvs
>
> >
> >
> > 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 ()
>
> The second argument of (>>=) must be a function, here of type (a -> IO b),
> ... >> return () or ... >>= return would typecheck, but the latter doesn't
> make any sense, since 'action >>= return' is the same as plain 'action'.
> The latter is only of use to finally get the right result type, which is
> what you're doing here.
>
> > else calcCUVs account prevMonthEnd monthEnd >>=
>
> No, that can't be. calcCUVs takes one argument and returns an IO [...],
> here you pass it three.
>
> > (\cuvs -> saveCUVs account cuvs) >>= return ()
> >
> >
> > The compiler gives the following error message:
> >
> > Couldn't match expected type `IO ()' against inferred type `()'
> > In the first argument of `return', namely `()'
> > In the second argument of `(>>=)', namely `return ()'
> > In the expression:
> > calcCUVs account >>= (\ cuvs -> saveCUVs account cuvs) >>=
> > return ()
> >
> >
> > I thought the last return () would correctly return us IO () as we are
> > in the IO monad... what am I missing?
> >
> > Thanks for any input you can provide.
> > Neil
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110528/a77dc0a2/attachment-0001.htm>
------------------------------
Message: 4
Date: Sat, 28 May 2011 20:33:55 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] return IO () vs. ()
To: Neil Jensen <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sat, May 28, 2011 at 4:35 PM, Neil Jensen <[email protected]> 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
------------------------------
Message: 5
Date: Sun, 29 May 2011 12:09:27 +0530
From: Prashant Shah <[email protected]>
Subject: [Haskell-beginners] question about round function
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi,
This is my first time with Haskell.
Here is what I found while trying out some examples :
Prelude> round 3.499999999999999
3
it :: Integer
Prelude> round 3.4999999999999999
4
it :: Integer
---------------
Prelude> round 111111.49999999999
111111
it :: Integer
Prelude> round 111111.499999999999
111112
it :: Integer
Is it something to do with the CPU register size ?
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 57
*****************************************