<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jan 1, 2014 at 2:55 PM, gettimothy <span dir="ltr">&lt;<a href="mailto:gettimothy@zoho.com" target="_blank">gettimothy@zoho.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> <br><u></u><div><div style="font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif">
In Eliot&#39;s first example on <a href="http://www.mirandabanda.org/cogblog/category/cog/page/14/" target="_blank">fixing the reentrancy problem</a> that uses &quot;factorial copy&quot; for the recursion.<br><br>I modifed his code:<br>
<br><font face="serif"></font><br><blockquote style="border:1px solid rgb(204,204,204);padding:7px;background-color:rgb(245,245,245)"><div><font face="serif">   | factorial |<br>                 factorial := [:n| n = 1 ifTrue: [1] ifFalse: [(factorial copy value: n - 1) * n]].<br>
                 (1 to: 10) collect: factorial copy</font></div></blockquote> <br><br>into something I could trace out a bit easier.<br><br><blockquote style="border:1px solid rgb(204,204,204);padding:7px;background-color:rgb(245,245,245)">
<div><br>| factorial fc |<br>Transcript clear.<br>factorial := [:n  |<br>    n = 1 <br>    ifTrue:[    <br>        Transcript show: &#39; n=&#39;,  (n asString),&#39; &#39;, (thisContext class name), &#39;(&#39;,  (thisContext identityHash asString),&#39;)&#39; .<br>
        Transcript show: &#39;--sender--&gt;&#39;, (thisContext sender) class name, &#39;(&#39;,   (thisContext sender) identityHash asString,&#39;)&#39;.         <br>        Transcript show: &#39;--home--&gt;&#39;, (thisContext home) class name,  &#39;(&#39;, (thisContext home) identityHash asString,&#39;)&#39;; cr.                                         <br>
        Transcript show: &#39;++++++++++++++++++++++++++++++++++++++++&#39;;cr.                         <br>         &quot;thisContext explore.    &quot;<br>        1<br>    ] <br>    ifFalse:[    Transcript show: &#39;-----------------------------------------------&#39;;cr. <br>
        Transcript show: &#39; n=&#39;,  (n asString),&#39; &#39;, (thisContext class name), &#39;(&#39;,  (thisContext identityHash asString),&#39;)&#39; .<br>        Transcript show: &#39;--sender--&gt;&#39;, (thisContext sender) class name, &#39;(&#39;,   (thisContext sender) identityHash asString,&#39;)&#39;.         <br>
        Transcript show: &#39;--home--&gt;&#39;, (thisContext home) class name,  &#39;(&#39;, (thisContext home) identityHash asString,&#39;)&#39;; cr.                                         <br>        (factorial copy value: n-1) * n<br>
        ]].<br>Transcript show:&#39; factorial = &#39; , (factorial class name),&#39;(&#39;,factorial identityHash asString,&#39;)&#39;.     <br>Transcript show: &#39;--sender--&gt;&#39;, (factorial sender) class name, &#39;(&#39;,   (factorial sender) identityHash asString,&#39;)&#39;.         <br>
Transcript show: &#39;--home--&gt;&#39;, (factorial home) class name,  &#39;(&#39;, (factorial home) identityHash asString,&#39;)&#39;; cr.     <br>Transcript show: &#39;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&#39;;cr.                                         <br>
fc := factorial copy .<br><br>Transcript show:&#39; fc = &#39; , (fc class name),&#39;(&#39;,fc identityHash asString,&#39;)&#39;.     <br>Transcript show: &#39;--sender--&gt;&#39;, (fc sender) class name, &#39;(&#39;,   (fc sender) identityHash asString,&#39;)&#39;.         <br>
Transcript show: &#39;--home--&gt;&#39;, (fc home) class name,  &#39;(&#39;, (fc home) identityHash asString,&#39;)&#39;; cr.     <br>Transcript show: &#39;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&#39;;cr.     <br>
<br>thisContext inspect.<br>factorial inspect.<br>fc inspect.<br>(1 to: 3) collect: fc</div></blockquote> <br><br><br>The Transcript output is as follows:<br><br><blockquote style="border:1px solid rgb(204,204,204);padding:7px;background-color:rgb(245,245,245)">
<div><br> factorial = BlockContext(694)--sender--&gt;UndefinedObject(3840)--home--&gt;MethodContext(2677)<br>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br> fc = BlockContext(3934)--sender--&gt;UndefinedObject(3840)--home--&gt;MethodContext(2677)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br> n=1 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)<br>++++++++++++++++++++++++++++++++++++++++<br>-----------------------------------------------<br>
 n=2 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)<br> n=1 BlockContext(1198)--sender--&gt;BlockContext(3934)--home--&gt;MethodContext(2677)<br>++++++++++++++++++++++++++++++++++++++++<br>
-----------------------------------------------<br> n=3 BlockContext(3934)--sender--&gt;MethodContext(2367)--home--&gt;MethodContext(2677)<br>-----------------------------------------------<br> n=2 BlockContext(1684)--sender--&gt;BlockContext(3934)--home--&gt;MethodContext(2677)<br>
 n=1 BlockContext(2780)--sender--&gt;BlockContext(1684)--home--&gt;MethodContext(2677)<br>++++++++++++++++++++++++++++++++++++++++</div></blockquote> <br><br><a href="http://www.mirandabanda.org/cogblog/category/cog/page/14/" target="_blank"></a><br>
Look at the 3&#39;rd line, where n=1. <br>Why is the sender--&gt;MethodContext(2367) instead of sender--&gt;MethodContext(2677)<br>What created that new MethodContext?<br></div></div></blockquote><div><br></div><div>That would be the activation of Interval&gt;&gt;collect:, which performs the first call of the copied factorial block for each 1 through 3.</div>
<div><br></div><div>collect: aBlock</div><div><span class="" style="white-space:pre">        </span>| nextValue result |</div><div><span class="" style="white-space:pre">        </span>result := self species new: self size.</div><div><span class="" style="white-space:pre">        </span>nextValue := start.</div>
<div><span class="" style="white-space:pre">        </span>1 to: result size do:</div><div><span class="" style="white-space:pre">                </span>[:i |</div><div><span class="" style="white-space:pre">                </span>result at: i put: (<b><u>aBlock value: nextValue</u></b>).</div>
<div><span class="" style="white-space:pre">                </span>nextValue := nextValue + step].</div><div><span class="" style="white-space:pre">        </span>^ result </div></div><br></div></div>