[erlang-questions] Idiom for multiple case matches

Ulf Wiger <>
Sat Nov 8 01:55:37 CET 2008


2008/11/7 Michael Radford <>:
> I hesitate to wade into this cranky-fest, but IMO this is extremely
> clear and easy to read:
>
>        is_vowel ($a) -> true;
>        is_vowel ($e) -> true;
>        is_vowel ($i) -> true;
>        is_vowel ($o) -> true;
>        is_vowel ($u) -> true;
>        is_vowel (_) -> false.

Sure, and it's a useful and powerful idiom.

But lists:member(C, "aeiou") is also extremely clear and easy to read,
and a whole lot easier to write (esp if you use emacs and the - in this
case - very annoying electric arrow, which will interfere with your
trying to write one-line function clauses like this.)


> True, the lists:member solution is less source code, but I think it's a
> toss-up in terms of clarity, and I have had far more problems dealing
> with code written by "shortest possible source code maniacs" than I have
> with "premature optimizers."

I don't think one qualifies as a "shortest possible source code maniac" for
preferring a one-liner over a 5-line solution, if it's a toss-up in terms of
clarity. (:


> I agree that micro benchmarks are irrelevant (except for library
> functions).  But when you can have both clarity and efficiency as above,
> why not?

Let's call this a micro-benchmark as regards the speed vs clarify
tradeoff. As both alternatives are ideomatic, quite readable and
efficient, it really boils down to a matter of taste. Robert and Mats
are to be commended primarily for suggesting elegant ways to
solve the problem. I'm sure both of them apply both patterns in
different situations, as their own tastes dictate.

My objection was only with the notion that Mats' version should
be discarded because of performance - not because it isn't slower
(it is, always), but because it's generally not fruitful to make such
decisions early, and at this level of abstraction.

Perhaps I also reacted instinctively since I happen to know that
Mats is certainly one of the most qualified to speak in this matter,
having been responsible for system-level profiling and troubleshooting
in the AXD 301 for nearly a decade. But of course, he's perfectly
capable of fighting his own battles.  (:

BR,
Ulf W

>
> Mike
>
> mats cronqvist writes:
>> "Hynek Vychodil" <> writes:
>>
>> > Anyway, 80% slow down can be good reason to not use lists:member for
>> > me when performance is important. I think that when mostly tested
>> > characters which are not vowels, it can be more.
>>
>>
>>   an "80% slowdown", or indeed any result of any micro-benchmark, is
>>   completely and utterly irrelevant. what is (often) important is
>>   overall application speed. unfortunately, optimizing a code base
>>   full of ad-hoc micro-optimizations can be... challenging.
>>
>>   mats
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://www.erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list