{"id":6209,"date":"2024-07-05T09:45:56","date_gmt":"2024-07-05T07:45:56","guid":{"rendered":"https:\/\/oopm.org\/?page_id=6209"},"modified":"2024-12-15T11:13:34","modified_gmt":"2024-12-15T10:13:34","slug":"14-3-interlocked-algorithms","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=6209","title":{"rendered":"15.3 Using coroutines to describe algorithms"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages6209&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages6209&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this section, we show examples of how to use coroutines to describe a certain class of algorithms often referred to as <em>interlocked sequential execution stacks<\/em>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<em>generator<\/em>&nbsp;is a coroutine capable of producing a sequence of values. A new value is produced for each resume of the coroutine. The next value depends on the sequence of previously generated values. We show how to define generators for computing the mathematical functions factorial. The main purpose of the factorial example is to illustrate how coroutines work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We assume the reader is familiar with the factorial function, but here is a short description: The&nbsp;factorial&nbsp;of a non-negative&nbsp;integer N,&nbsp;denoted&nbsp;by&nbsp;N!,&nbsp;is the&nbsp;product&nbsp;of all positive integers less than or equal&nbsp;to N. First we show how to define factorial as a simple method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   FactorialFunction(N: <strong>var<\/strong> integer) -&gt; F: <strong>var<\/strong> integer:\n      F := N\n      loop: do\n         if (N &gt; 1) :then\n            N := N - 1\n            F := F * N\n            restart(loop)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>FactorialFunction<\/code> computes the product <em>N * (N &#8211; 1) * (N &#8211; 2) * &#8230; * 2 * 1<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next we show how to define a generator that produce the sequence of factorial, <em>1!, 2!, 3!, &#8230;, N!, &#8230;<\/em>. When the coroutine is resumed, it returns the next value in the sequence when it suspends.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   PlainFactorial: <strong>obj<\/strong>\n      getNext -&gt; R: <strong>var<\/strong> integer:\n         resume(PlainFactorial)<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">\n<\/mark>         R := F \n      F: <strong>var<\/strong> integer\n      N: <strong>var<\/strong> integer\n      F := 1\n      cycle\n         PlainFactorial.suspend\n         N := N + 1\n         F := F * N<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>PlainFactorial<\/code> is generated it executes statements until the first execution of <code>PlainFactorial.suspend<\/code>. A subsequent  resume of <code>PlainFactorial<\/code> will continue execution after this <code>suspend<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>PlainFactorial.getNext <\/code>will return the factorial of 1 which is 1. Subsequent resumes will return the next factorial as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      -\"-\n   V: <strong>var<\/strong> integer\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span> -- V = 1\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span> -- V = 2\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span> -- V = 6\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span> -- V = 24<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The next figure shows a snapshot of the execution of <code>UsingPlainFactorial<\/code> where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PlainFactorial<\/code> has been generated and suspended execution during the first iteration of <code>cycle<\/code>.<\/li>\n\n\n\n<li>The read arrow <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">&#8211;&gt;<\/mark> shows that <code>PlainFactorial<\/code> is suspended just before the statement <code>N := N + 1<\/code>.<\/li>\n\n\n\n<li>The point of execution in <code>UsingPlainFactorial<\/code> is before the first statement <code>V := PlainFactorial.next<\/code> as indicated by the read arrow <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark>.<\/li>\n\n\n\n<li>A read arrow of the form <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark> shows the active point of execution.<\/li>\n\n\n\n<li>A read arrow of the form <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">&#8211;&gt;<\/mark> show the point of suspension of a coroutine.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The OSD in the right column illustrates what <code>UsingPlainFactorial<\/code> is currently executing and that <code>PlainFactorial<\/code> is suspended.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      -\"-\n      F := 1\n      cycle\n         PlainFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      N := N + 1\n         F := F * N\n    V: <strong>var<\/strong> integer\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"672\" height=\"314\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-I-4.jpg\" alt=\"\" class=\"wp-image-6362\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-I-4.jpg 672w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-I-4-300x140.jpg 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The OSD also shows the values of the variable <code>V<\/code> in <code>UsingPlainFactorial<\/code>, and the variables , <code>F<\/code> and <code>N<\/code> in <code>PlainFactorial<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next next scenario shows the situation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PlainFactorial.getNext<\/code> has been executed by <code>UsingPlainFactorial<\/code>.<\/li>\n\n\n\n<li><code>getNext<\/code> has executed <code>resume(PlainFactorial)<\/code>.<\/li>\n\n\n\n<li>The next statement to be executed is <code>N := N + 1<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      getNext -&gt; R: <strong>var<\/strong> integer:\n         resume(PlainFactorial)\n         R := F \n      F: <strong>var<\/strong> integer\n      N: <strong>var<\/strong> integer\n      F := 1\n      cycle\n         PlainFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark>      N := N + 1\n         F := F * N\n   V: <strong>var<\/strong> integer\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n   V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"681\" height=\"319\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-II-1.jpg\" alt=\"\" class=\"wp-image-6363\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-II-1.jpg 681w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-II-1-300x141.jpg 300w\" sizes=\"(max-width: 681px) 100vw, 681px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The next snapshot shows the situation after <code>PlainFactorial<\/code> has executed the two assignments statements, <code>N := N + 1 <\/code>and <code>F := F * N<\/code><strong>,  <\/strong>and executed a <code>PlainFactorial.suspend<\/code> during the second iteration of <code>cycle<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PlainFactorial<\/code> is suspended before the statement <code>N := N + 1<\/code> &#8211; as in the first figure above.<\/li>\n\n\n\n<li>The active point of execution is in <code>UsingPlainFactorial<\/code> before the second statement <code>V := PlainFactorial.getNext<\/code>.<\/li>\n\n\n\n<li>The variables have been updated <code>V<\/code>, <code>F<\/code> and <code>N<\/code> have all new values.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      -\"- \n      cycle\n         PlainFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      N := N + 1\n         F := F * N\n    V: <strong>var<\/strong> integer\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"683\" height=\"322\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-III-2.jpg\" alt=\"\" class=\"wp-image-6364\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-III-2.jpg 683w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-III-2-300x141.jpg 300w\" sizes=\"(max-width: 683px) 100vw, 683px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Execution of the second <code>V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span><\/code> resumes execution of <code>PlainFactorial<\/code>, which then generate the next factorial value. We don&#8217;t show a snapshot of this. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next snapshot shows the situation after <code>PlainFactorial<\/code> has suspended:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PlainFactorial<\/code> is as in previous snapshots suspended before the statement <code>N := N + 1<\/code>.<\/li>\n\n\n\n<li>The current point of execution is in <code>UsingPlainFactorial<\/code> before the third statement <code>V := PlainFactorial.getNext<\/code>.<\/li>\n\n\n\n<li>The variables <code>V<\/code>, <code>F<\/code> and <code>N<\/code> have been updated.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      -\"- \n      cycle\n         PlainFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      N := N + 1\n         F := F * N\n    V: <strong>var<\/strong> integer\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"328\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VII-3.jpg\" alt=\"\" class=\"wp-image-6374\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VII-3.jpg 718w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VII-3-300x137.jpg 300w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Finally we show a snapshot at the situation after execution of the last <code>V := PlainFactorial.getNext<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>PlainFactorial<\/code> is (again) suspended before the statement <code>N := N + 1<\/code>.<\/li>\n\n\n\n<li>The active point of execution is after the last statement.<\/li>\n\n\n\n<li>The variables <code>V<\/code>, <code>F<\/code> and <code>N<\/code> have been updated.<\/li>\n\n\n\n<li><code>V<\/code> = 24 = 6!<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>UsingPlainFactorial: <strong>obj<\/strong>\n   PlainFactorial: <strong>obj<\/strong>\n      -\"- \n      cycle\n         PlainFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      N := N + 1\n         F := F * N\n    V: <strong>var<\/strong> integer\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark> V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n    V := <span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">PlainFactorial.getNext<\/span>\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"671\" height=\"352\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VIII-2.jpg\" alt=\"\" class=\"wp-image-6376\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VIII-2.jpg 671w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/Coroutines-book-VIII-2-300x157.jpg 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Recursive factorial generator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next we show a a version of a factorial generator using a recursive method instead of the loop implemented using <code>cycle<\/code>. As said the purpose is to show how coroutines work and not necessarily a recommended programming style.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  RecursiveFactorial: <strong>obj<\/strong>\n      getNext -&gt; R: <strong>var<\/strong> integer:\n         resume(RecursiveFactorial)\n         R := F\n      F: <strong>var<\/strong> integer\t \n      N: <strong>var<\/strong> integer\n      next:\n         RecursiveFactorial.suspend\n         N := N + 1\n         F := F * N\n         next\n      F := 1 \n      next<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>RecursiveFactorial<\/code> is generated it invokes the local method <code>next<\/code>, which suspends execution. Successive invocations of <code>next<\/code> resumes the coroutine and returns the <code>next<\/code> factorial (<code>F<\/code>) in the sequence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next figures shows snapshots of using <code>RecursiveFactorial<\/code> by the object <code>UsingRecursiveFactorial<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UsingRecursiveFactorial: <strong>obj<\/strong>\n   RecursiveFactorial: <strong>obj<\/strong>\n      -\"-\n   V: <strong>var<\/strong> integer\n   V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 1\n   V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 2\n   V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 6\n   V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 24 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first snapshot how the situation after the objects <code>UsingRecursiveFactorial<\/code> and <code>RecursiveFactorial<\/code> have been generated:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RecursiveFactorial<\/code> is suspended before <code>N := N + 1<\/code> in the method object <code>next<\/code>.<\/li>\n\n\n\n<li>Note that <code>RecursiveFactorial<\/code> has been suspended while execution an instance of the method <code>next<\/code>.<\/li>\n\n\n\n<li>The active point of execution is at the first <code>V := RecursiveFactorial.getNext<\/code> in <code>UsingRecursiveFactorial<\/code>.<\/li>\n\n\n\n<li>The values of the variables are <code>V = 0<\/code>, <code>F = 1<\/code> and <code>N = 0<\/code>.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code> UsingRecursiveFactorial: <strong>obj<\/strong>\n    RecursiveFactorial: <strong>obj<\/strong>\n       getNext -&gt; R: <strong>var<\/strong> integer:\n          resume(RecursiveFactorial)\n          R := F\n       F: <strong>var<\/strong> integer\t \n       N: <strong>var<\/strong> integer\n       next:\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>       RecursiveFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>       N := N + 1\n          F := F * N\n          next\n       F := 1\n       next\n    V: <strong>var<\/strong> integer\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark> V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"363\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-I-2.jpg\" alt=\"\" class=\"wp-image-6369\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-I-2.jpg 849w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-I-2-300x128.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-I-2-768x328.jpg 768w\" sizes=\"(max-width: 849px) 100vw, 849px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"wp-block-paragraph\">The next snapshot shows a situation after execution of the first <code>RecursiveFactorial.getNext<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>RecursiveFactorial<\/code> has been resumed.<\/li>\n\n\n\n<li><code>N := N + 1<\/code> has been executed &#8212; <code>N = 1<\/code>.<\/li>\n\n\n\n<li><code>F := F * N<\/code> has been executed &#8212; <code>F = 1<\/code>.<\/li>\n\n\n\n<li>A recursive invocation of <code>next<\/code> has been generated.<\/li>\n\n\n\n<li>The active point of execution is at <code>RecursiveFactorial.suspend<\/code> in this <code>next<\/code> method.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code> UsingRecursiveFactorial: <strong>obj<\/strong>\n    RecursiveFactorial: <strong>obj<\/strong>\n       -\"-\n       next:\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">==&gt;<\/mark>       RecursiveFactorial.suspend\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>       N := N + 1\n          F := F * N\n          next\n       F := 1\n       next\n    V: <strong>var<\/strong> integer\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span>\n    V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span><\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"859\" height=\"406\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-II-3.jpg\" alt=\"\" class=\"wp-image-6377\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-II-3.jpg 859w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-II-3-300x142.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecCoroutines-book-II-3-768x363.jpg 768w\" sizes=\"(max-width: 859px) 100vw, 859px\" \/><\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"740\" height=\"324\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-I.jpg\" alt=\"\" class=\"wp-image-6269\" style=\"width:879px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-I.jpg 740w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-I-300x131.jpg 300w\" sizes=\"(max-width: 740px) 100vw, 740px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 2<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 6<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>V := Recursive<span style=\"font-family: inherit; font-size: var(--wp--preset--font-size--tiny);\">Factorial.getNext<\/span> -- V = 24<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"478\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-1024x478.jpg\" alt=\"\" class=\"wp-image-6272\" style=\"width:781px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-1024x478.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-300x140.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-768x359.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-1536x717.jpg 1536w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/RecursiveFactorial-IV-2048x957.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, a <code>suspend<\/code> within <code>next<\/code>, suspends the whole execution stack and a <code>resume<\/code> resumes execution of the top element of the stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Merging binary search trees<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The next example is more interesting. Here we show how to merge two binary search trees. We assume that the reader is familiar with the concept of a binary search tree <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">+++ evt henvisning.<\/mark> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we define a binary tree where the a node contains a <code>String<\/code> being the name of a person. First we define class <code>BinarySearchTree<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> BinaryTree: \n      <strong>class<\/strong> node(elm: <strong>var<\/strong> String):\n         left: <strong>ref<\/strong> node\n         right: <strong>ref<\/strong> node\n         insert(S: <strong>var<\/strong> String):\n            if (S &lt;= elm) :then \n               if (left == none) :then \n                  left := node(S) \n               :else \n                  left.insert(S) \n            :else \n               if (right == none) :then \n                  right := node(S) \n               :else \n                  right.insert(S)\n         print(ind: <strong>var<\/strong> integer):\n           ... \n      root: <strong>ref<\/strong> node\n      insert(S: <strong>var<\/strong> string):\n         if (root == none) :then \n            root := node(S) \n         :else \n            root.insert(S)\n      :::<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>BinaryTree<\/code> has the following attributes:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A local class <code>Node<\/code>.<\/li>\n\n\n\n<li>A reference <code>root<\/code>, that refers to the root (top <code>Node<\/code>) of the tree.<\/li>\n\n\n\n<li>A method <code>insert<\/code> for inserting a <code>Node<\/code> in the tree with the parameter <code>S<\/code> being the <code>String<\/code> stored in the <code>Node<\/code>. It works as follows:\n<ul class=\"wp-block-list\">\n<li>If <code>root == none<\/code>, then the insertion is the first <code>Node<\/code> else <code>root.insert(S)<\/code> is invoked.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A <code>Node<\/code> has the following attributes:<\/li>\n\n\n\n<li>References <code>left<\/code> and <code>right<\/code> that refer to the left and right branches of the <code>Node<\/code> respectively.<\/li>\n\n\n\n<li>An <code>insert<\/code> method that works as follows:\n<ul class=\"wp-block-list\">\n<li>If <code>S &lt;= elm<\/code> where <code>elm<\/code> is the name in the current <code>Node<\/code>, then <code>S<\/code> is inserted in the left branch.\n<ul class=\"wp-block-list\">\n<li>If <code>left == none<\/code> then <code>S<\/code> is the first element in the left branch and <code>left := Node(S)<\/code> is executed otherwise <code>left.insert(S)<\/code> is executed recursively.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If <code>S &gt; elm<\/code> then <code>S<\/code> is inserted in the right branch.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A print <code>method<\/code> not shown.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">When a <code>Node<\/code> is inserted into the tree it is ordered based on a lexicographical ordering. I.e. &#8220;Dave&#8221; comes before &#8220;John&#8221; (&#8220;Dave&#8221; &lt; &#8220;John&#8221;).<\/p>\n\n\n\n<p class=\"has-pale-cyan-blue-background-color has-background has-tiny-font-size wp-block-paragraph\">Preliminary figure<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"723\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-I-1024x723.jpg\" alt=\"\" class=\"wp-image-6273\" style=\"width:425px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-I-1024x723.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-I-300x212.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-I-768x542.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-I.jpg 1394w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We may then declare two <code>BinaryTree<\/code> objects and insert some elements into them:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>boys: <strong>obj<\/strong> BinaryTree\ngirls: <strong>obj<\/strong> BinaryTree\nboys.insert(\"Peter\")\ngirls.insert(\"Cecilie\")\ngirls.insert(\"Maria\")\nboys.insert(\"Robin\")\n... <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next we add a coroutine, <code>theScanner<\/code>, that traverse the tree and for each node in the tree, it suspends execution and returns the value at the node. If the tree has n nodes it returns a sequence of <code>String<\/code> values where <code>V1 &lt;= V2 &lt;= V3 &lt;= ... &lt;= Vn.<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   <strong>class<\/strong> BinaryTree: \n      -\"-\n      theScanner: <strong>obj<\/strong>\n         CV: <strong>var<\/strong> String\n         next -&gt; V: var String:\n          <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">  <\/mark>resume(theScanner)\n            V := CV\n         scan(current: <strong>ref<\/strong> node):\n            if (current =\/= none) :then \n               scan(current.left)\n               CV := current.elm\n               theScanner.suspend\n               scan(current.right)\n         theScanner.suspend\n         scan(root)\n         CV := \"\"\n         theScanner.suspend\n      next -&gt; V: <strong>var<\/strong> String:\n         V := theScanner.next<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally we may add a method, <code>merge<\/code> that merges the values of two binary search trees:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   merge: \n      nextBoy: <strong>var<\/strong> String\n      nextGirl: <strong>var<\/strong> String\n      tail(T: <strong>ref<\/strong> BinaryTree):\n         cycle\n            S: <strong>var<\/strong> String\n            S := T.next\n            if (S = \"\") :then \n               leave(tail)\n            :else \n               S.print\n      nextBoy := boys.next\n      nextGirl := girls.next\n      loop: <strong>do<\/strong>\n         if (nextBoy &lt;= nextGirl) :then\n            if (nextBoy = \"\") :then\n               nextGirl.print\n               tail(girls) \n            :else \n               nextBoy.print\n               nextBoy := boys.next\n               restart(loop)\n         :else\n            -- nextBoy &gt; nextGirl\n            if (nextGirl = \"\") :then \n               nextBoy.print\n               tail(boys) \n            :else \n               nextGirl.print\n               if (nextBoy.length &gt; 0) :then \n                  nextGirl := girls.next\n               restart(loop)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"431\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-1024x431.jpg\" alt=\"\" class=\"wp-image-6275\" style=\"width:767px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-1024x431.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-300x126.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-768x323.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-1536x646.jpg 1536w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/07\/BinTree-II-2048x862.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The merge method works ass follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The statements: <code>nextBoy := boys.next<\/code> and <code>nextGirl := girls.next<\/code> assigns the first boy and first girl to <code>nextBoy<\/code> and <code>nextGirl<\/code> respectively where the ordering is alphabetical.<\/li>\n\n\n\n<li>If <code>nextBoy &lt;= nextGirl<\/code>, then <code>nextBoy<\/code> is printed and <code>nextBoy<\/code> is assigned the next boy from the tree using <code>boys.next<\/code>.<\/li>\n\n\n\n<li>If <code>nextBoy &gt; nextGirl<\/code>, then <code>nextGirl<\/code> is printed and assigned the next girl.<\/li>\n\n\n\n<li>This is repeated until no more boys and girls in the trees.<\/li>\n\n\n\n<li>The termination condition is that the empty string (<code>\"\"<\/code>) is returned by <code>next<\/code> if no more girls\/boys in a tree. <\/li>\n\n\n\n<li>if the boys tree becomes empty before the girls tree, then the method invocation <code>tail(girls) <\/code>prints the remaining girls in the tree &#8211; similarly if the girls tree becomes empty before the boys tree.<\/li>\n<\/ol>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=6209\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this section, we show examples of how to use coroutines to describe a certain class of algorithms often referred to as interlocked sequential execution stacks. A&nbsp;generator&nbsp;is a coroutine capable of producing a sequence of values. A new value is produced for each resume of the coroutine. The next value depends on the sequence of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1961,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-6209","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/6209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6209"}],"version-history":[{"count":86,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/6209\/revisions"}],"predecessor-version":[{"id":10445,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/6209\/revisions\/10445"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}