On Mon, Jan 29, 2007 at 08:14:55PM +0100, Michael Roth wrote:
> Hello list,

Hi!

Just to simplify one function...

> logdir      = "/var/log"
...
> makeOldname :: String -> String
> makeOldname fn = logdir ++ '/' : fn
...
> main :: IO ()
> main = do
>   files <- liftM (filter isLogfile) (getDirectoryContents logdir)
>   let oldnames = map makeOldname files
>   times <- mapM getModificationTime oldnames

main = do files <- liftM (filter isLogfile) (getDirectoryContents logdir)
          times <- mapM (getModificationTime . ("/var/log/"++)) files

(also consider)

main = do files <- filter isLogfile `fmap` getDirectoryContents logdir
          times <- (getModificationTime . ("/var/log/"++)) `mapM` files

If you count reindenting of the first line of the do statement and removal
of type signatures, I've eliminated six out of eight lines, and the
resulting function doesn't need to be read like spaghetti, looking back and
forth to find out what makeOldname and logdir are.  Of course, if you
expect to change logdir or use it elsewhere in the code, you still might
want to give it a name.  But my versions I'd say are more readable and much
more compact.

On large projects it's worthwhile using type declarations for top-level
functions, and it's worth adding them while debugging (to get better error
messages), or for tricky functions where the types aren't obvious.  But for
code like this, they just make it harder to read.
-- 
David Roundy
Department of Physics
Oregon State University

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to