<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"><<a href="mailto:gettimothy@zoho.com" target="_blank">gettimothy@zoho.com</a>></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's first example on <a href="http://www.mirandabanda.org/cogblog/category/cog/page/14/" target="_blank">fixing the reentrancy problem</a> that uses "factorial copy" 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: ' n=', (n asString),' ', (thisContext class name), '(', (thisContext identityHash asString),')' .<br>
Transcript show: '--sender-->', (thisContext sender) class name, '(', (thisContext sender) identityHash asString,')'. <br> Transcript show: '--home-->', (thisContext home) class name, '(', (thisContext home) identityHash asString,')'; cr. <br>
Transcript show: '++++++++++++++++++++++++++++++++++++++++';cr. <br> "thisContext explore. "<br> 1<br> ] <br> ifFalse:[ Transcript show: '-----------------------------------------------';cr. <br>
Transcript show: ' n=', (n asString),' ', (thisContext class name), '(', (thisContext identityHash asString),')' .<br> Transcript show: '--sender-->', (thisContext sender) class name, '(', (thisContext sender) identityHash asString,')'. <br>
Transcript show: '--home-->', (thisContext home) class name, '(', (thisContext home) identityHash asString,')'; cr. <br> (factorial copy value: n-1) * n<br>
]].<br>Transcript show:' factorial = ' , (factorial class name),'(',factorial identityHash asString,')'. <br>Transcript show: '--sender-->', (factorial sender) class name, '(', (factorial sender) identityHash asString,')'. <br>
Transcript show: '--home-->', (factorial home) class name, '(', (factorial home) identityHash asString,')'; cr. <br>Transcript show: '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^';cr. <br>
fc := factorial copy .<br><br>Transcript show:' fc = ' , (fc class name),'(',fc identityHash asString,')'. <br>Transcript show: '--sender-->', (fc sender) class name, '(', (fc sender) identityHash asString,')'. <br>
Transcript show: '--home-->', (fc home) class name, '(', (fc home) identityHash asString,')'; cr. <br>Transcript show: '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^';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-->UndefinedObject(3840)--home-->MethodContext(2677)<br>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br> fc = BlockContext(3934)--sender-->UndefinedObject(3840)--home-->MethodContext(2677)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br> n=1 BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)<br>++++++++++++++++++++++++++++++++++++++++<br>-----------------------------------------------<br>
n=2 BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)<br> n=1 BlockContext(1198)--sender-->BlockContext(3934)--home-->MethodContext(2677)<br>++++++++++++++++++++++++++++++++++++++++<br>
-----------------------------------------------<br> n=3 BlockContext(3934)--sender-->MethodContext(2367)--home-->MethodContext(2677)<br>-----------------------------------------------<br> n=2 BlockContext(1684)--sender-->BlockContext(3934)--home-->MethodContext(2677)<br>
n=1 BlockContext(2780)--sender-->BlockContext(1684)--home-->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'rd line, where n=1. <br>Why is the sender-->MethodContext(2367) instead of sender-->MethodContext(2677)<br>What created that new MethodContext?<br></div></div></blockquote><div><br></div><div>That would be the activation of Interval>>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>