Noob - Getting Started Infinte Loop?

Richard A. O'Keefe <>
Fri Sep 1 06:14:42 CEST 2006

"Lennart Ohman" <> wrote:
	the _ (only an underscore) is never bound from efficiency
	reasons. For instance:
	f(foo,bar) -> ...;
	f(frotz,X) -> ...;
	f(_,X) -> ....
One more time: this has NOTHING TO DO WITH EFFICIENCY.

>From a formal point of view, ALL VARIABLES ARE ALIKE.  This is true
in ML, Haskell, Clean, Mercury, Prolog, and Erlang.  Their spelling
makes NO difference to the semantics (possible style check warnings
aside) except that there can be any number of "anonymous" variables
spelled "_", and they are all different.

Your normal mental model of variables should treat them all alike.
Spelling is about telling PEOPLE stuff.

A good compiler will notice that a variable has a single occurrence
WITHOUT CARING HOW IT IS SPELLED and will perform whatever optimisations
are appropriate.

Note, for example, that in

    f(X, Y) -> [ignore,X,and,Y], g(Y).

X and Y are both used more than once.  However, a compiler could (and I
expect at least HiPE does) notice that [ignore,X,and,Y] doesn't actually
do anything useful, and the code you get might be the same as that for

    f(_, Y) -> g(Y).

My point here is that as far as optimisation is concerned, what counts
is how many uses of a variable survive after *other* optimisations have
been applied as well; any compiler that just relied on the spelling of
the variable names would be too dumb to remember to breathe in as well as out.

More information about the erlang-questions mailing list