[Haskell-cafe] Alternatives to convoluted record syntax

Dougal Stanton dougal at dougalstanton.net
Thu Jul 3 06:00:43 EDT 2008


Hi Haskellers,

This is a style question, as my program works fine but looks really
ugly and can be confusing to maintain.

I take options from the command line using GetOpt and hand back this
structure for later use.

> data Opts = Opts
>     { query         :: Query
>     , queryLimit    :: Maybe Limit
>     , disabledOnly  :: Bool
>     } deriving Show

Here's a snippet from the parser for one option (others omitted for clarity):

> options :: [OptDescr (Opts -> Opts)]
> options =
>     [ Option "b" ["bus"] (ReqArg busNum "NUM") "Bus number"
>     , ...
>     ]
>   where busNum n os = let b = (query os) { queryBusNumber = Just n }
>                       in if isBusId n then os { query = b } else os

Variations on that ugliness are repeated four times for other fields.
Is there an alternative way to change the value of nested fields?

Thanks,

Dougal.

-- 
Dougal Stanton
dougal at dougalstanton.net // http://www.dougalstanton.net


More information about the Haskell-Cafe mailing list