[clean-list] questions
Fergus Henderson
fjh@cs.mu.oz.au
Mon, 24 Nov 2003 10:10:58 +1100
On 20-Nov-2003, Tomasz Zielonka <t.zielonka@zodiac.mimuw.edu.pl> wrote:
> On Thu, Nov 20, 2003 at 05:26:36PM +0100, Jenda Krynicky wrote:
> >
> > If e.g. I want to make sure that the stuff entered by the user is a
> > date in YYYY-MM-DD format I definitely do not want to spend twenty
> > cryptic lines writing a parser where
> > /^(\d{4})-(\d{1,2})-(\d{1,2})$/
> > suffices. (I know you need to make sure the month is between 1 and 12
> > and the day is reasonable, but you'd do that outside the parser as
> > well.)
>
> date = do
> yyyy <- count 4 digit
> char '-'
> mm <- count 2 digit
> char '-'
> dd <- count 2 digit
> return (yyyy, mm, dd)
>
> Is it really so long and cryptic?
No! "\d{4}" is cryptic! "yyyy <- count 4 digit" is elegant and reasonably
concise without being cryptic.
But if you really want to make it as concise as possible, at the expense
of readability, like the regexp, it could be a lot more concise:
do { y <- count 4 digit; char '-'; m <- count 2 digit; char '-';
d <- count 2 digit; return (y, m, d); }
With suitable abbreviations,
c = char
r = count -- r for "repeat"
d = digit
you could make it a one-liner:
do{yyyy<-r 4 d;c '-';mm<-r 2 d;c '-';dd<-r 2 d;return(yyyy,mm,dd); }
But I hope you will agree that such source code compression harms readability
and that this code, *just like regexps*, is unnecessarily cryptic.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.