[erlang-questions] In what order are pattern matching clauses evaluated?

Bjorn Gustavsson <>
Tue Feb 20 07:07:55 CET 2007


The matching is always performed AS IF it was performed strictly top-down.
The pattern matching compiler only rearranges clauses when that will not
change the meaning of the program.

/Bjorn

"Seth Warn" <> writes:

> I wish I had looked at that code twice before posting, I wasn't
> thinking about the guard.  In any case, I'm still curious about the
> clause reordering mentioned in the referenced post.
> 
> Seth Warn
> University of Kansas
> 
> 
> 
> On 2/19/07, Seth Warn <> wrote:
> > Hello,
> >
> > While trying out Erlang, I noticed the following factorial function works:
> >
> > fac(N) when N > 0 ->
> >     N * fac(N-1);
> > fac(0) -> 1.
> >
> > "Ah ha," I said, "the pattern matching is not strictly top-down."  Is
> > there a resource somewhere that describes how the clauses may/will be
> > rearranged?  Searching the archive, the closest thing I found was this
> > post, which says that the clauses will be grouped by type:
> >
> > http://www.erlang.org/pipermail/erlang-questions/2000-May/001267.html
> >
> > Thanks,
> >
> > Seth Warn
> > University of Kansas
> > 
> > 
> >
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB




More information about the erlang-questions mailing list