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: A Question about withFile (Erlend Hamberg)
2. Very little (golfing) question... (Corentin Dupont)
3. Re: Very little (golfing) question... (David McBride)
4. Re: Very little (golfing) question... (Brent Yorgey)
5. Re: Very little (golfing) question... (Corentin Dupont)
----------------------------------------------------------------------
Message: 1
Date: Wed, 25 Jul 2012 12:44:51 +0200
From: Erlend Hamberg <[email protected]>
Subject: Re: [Haskell-beginners] A Question about withFile
To: Bin Shi <[email protected]>
Cc: [email protected]
Message-ID:
<ca+g9oxk3nvnot9aebag5msm6wsahapzqzyhvrvd8jn1eufk...@mail.gmail.com>
Content-Type: text/plain; charset=windows-1252
On 25 July 2012 11:40, Bin Shi <[email protected]> wrote:
> but if I changed to
>
> main = do
> c <- withFile "a.txt" ReadMode hGetContents
> putStrLn c
>
> I got just a empty line.
>
> Where am I wrong?
If we look at the type of ?withFile? we see that it is
withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
The important part here is (Handle -> IO r), i.e. a function that
takes a handle as an argument and produces a result of IO r. If we
de-sugar the do-notation in your first example we get
withFile "a.txt" ReadMode (\h -> hGetContents h >>= \c -> putStrLn c)
Here we see that the ?processing function? passed to withFile is (\h
-> hGetContents h >>= \c -> putStrLn c). In your second example, we
get the following:
withFile "a.txt" ReadMode hGetContents >>= \c -> putStrLn c
which is equivalent to
(withFile "a.txt" ReadMode hGetContents) >>= \c -> putStrLn c
hGetContents is lazy and won't start reading until its needed. This,
together with the fact that withFile guarantees that the file handle
is closed after it's finished leads to the whole ?withFile? call
returning "" which is then printed by putStrLn.
--
Erlend Hamberg
[email protected]
------------------------------
Message: 2
Date: Wed, 25 Jul 2012 23:02:39 +0200
From: Corentin Dupont <[email protected]>
Subject: [Haskell-beginners] Very little (golfing) question...
To: [email protected]
Message-ID:
<caeyhvmqsezxkztwah2xxmddcpvtw5i3afo8uz1_erd0fmoj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
I always write functions like this:
addPlayer :: PlayerInfo -> State Game Bool
addPlayer pi@(PlayerInfo {playerNumber = pn}) = do
pls <- gets players
case find (\(PlayerInfo {playerNumber = myPn}) -> pn == myPn) pls of
Nothing -> do
modify (\game -> game { players = pi : pls})
return True
otherwise -> return False
It simply adds a new PlayerInfo to a list contained in Game, with the
condition that it doesn't exists already.
Do you see a more elegant way to do this?
Best,
Corentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120725/23482284/attachment-0001.htm>
------------------------------
Message: 3
Date: Wed, 25 Jul 2012 18:27:11 -0400
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] Very little (golfing) question...
To: Corentin Dupont <[email protected]>
Cc: [email protected]
Message-ID:
<can+tr40zozfm-f5dfvow-1w66hsalgsnaijs1ioq-67wg+n...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
This is marginally clearer, but not all that great.
import Data.List as L
import Control.Monad.State
import Data.Maybe (isJust)
data PlayerInfo = PlayerInfo {
playerNumber :: Integer
}
instance Eq PlayerInfo where
p1 == p2 = playerNumber p1 == playerNumber p2
data Game = Game {
players :: [PlayerInfo]
}
addPlayer newPlayer = do
exists <- fmap (isJust . find (==newPlayer)) $ gets players
when exists $ gets players >>= \oldPlayers -> modify (\game -> game
{ players = newPlayer:oldPlayers })
return exists
So, add an Eq instance to players so that you can just compare them
simply without deconstructing them all the time.
However, if you were to give an Ord instance to players, you could
replace lists with sets, which will allow you to insert without caring
whether the player is already in the set. If you are dead set on
getting back a boolean, then you'll still have to check for existence
with member, so you won't gain much in brevity. But if you weren't,
you could shorten it considerably to:
import Control.Monad.State
import Data.Set as S
data PlayerInfo = PlayerInfo {
playerNumber :: Integer
} deriving Ord
instance Eq PlayerInfo where
p1 == p2 = playerNumber p1 == playerNumber p2
data Game = Game {
players :: Set PlayerInfo
}
addPlayer newPlayer = modify $
\game@(Game { players = oldPlayers }) -> game { players = S.insert
newPlayer oldPlayers }
On Wed, Jul 25, 2012 at 5:02 PM, Corentin Dupont
<[email protected]> wrote:
>
> Hi,
> I always write functions like this:
>
> addPlayer :: PlayerInfo -> State Game Bool
> addPlayer pi@(PlayerInfo {playerNumber = pn}) = do
> pls <- gets players
> case find (\(PlayerInfo {playerNumber = myPn}) -> pn == myPn) pls of
> Nothing -> do
> modify (\game -> game { players = pi : pls})
> return True
> otherwise -> return False
>
> It simply adds a new PlayerInfo to a list contained in Game, with the
> condition that it doesn't exists already.
> Do you see a more elegant way to do this?
>
> Best,
> Corentin
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Wed, 25 Jul 2012 20:31:26 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Very little (golfing) question...
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Jul 25, 2012 at 11:02:39PM +0200, Corentin Dupont wrote:
>
This is a little better, it avoids all the record syntax for
extracting player numbers, and uses 'any' instead of 'find' since we
don't actually care about the player with the given number if they
exist:
> addPlayer :: PlayerInfo -> State Game Bool
> addPlayer pi = do
> pls <- gets players
> let exists = any (((==) `on` playerNumber) pi) pls
> when (not exists) $ modify (\game -> game { players = pi : pls})
> return $ not exists
-Brent
------------------------------
Message: 5
Date: Thu, 26 Jul 2012 11:23:36 +0200
From: Corentin Dupont <[email protected]>
Subject: Re: [Haskell-beginners] Very little (golfing) question...
To: Brent Yorgey <[email protected]>, [email protected]
Cc: [email protected]
Message-ID:
<CAEyhvmr8Zk4oU=7qU7bWzCp7=n8rwmn48aqjspxwe+qkuod...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
Thanks Brent and David, that's very neat!
I always forget to use these operators 'on', 'when', 'any'...
I also always hesitate on the container to use, list, sets or something
else... Now everything is done with lists since they are very well
supported by the prelude...
On Thu, Jul 26, 2012 at 2:31 AM, Brent Yorgey <[email protected]>wrote:
> On Wed, Jul 25, 2012 at 11:02:39PM +0200, Corentin Dupont wrote:
> >
>
> This is a little better, it avoids all the record syntax for
> extracting player numbers, and uses 'any' instead of 'find' since we
> don't actually care about the player with the given number if they
> exist:
>
> > addPlayer :: PlayerInfo -> State Game Bool
> > addPlayer pi = do
> > pls <- gets players
> > let exists = any (((==) `on` playerNumber) pi) pls
> > when (not exists) $ modify (\game -> game { players = pi : pls})
> > return $ not exists
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120726/261f5cbe/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 49, Issue 28
*****************************************