Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Very carefully. There are two things wrong with your definition in its current state:</p> <p>The first is the one that generates the exception you're seeing. You don't actually want the assignment operator preceding <code>b(a)</code>, you want that to be the body of your anonymous function, so you'll need the <code>=&gt;</code> operator:</p> <p><code>val compose = (a: Double =&gt; Double, b: Double =&gt; Double) =&gt; b(a)</code></p> <p>However, you're still not done. In the body <code>b(a)</code>, what the compiler thinks you're doing is <code>apply</code>ing b, with <code>a</code> as the parameter, so you get:</p> <pre><code>&lt;console&gt;:7: error: type mismatch; found : Double =&gt; Double required: Double val compose = (a: Double =&gt; Double, b: Double =&gt; Double) =&gt; b(a) </code></pre> <p>As you can see, it's expecting <code>b</code> to take a <code>Double</code>, but you're passing <code>a</code> as an argument, which is a <code>Double =&gt; Double</code> / <code>Function1[Double, Double</code>].</p> <p>So that's why you're seeing what you're seeing, and what you'd see in the next step, but I'm not entirely sure myself how to answer quite <em>how</em> to write <code>compose</code>. You can cheat somewhat by adding a parameter to <code>compose</code>:</p> <pre><code>scala&gt; val compose = (a: Double =&gt; Double, b: Double =&gt; Double, c: Double) =&gt; b(a(c)); compose: (Double =&gt; Double, Double =&gt; Double, Double) =&gt; Double = &lt;function3&gt; scala&gt; compose(x =&gt; x * 2, y =&gt; y * 2, 2) res2: Double = 8.0 </code></pre> <p>But that's cheating.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload