Help with a Shootout program

Bayley, Alistair Alistair_Bayley at
Fri Feb 25 11:34:40 EST 2005

Here's your program, but a little slimmer. I think size counts for something
in the shootout. Still runs at the same speed though :-(

The program seems to spend about 50% reading the files, 22% building the
hash table (7% chopping input into lines, 11% inserting into table), 25%
spellchecking (11% chopping input into lines, 12% hash lookup). So until
String IO gets a bit faster, you're stuck.

You could look into using Peter Simons' BlockIO, and operating over bytes
rather than chars. But then you'd have to include that in the submitted
code, because it's not part of the standard libraries.

module Main where
import Data.HashTable
import System.IO

buildHash input = do
    table <- new (==) hashString
    sequence_ [ insert table w True | w <- lines input ]
    return table

check :: HashTable String Bool -> String -> IO ()
check table w = do
    r <- Data.HashTable.lookup table w
    maybe (putStrLn w) (const $ return ()) r

spellcheck table input = sequence_ [ check table w | w <- lines input ]

main = do
    table <- (readFile "Usr.Dict.Words" >>= buildHash)
    hGetContents stdin >>= spellcheck table

Confidentiality Note: The information contained in this   message, and any
attachments, may contain confidential   and/or privileged material. It is
intended solely for the   person(s) or entity to which it is addressed. Any
review,   retransmission, dissemination, or taking of any action in
reliance upon this information by persons or entities other   than the
intended recipient(s) is prohibited. If you received  this in error, please
contact the sender and delete the   material from any computer.

More information about the Glasgow-haskell-users mailing list