[erlang-questions] some language changes

Tue Jun 5 02:29:27 CEST 2007

On 5 Jun 2007, at 1:49 am, James Hague wrote:
> I commonly see code like this, even in OTP:
> {Hours, Rest1} = parse_integer(L),
> Rest2 = parse_char(Rest1, $:),
> {Minutes, Rest3} = parse_integer(Rest2),
> etc.

Here's how I would write that in Haskell:

	let Just (hours, minutes) =
	       parse (pInteger <&> pLit: ':' <.> pInteger) chars

where parse :: Parser t r -> [t] -> Maybe r
       Parser t r = [t] -> Maybe ([t], r)

The merit of this approach is that the Rest variables (and the original
example would read so much better if L were Rest0) are *ENTIRELY HIDDEN*
by the use of parser combinators.

I've just rewritten (part of) my Haskell parser combinator library
in Erlang, and the example now looks like this:

	[{Hours, Minutes}] = parse(
	    pThen(pNat(),pKey(pLit($:),pNat()), Chars)

(Note that in order to make this kind of stuff readable it is
essential NOT to use module prefixes on the imported combinators.)

The idea of hiding "plumbing" variables inside combinators is very
useful indeed and is widely used in Haskell.

In my view, the use of assignment in such a case, while technically
equivalent to renaming, would be HARDER to read and HARDER to maintain
than what we have now.

More information about the erlang-questions mailing list