<p>Simply put, it could recurse infinitely.</p>
<p>Haskell doesn't have loops, so this is the equivalent of a <code>while</code> loop or something, where the exit case is the <code>Contract</code> monad throwing an error.</p>
<p>If you want to learn more, check out this page on <a href="https://wiki.haskell.org/Tail_recursion" rel="nofollow noreferrer">tail recursion</a> in Haskell.</p>

CC BY-SA 4.0
2021-08-03T19:55:31.153Z
2021-08-03T19:55:31.153Z
Mitchell Turner
<p><code>payContract</code> blocks on the first line of the <code>do</code> block until the pay endpoint is invoked. The input of the invocation is put in <code>pp</code>.</p>
<pre><code>pp &lt;- endpoint @&quot;pay&quot;
</code></pre>
<p>It then executes the endpoint invocation by creating the transaction and submitting it, and returns to the blocked state waiting for the next invocation.</p>
<p>It does recurse indefinitely, thus ensuring that the contract endpoints are available.</p>

CC BY-SA 4.0
2021-08-04T04:16:19.253Z
Thanks @vcwebco - that makes sense. When I am running the Emulator from the cmd line by calling payTest1: 
payTest1 :: IO ()
payTest1 = runEmulatorTraceIO $ payTrace 1000000 2000000
does the payContract fn keep running while it's blocked listening for the endpoint call in a separate thread even after payTrace returns?

How does the Emulator know to stop listening for endpoint calls?
marcel_g
CC BY-SA 4.0
2021-08-04T12:53:33.780Z
@marcel_g - contracts get activated within the context of a wallet, e.g in payTrace with code line h <- activateContractWallet (Wallet 1) payContract. That makes the endpoint(s) available for invocation, e.g. with callEndpoint @"pay" h $ PayParams. Note that activation is not global. The visual representation in the playground simulator shows wallets with the contract already activated (buttons), and with the ability to add more.
vcwebco
CC BY-SA 4.0
2021-08-03T10:58:27.663Z
2021-08-03T10:58:27.663Z
vcwebco
<p>In the solution for the Lecture #4 (cohort 1 I guess, I'm following on Youtube)</p>
<pre><code>payContract :: Contract () PaySchema Text () 
payContract = do
      pp &lt;- endpoint @&quot;pay&quot;
      let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
      handleError (\err -&gt; Contract.logInfo $ &quot;caught error: &quot; ++ unpack err) $ void $ submitTx tx
      payContract 
</code></pre>
<p>It appears that payContract calls itself, and this is required to get the payTest1 and payTest2 functions to actually run.</p>
<p>My question is: how does this not recurse infinitely? Where is the condition that stops it?</p>
<p>I'm still learning Haskell as I go and I come from a C# background, so I'm still getting my head around the functional programming thing.</p>
<p>TIA</p>

<plutus><smart-contract><plutus-pioneer-program><haskell>
Pioneer Lecture 4 recursion - how is it not infinite?
# Pioneer Lecture 4 recursion - how is it not infinite?

(Asked by: username_0 on 2021-08-03)

In the solution for the Lecture #4 (cohort 1 I guess, I'm following on Youtube)
payContract :: Contract () PaySchema Text () 
payContract = do
      pp <- endpoint @"pay"
      let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
      handleError (\err -> Contract.logInfo $ "caught error: " ++ unpack err) $ void $ submitTx tx
      payContract 

It appears that payContract calls itself, and this is required to get the payTest1 and payTest2 functions to actually run.
My question is: how does this not recurse infinitely? Where is the condition that stops it?
I'm still learning Haskell as I go and I come from a C# background, so I'm still getting my head around the functional programming thing.
TIA

**Tags:** plutus, smart-contract, plutus-pioneer-program, haskell

---

## 2 answers

### Answer by username_2 on 2021-08-03. Score: 3 | Accepted answer

payContract blocks on the first line of the do block until the pay endpoint is invoked. The input of the invocation is put in pp.
pp <- endpoint @"pay"

It then executes the endpoint invocation by creating the transaction and submitting it, and returns to the blocked state waiting for the next invocation.
It does recurse indefinitely, thus ensuring that the contract endpoints are available.

> **Comments:**
> **username_0**: Thanks @vcwebco - that makes sense. When I am running the Emulator from the cmd line by calling payTest1:  payTest1 :: IO () payTest1 = runEmulatorTraceIO $ payTrace 1000000 2000000 does the payContract fn keep running while it's blocked listening for the endpoint call in a separate thread even after payTrace returns?  How does the Emulator know to stop listening for endpoint calls?  
> **username_2**: @marcel_g - contracts get activated within the context of a wallet, e.g in payTrace with code line h <- activateContractWallet (Wallet 1) payContract. That makes the endpoint(s) available for invocation, e.g. with callEndpoint @"pay" h $ PayParams. Note that activation is not global. The visual representation in the playground simulator shows wallets with the contract already activated (buttons), and with the ability to add more.  

---

### Answer by username_1 on 2021-08-03. Score: 2

Simply put, it could recurse infinitely.

Haskell doesn't have loops, so this is the equivalent of a `while` loop or something, where the exit case is the `Contract` monad throwing an error.

If you want to learn more, check out this page on [tail recursion](https://wiki.haskell.org/Tail_recursion) in Haskell.


---

