[Haskell-beginners] Critique my program

Daniel Fischer daniel.is.fischer at web.de
Sun Jan 10 15:33:18 EST 2010

Am Sonntag 10 Januar 2010 20:44:38 schrieb David Frey:
> A couple of months ago, I was tutoring my cousin in an introductory
> computer science class.  We talked about his assignment in which he had
> to implement a subset of the Battleship board game using Java.
> I decided it would be fun for me to try to complete the assignment in
> Haskell. I forgot about the project for a while, but I picked it up and
> finished it today.
> I have put the code on BitBucket (a Mercurial based code hosting site
> for those who don't know) at this location:
> http://bitbucket.org/dfrey/battleship/
> I am hoping that some people from this list could review my code.  I'm
> not very concerned with correctness.  I'm more interested in how my code
> can be clarified through use of existing functions or standard Haskell
> idioms.

I prefer to have e.g.

data BoardLocation
    = BoardLocation
    { locationRow :: Int
    , locationCol :: Int
      deriving (Show, Eq)

but that's a matter of taste.

One thing that I dislike is

unsignedInteger = many1 digit >>= \x -> return $ read x

better is

unsignedInteger = many1 digit >>= return . read

, but

m >>= return . f

isn't good, that should be

fmap f m


liftM f m

, so I'd recommend

unsignedInteger = fmap read (many1 digit)

or, using Control.Applicative,

unsignedInteger = read <$> many1 digit

signedInteger could be

signedInteger = do
    signFun <- option id (char '-' >> return negate)
    fmap signFun unsignedInteger

it might be good to be more lenient with the format and allow more than one 
space between items (and trailing whitespace for user input).

allPairs (x:xs) = [(x,y) | y <- xs] ++ allPairs xs

> If you don't have Mercurial installed or don't want to clone the
> repository, you can browse the source here:
> http://bitbucket.org/dfrey/battleship/src/
> The original PDF from my cousin's assignment can be viewed here:
> http://bitbucket.org/dfrey/battleship/raw/81a01574f164/Assn4.pdf
> Feel free to submit your comments in any format.  Patches, written
> comments, complete re-writes of the program, etc. are all welcome.
> Thanks,
> David Frey

More information about the Beginners mailing list