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. error monad and Map lookup (Dennis Raddle)
2. Re: error monad and Map lookup (Brandon Allbery)
3. Re: error monad and Map lookup (Daniel Fischer)
4. Re: error monad and Map lookup (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Thu, 22 Dec 2011 11:44:26 -0800
From: Dennis Raddle <[email protected]>
Subject: [Haskell-beginners] error monad and Map lookup
To: Haskell Beginners <[email protected]>
Message-ID:
<CAKxLvop1vUtiyLKMZJvD=9dUVmgRZ+45gLDbPKeFA=W=jsj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Is there a more elegant way to write 'test' in the following?
import qualified Data.Map as M
import Data.Map(Map)
import Control.Monad.Error
testMap :: Map Int String
testMap = M.fromList [(1, "whatever"), (2, "dude")]
test :: Int -> Either String String
test x = do
y <- case M.lookup x testMap of
Nothing -> throwError "not in map"
Just z -> return z
return y
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20111222/d1b43a93/attachment-0001.htm>
------------------------------
Message: 2
Date: Thu, 22 Dec 2011 15:04:19 -0500
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] error monad and Map lookup
To: Dennis Raddle <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID:
<CAKFCL4U5wbKFM=WXS1F4i+rAS=hvgpm1k7fqn1oykb_sqtw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Thu, Dec 22, 2011 at 14:44, Dennis Raddle <[email protected]>wrote:
> test :: Int -> Either String String
> test x = do
> y <- case M.lookup x testMap of
> Nothing -> throwError "not in map"
> Just z -> return z
> return y
>
> test x = maybe (Left "not in map") Right $ M.lookup x testMap
--
brandon s allbery [email protected]
wandering unix systems administrator (available) (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20111222/c9f7107e/attachment-0001.htm>
------------------------------
Message: 3
Date: Thu, 22 Dec 2011 21:06:09 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] error monad and Map lookup
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Thursday 22 December 2011, 20:44:26, Dennis Raddle wrote:
> Is there a more elegant way to write 'test' in the following?
>
> import qualified Data.Map as M
> import Data.Map(Map)
> import Control.Monad.Error
>
> testMap :: Map Int String
> testMap = M.fromList [(1, "whatever"), (2, "dude")]
>
> test :: Int -> Either String String
> test x = do
> y <- case M.lookup x testMap of
> Nothing -> throwError "not in map"
> Just z -> return z
> return y
test = maybe (throwError "not in map") return . flip M.lookup testMap
------------------------------
Message: 4
Date: Thu, 22 Dec 2011 22:06:16 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] error monad and Map lookup
To: "Haskell Beginners" <[email protected]>, "Dennis Raddle"
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Thu, 22 Dec 2011 20:44:26 +0100, Dennis Raddle
<[email protected]> wrote:
> Is there a more elegant way to write 'test' in the following?
>
> import qualified Data.Map as M
> import Data.Map(Map)
> import Control.Monad.Error
>
> testMap :: Map Int String
> testMap = M.fromList [(1, "whatever"), (2, "dude")]
>
> test :: Int -> Either String String
> test x = do
> y <- case M.lookup x testMap of
> Nothing -> throwError "not in map"
> Just z -> return z
> return y
If you run hlint (available on Hackage), you get the following message:
MonadicCase.hs:11:10: Error: Redundant return
Found:
do y <- case M.lookup x testMap of
Nothing -> throwError "not in map"
Just z -> return z
return y
Why not:
do case M.lookup x testMap of
Nothing -> throwError "not in map"
Just z -> return z
And of course, you can leave out the 'do'; the test function then becomes:
test x =
case M.lookup x testMap of
Nothing -> throwError "not in map"
Just z -> return z
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 42, Issue 25
*****************************************