{"id":379,"date":"2018-11-05T17:29:36","date_gmt":"2018-11-05T17:29:36","guid":{"rendered":"https:\/\/books.compclassnotes.com\/elementarycomputing\/?page_id=379"},"modified":"2018-11-05T17:29:36","modified_gmt":"2018-11-05T17:29:36","slug":"lifetime-diagrams","status":"publish","type":"page","link":"https:\/\/books.compclassnotes.com\/elementarycomputing\/lifetime-diagrams\/","title":{"rendered":"Lifetime Diagrams"},"content":{"rendered":"<p>Recall from the last section that we had this interaction with Racket:<\/p>\n<pre>&gt; (define x 2) \r\n&gt; (define add1 (lambda (y) (+ 1 y))) \r\n&gt; (add1 x)\r\n3\r\n&gt; x \r\n2 \r\n&gt; y \r\ny: undefined;<\/pre>\n<p>Let&#8217;s run through this step by step and see exactly what happens:<\/p>\n<ul>\n<li><strong>x\u00a0<\/strong>is defined and given a value<\/li>\n<li>The\u00a0<strong>add1<\/strong> function is created<\/li>\n<li><strong>add1<\/strong> is called<\/li>\n<li>Variable\u00a0<strong>y<\/strong> is created<\/li>\n<li>The argument (contents of\u00a0<strong>x<\/strong>) are copied into y<\/li>\n<li>The body\u00a0<strong>(+ 1 y)\u00a0<\/strong>is executed<\/li>\n<li><strong>y<\/strong>\u00a0is thrown away<\/li>\n<\/ul>\n<p>This means that\u00a0<strong>y<\/strong> has a limited &#8220;lifetime&#8221;;\u00a0<strong>x<\/strong> still exists because it is a global variable. We can visualise this using\u00a0<em>Lifetime Diagrams\u00a0<\/em>as in the figure below:<\/p>\n<div id=\"attachment_409\" style=\"width: 660px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-409\" class=\" wp-image-409\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/01-LIfetime.png\" alt=\"\" width=\"650\" height=\"156\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/01-LIfetime.png 1209w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/01-LIfetime-300x72.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/01-LIfetime-768x184.png 768w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/01-LIfetime-1024x246.png 1024w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><p id=\"caption-attachment-409\" class=\"wp-caption-text\">A Lifetime Diagram showing when functions and variables exist.<\/p><\/div>\n<p>The top red line shows how long Racket is running. Once we define\u00a0<strong>x<\/strong>, it becomes part of the language and stays in existence until Racket finishes, as does\u00a0<strong>add1<\/strong>, which comes into existence shortly after\u00a0<strong>x<\/strong>. However,\u00a0<strong>y<\/strong> has a limited lifetime because it only exists while\u00a0<strong>add1<\/strong> is\u00a0<em>executing<\/em>; that is, as soon as\u00a0<strong>add1<\/strong> is finished,\u00a0<strong>y<\/strong> no longer exists.<\/p>\n<p>However, what happens if\u00a0<em>two<\/em> copies of\u00a0<strong>add1<\/strong> are running at the same time? Clearly, they can&#8217;t both use\u00a0<strong>y<\/strong>, otherwise they could give the wrong result, so each has its\u00a0<em>own<\/em>\u00a0<strong>y<\/strong>. This is often denoted by appending a dollar sign and random number to the variable name, for example\u00a0<strong>y$1231<\/strong> or\u00a0<strong>y$1831<\/strong>. This shows that it is a local copy of\u00a0<strong>y<\/strong>, but clearly shows that it is a different variable. So, if we had two copies running, a more precise version of the diagram above would be this:<\/p>\n<div id=\"attachment_410\" style=\"width: 711px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-410\" class=\" wp-image-410\" src=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime.png\" alt=\"\" width=\"701\" height=\"203\" srcset=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime.png 1216w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-300x87.png 300w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-768x222.png 768w, https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-content\/uploads\/sites\/9\/2018\/11\/02-Lifetime-1024x296.png 1024w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><p id=\"caption-attachment-410\" class=\"wp-caption-text\">A Lifetime Diagram with multiple copies of the same function running.<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recall from the last section that we had this interaction with Racket: &gt; (define x 2) &gt; (define add1 (lambda (y) (+ 1 y))) &gt; (add1 x) 3 &gt; x 2 &gt; y y: undefined; Let&#8217;s run through this step <span class=\"readmore\"><a href=\"https:\/\/books.compclassnotes.com\/elementarycomputing\/lifetime-diagrams\/\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":4,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-379","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/comments?post=379"}],"version-history":[{"count":4,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/379\/revisions"}],"predecessor-version":[{"id":411,"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/pages\/379\/revisions\/411"}],"wp:attachment":[{"href":"https:\/\/books.compclassnotes.com\/elementarycomputing\/wp-json\/wp\/v2\/media?parent=379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}