Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>This does not mean what you think:</p> <pre><code>fun funkyPlus 3 = (fn x =&gt; fn y =&gt; x*x*y)3 </code></pre> <p>It defines a function which takes an argument that must be 3, and which evaluates to its RHS if it is 3 and is undefined otherwise. What you mean to say is this: If we only provide an argument for x, we have the following:</p> <pre><code>funkyPlus 3 → (fn x =&gt; fn y =&gt; x*x+y) 3 </code></pre> <p>and so forth.</p></li> <li><p>Secondly, there is an error in your <code>foldl</code>:</p> <pre><code>fun foldl f b [] = b|foldl f b (h::t) = foldl f f(h,b) t; ^^^^^ Type clash: expression of type 'a * 'b cannot have type 'c list </code></pre> <p>This is because <code>(h,b)</code> is parsed as the third argument to <code>foldl</code> and not as the argument to <code>f</code>. Parenthesize it:</p> <pre><code>fun foldl f b [] = b|foldl f b (h::t) = foldl f (f(h,b)) t; &gt; val ('a, 'b) foldl = fn : ('a * 'b -&gt; 'b) -&gt; 'b -&gt; 'a list -&gt; 'b </code></pre></li> </ol> <p>Now, getting to your question, ML can tell us that an expression like <code>foldl add</code> would have type <code>int -&gt; int list -&gt; int</code>.</p> <p>But in general, it may help to realize that function application is entirely mechanical. If we have these two definitions:</p> <pre><code>fun foldl f b [] = b | foldl f b (h::t) = foldl f (f(h,b)) t; add (x,y) = x + y; </code></pre> <p>then <code>var example = foldl add</code> would be equivalent to this:</p> <pre><code>fun example b [] = b | example b (h::t) = example (h::t) (add(h,b)) t; </code></pre> <p>All that’s been done is that <code>add</code> has been substituted for <code>f</code> in the body of <code>foldl</code>, nothing more (although I have taken the liberty of replacing <code>foldl add</code> with <code>example</code> in the body).</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