Noob - Getting Started Infinte Loop?

Håkan Stenholm hokan.stenholm@REDACTED
Thu Aug 31 03:03:21 CEST 2006


fbg111 wrote:

>
>ke han wrote:
>
>>3 - Also, for efficiency sake, you should name the variable "First"  
>>as "_First" to indicate you do not want the value bound.
>>
>>
>
>I was under the impression so far that all variables in Erlang are bound,
>unless 'bound' means something other than you can't change the value of a
>variable after assignment?  Thanks!
>
There are actualy two uses of of the _ (underscore).


* case 1; as a way to tell pattern matching to match anything (and don't 
bind anything to the _) e.g. in a function clause:

foo(_, V2) -> V2;  % ignore the first argument, as it's unused in this 
function clause
foo(...) -> ...



* case 2; as a way to tell the compiler that it is ok that the variable 
name V is unused (you'll otherwise get a "variable V unused in ..." 
error when using erlc) e.g.:

foo(_V) -> ok;  % variable V is unused, renamed to _V to inidicate that 
it's not used in this clause
foo(V) -> ....

Note that _V is still a valid variablename, so the code below is valid:

    foo(_V) ->
        _V * 2.

    a call to foo(42) would yield 84 in this case.


But you should never use _ in variable names in this way (instead remove 
the _ if the V is to be used).

The _ is usually applied to variable names after the compiler complains 
about unused variable names and we have ensured that the lack of usuage 
isn't due to some kind of bug - e.g. we forgot to use the variable or 
accidently instead used another variable in the wrong place.



More information about the erlang-questions mailing list