[Haskell-beginners] Very little (golfing) question...

Corentin Dupont corentin.dupont at gmail.com
Wed Jul 25 23:02:39 CEST 2012


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.htm>


More information about the Beginners mailing list