title: Scala For Comprehensions and For Loops
body: <h1>Objective</h1>
  
  <p>Trying to decrypt <a href="http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#for-comprehensions-and-for-loops" rel="nofollow">for comprehension and loop</a> and their difference.</p>
  
  <pre><code>Expr1          ::=  `for' (`(' Enumerators `)' | `{' Enumerators `}')
                         {nl} [`yield'] Expr
  Enumerators    ::=  Generator {semi Generator}
  Generator      ::=  Pattern1 `&lt;-' Expr {[semi] Guard | semi Pattern1 `=' Expr}
  Guard          ::=  `if' PostfixExpr
  </code></pre>
  
  <h2>Questions</h2>
  
  <p><strong><em>For loop</em></strong></p>
  
  <blockquote>
    <p>A for loop for (enumsenums) ee executes expression ee for each binding generated by the enumerators enumsenums. </p>
  </blockquote>
  
  <p>"Executes expression" means For Loop will not produce a value as the result but just apply some operation on each binding, hence it is basically a <strong><em>statement</em></strong> (in my understanding, in Scala, a expression returns a value, but a statement does not)?</p>
  
  <p>For example, below there will produce none. </p>
  
  <pre><code>val mnemonic = Map('2' -&gt; "ABC", '3' -&gt; "DEF")
  val a = for ((digit, str) &lt;- mnemonic) str.contains(digit)
  </code></pre>
  
  <p><strong><em>For comprehension</em></strong></p>
  
  <blockquote>
    <p>A for comprehension for (enumsenums) yield ee evaluates expression ee for each binding generated by the enumerators enumsenums and collects the results. </p>
  </blockquote>
  
  <p>Whereas For Comprehension will produce a collection object as the result by collecting the result of evaluating the <strong><em>Expr</em></strong> expression for each binding. If so, what is the type of the collection crated? If it is a method, I can look into API document but which document specifies the type returned by For comprehension?</p>