title: How is a for expression with multiple monads translated in scala?
body: <p>I am reading "Programming in Scala 2nd Edition" and I have some idea about monad from a Haskell course I took. However, I do not understand why the following code "magically" works:</p>
  
  <pre><code>scala&gt; val a: Option[Int] = Some(100)
  a: Option[Int] = Some(100)
  
  scala&gt; val b = List(1, 2, 3)
  b: List[Int] = List(1, 2, 3)
  
  for ( y &lt;- b; x &lt;- a ) yield x;
  res5: List[Int] = List(100, 100, 100)
  </code></pre>
  
  <p>I do not understand the above because according to the book's Chapter 23.4, the <code>for</code> expression is translated to something like:</p>
  
  <pre><code>b flatMap ( y =&gt;
    a map ( x =&gt; x )
  )
  </code></pre>
  
  <p>I am puzzled why the above code compiles because <code>y =&gt; a map (x =&gt; x)</code> is of type <code>Int =&gt; Option[Int]</code>, while the <code>b.flatMap</code> expects a <code>Int =&gt; List[Something]</code>.</p>
  
  <p>On the other hand, the following code does NOT compile (which is good otherwise I would be more lost):</p>
  
  <pre><code>scala&gt; for ( x &lt;- a; y &lt;- b ) yield y;
  &lt;console&gt;:10: error: type mismatch;
   found   : List[Int]
   required: Option[?]
                for ( x &lt;- a; y &lt;- b ) yield y;
                            ^
  </code></pre>
  
  <p>So what is magical with the first example?</p>