The following is a slightly contrived example, but it explains the style question I’m trying to understand.
Say that you’re trying to write a little lexical analyzer by hand. You want to discriminate between a number of classes that a character
c could be a member of, and rather than using match constructs like
'A'..'Z' | 'a'..'z' you would like to call some boolean function like
is_letter c instead.
(Note: I said this was slightly contrived, but it reflects a real problem in a slightly different domain.)
Say there are a dozen such classes you’re trying to discriminate between.
Now, you could do a
match c with followed by a lot of lines with guard conditions expressed with
match c with | c when is_letter c -> something | c when is_digit c -> something_else | c when is_ctrl c -> yet_another_thing
Or you could do
if is_letter c then something else if is_digit c then another_something else if is_ctrl c then yet_another_thing
Both of these choices feel kind of unsatisfying to me. The
match is really not a match at all, it’s just an endless series of guard expressions that don’t really use the pattern variable for anything, and it looks ugly to me.
if also feels kind of gross, perhaps because it looks too much like the worst of what you would do in other (lesser) languages.
In Lisp, I’d just use a
cond, which is purpose-built for discriminating against a large set of cases defined by boolean expressions, but it doesn’t feel like there’s a good choice here in OCaml.
Which of these, if either, is better style? If neither, what is the correct way to do such a thing with good style?