December 4th Friday 2009

In Erlang,  the both syntax of Bit String Comprehensions and List Comprehensions are so similar.

 

Bit string comprehensions are written with the following syntax:

<< BitString || Qualifier1,...,QualifierN >>

BitString is a bit string expression, and each Qualifier is either a generator, a bit string generator or a filter.

 

List comprehensions are written with the following syntax:

[Expr || Qualifier1,...,QualifierN]

Expr is an arbitrary expression, and each Qualifier is either a generator or a filter.

 

  From their aspects, there is only difference at the brackets.  Yes, a bit string comprehension returns a bit string, which is created by concatenating the results of evaluating BitString for each combination of bit string generator elements for which all filters are true. A list comprehension returns a list, where the elements are the result of evaluating Expr for each combination of generator list elements and bit string generator elements for which all filters are true.

 

A generator is written as:
  Pattern <- ListExpr.
ListExpr must be an expression which evaluates to a list of terms.
A bit string generator is written as:
  BitstringPattern <= BitStringExpr.
BitStringExpr must be an expression which evaluates to a bitstring.
A filter is an expression which evaluates to true or false.
The variables in the generator patterns shadow variables in the function clause surrounding the bit string comprehensions.

 

  That is so nature to our mind.  It is easy to remember them.

你可能感兴趣的:(list,String,erlang,each,generator,variables)