Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>How the final return value is constructed...</strong></p> <p>When <code>[Msg | important()]</code> is being returned for the first time, the form of the final return value is determined. The only concern is, we don't know all the details of the final return value yet. So the <code>important()</code> in the <code>[Msg | important()]</code> will continue being evaluated. The following is an illustration of how the final return value <code>[high,high,low,low]</code> is constructed.</p> <pre><code>[high | important( )] &lt;---- Defines the final form --------------------------------- [high | important( )] &lt;---- Adds more details ------------------------ normal( ) &lt;---- Adds more details ------------------------ [low | normal( )] &lt;---- Adds more details ---------------- [low | normal()] &lt;---- Adds more details -------- [ ] &lt;---- Adds more details ------------------------------------------ [high | [high | [low | [low | []]]]] [high,high,low,low] &lt;---- The final return value </code></pre> <p><strong>How the code works...</strong></p> <p>In function <code>important/0</code>, <code>after 0</code> simply means "I don't wait for messages to come" -- if there is any message in my mailbox, I will look at it; if there isn't any, I will keep going (execute <code>normal()</code>) rather than waiting there. In the mailbox, there are <em>{15, high}, {7, low}, {1, low}, {17, high}</em> sitting there already. In Erlang, the messages in the mailbox are <strong>Not</strong> queued in a first-come-first-serve order. The <code>receive</code> clause can be picky. It scans through all the messages in the mailbox and "picks" the ones it desires. In our case, <em>{15, high}</em> and <em>{17, high}</em> get picked first according to <code>{Priority, Msg} when Priority &gt; 10</code>. After that, the function <code>normal/0</code> takes over. And <em>{7, low}, {1, low}</em> get processed (consed) in order. Finally, we got <code>[high,high,low,low]</code>.</p> <p><strong>A modified version that reveals the processing order...</strong></p> <p>We can modify the code a little bit in order to make the processing (consing) order more explicit:</p> <pre><code>-module(prior). -compile(export_all). important() -&gt; receive {Priority, Msg} when Priority &gt; 10 -&gt; [{Priority, Msg} | important()] % &lt;---- Edited after 0 -&gt; normal() end. normal() -&gt; receive {Priority, Msg} -&gt; % &lt;---- Edited [{Priority, Msg} | normal()] % &lt;---- Edited after 0 -&gt; [] end. </code></pre> <p>Run it in the shell:</p> <pre><code>4&gt; c(prior). {ok, prior} 5&gt; self() ! {15, high}, self() ! {7, low}, self() ! {1, low}, self() ! {17, high}. {17,high} 6&gt; prior:important(). [{15,high},{17,high},{7,low},{1,low}] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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