现在的位置: 首页 > 综合 > 正文

November 15th Sunday

2013年05月24日 ⁄ 综合 ⁄ 共 2968字 ⁄ 字号 评论关闭

  During learn Erlang, I found there are some detail technique problem must be taked down.

 

Term Comparisons

 

  For the purposes of comparison, a total ordering is defined over all terms. This is defined so that the following is true:

 

number < atom < reference < fun < port < pid < tuple < list < binary

 

  This means that, for example, a number (any number) is defined to be smaller than an atom (any atom), that a tuple is greater than an atom, and so on. (Note that for the purposes of ordering, ports and PIDs are included in this list. We’ll talk about these later.)

 

  Having a total order over all terms means we can sort lists of any type and build efficient data access routines based on the sort order of the keys.

 

  All the term comparison operators, with the exception of =:= and =/=, behave in the following way if their arguments are numbers:

 

  •   If one argument is a integer and the other is a float, then the integer is converted to a float before the comparison is performed.
  • If both arguments are integers or if both arguments are floats, then the arguments are used “as is,” that is, without conversion.

Operator          Meaning

X > Y             X is greater than Y.

X < Y             X is less than Y.

X =< Y            X is equal to or less than Y.

X >= Y            X is greater than or equal to Y.

X == Y            X is equal to Y.

X /= Y            X is not equal to Y.

X =:= Y           X is identical to Y.

X =/= Y           X is not identical to Y.

 

  =:= is for testing whether two terms are identical.  If in doubt, use =:=, and be suspicious if you see ==.  Note that a similar comment applies to using /= and =/=, where /= means “not equal to” and =/= means “not identical.”

 

  Note: In a lot of library and published code, you’ll see == used when the operator should have been =:=.  Fortunately, this kind of error does not often result in an incorrect program, since if the arguments to == do not contain any floats, then the behaviors of the two operators are the same.

 

Underscore Variables

 

  The special syntax _VarName is used for a normal variable, not an anonymous variable.  Normally the compiler will generate a warning if a variable is used only once in a clause since this is usually the sign of an error. If the variable
is used only once but starts with an underscore, the warning message will not be generated.

 

  Since _Var is a normal variable, very subtle bugs can be caused by forgetting this and using it as a “don’t care” pattern. In a complicated pattern match, it can be difficult to spot that, for example, _Int is repeated when it shouldn’t have been, causing the pattern match to fail.

 

  There are two main uses of underscore variables:

 

  • To name a variable that we don’t intend to use. That is, writing open(File, _Mode) makes the program more readable than writing open(File, _).
  • For debugging purposes. For example, suppose we write this:

                 some_func(X) ->
              {P, Q} = some_other_func(X),
              io:format("Q = ~p~n", [Q]),

              P.

      Now comment out the format statement:

          some_func(X) ->
              {P, Q} = some_other_func(X),
              %% io:format("Q = ~p~n", [Q]),

              P.

  If we compile this, the compiler will issue a warning that the variable Q is not used.

 

  If we rewrite the function like this:

          some_func(X) ->
              {P, _Q} = some_other_func(X),
              %% io:format("_Q = ~p~n", [_Q]),

              P.

   then we can comment out the format statement, and the compiler will not complain.

 

  The above is from Programmnig  Erlang

抱歉!评论已关闭.