multiple patterns per clause?

Christian S chsu79@REDACTED
Mon Aug 28 17:39:29 CEST 2006


My take: One way to code it would be to duplicate the code:

f("foo" ++ X) ->
   things,
   to,
   do;
f("bar" ++ X) ->
   things,
   to,
   do;
f("baz" ++ X) ->
   other_things,
   to_maybe,
   do_later;

But as code duplication is bad: Instead you refactor out to a function
the parts that would be duplicated if you would be insane like the
above example.

f_things(X) ->
   things,
   to,
   do;

f("foo" ++ X) ->
  f_things(X);
f("bar" ++ X) ->
  f_things(X);
f("baz" ++ X) ->
   other_things,
   to_maybe,
   do_later;

It has some annoying repetition to type, but hopefully that redundancy
makes the code easier to read.

> > Specifically, I want to test if a list has a number of different
> > prefixes, all of which would lead to the same expression.



More information about the erlang-questions mailing list