Note that there are some explanatory texts on larger screens.

plurals
  1. POErlang function with number for parameter
    text
    copied!<p>Hi I'm learning Erlang via Learn You Some Erlang by Fred Hebert.</p> <p>And I've come across a code that I'm confuse about:</p> <pre><code>sword(1) -&gt; throw(slice); sword(2) -&gt; erlang:error(cut_arm); sword(3) -&gt; exit(cut_leg); sword(4) -&gt; throw(punch); sword(5) -&gt; exit(cross_bridge). talk() -&gt; "blah blah". black_knight(Attack) when is_function(Attack, 0) -&gt; try Attack() of _ -&gt; "None shall pass." catch throw:slice -&gt; "It is but a scratch."; error:cut_arm -&gt; "I've had worse."; exit:cut_leg -&gt; "Come on you pansy!"; _:_ -&gt; "Just a flesh wound." end. </code></pre> <p>So here's the confusion. I don't understand <code>sword(#)</code> function. Why are there number as parameter? The function <code>is_function</code> actually check if these function are of arity 0 and apparently all the <code>sword(#)</code> functions are of arity 0. </p> <p>Also the way to pass in the <code>sword(#)</code> function to the <code>black_knight</code> function is different compare to the <code>talk</code> function.</p> <p>Here's how the book pass a <code>sword</code> function and the <code>talk</code> function.</p> <pre><code>exceptions:black_knight(fun exceptions:talk/0). </code></pre> <p>vs</p> <pre><code>exceptions:black_knight(fun() -&gt; exceptions:sword(1) end). </code></pre> <p>The <code>talk</code> function we just pass the function where as the <code>sword(1)</code> function we have to wrap it with a anonymous function. I don't get it.</p> <p>So the questions are:</p> <ol> <li>Why is passing these <code>sword(#)</code> different from <code>talk</code> function.</li> <li>Why <code>sword(#)</code> have a number as a parameter?</li> <li>Why <code>sword(#)</code> have 0 arity when it seems like it have an arity of 1 (I'm counting the number parameter as a parameter)?</li> </ol> <p><a href="http://learnyousomeerlang.com/errors-and-exceptions#dealing-with-exceptions" rel="nofollow">The chapter of the book I'm at.</a></p> <p>Thank you for your time.</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