# Tail recursions in DAML

**URL:** <https://forum.canton.network/t/tail-recursions-in-daml/6685>\
**Category:** App Development\
**Tags:** daml\
**Created:** [June 18, 2023, 9:07pm UTC](https://forum.canton.network/t/tail-recursions-in-daml/6685 "2023-06-18T21:07:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [June 18, 2023, 9:07pm UTC](https://forum.canton.network/t/tail-recursions-in-daml/6685/1 "2023-06-18T21:07:32Z")

</div>

G-d willing

Hello,  
I have a question about tail recursion.  
for example, I want to write a tail recursion that calculates the sum of numbers from 1 to some number.  
The following function works:

```haskell
sumNumbers : Int -> Int
sumNumbers number =
  tailSumNumbers number 0

tailSumNumbers : Int -> Int -> Int
tailSumNumbers number acc
  | number == 0 = acc
  | otherwise = tailSumNumbers (number -1) (acc + number)

```

However, when I am trying to do it like that it is not working:

```haskell
sumNumbers : Int -> Int
sumNumbers number =
  tailSumNumbers number 0
    where
      tailSumNumbers number acc
        | number == 0 = acc
        | otherwise = tailSumNumbers (number -1) (acc + number)

```

And it also does not accept this version

```haskell
sumNumbers : Int -> Int
sumNumbers number = 
  let tailSumNumbers number acc  
        | number == 0 = acc
        | otherwise = tailSumNumbers (number-1) (acc + number) 
  in 
  tailSumNumbers number 0

```

The error is:

```haskell
Failure to process Daml program, this feature is not currently supported.
Local variables defined recursively - recursion can only happen at the top level.

```

Is it possible to have this solution inside 1 function only, and not 2?

---

<div class="post-metadata">

**Author:** ![Remy](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/remy/32/595_2.png) [@Remy](https://forum.canton.network/u/Remy)\
**Post date:** [June 19, 2023, 10:46am UTC](https://forum.canton.network/t/tail-recursions-in-daml/6685/2 "2023-06-19T10:46:01Z")

</div>

This is a known limitation of Daml that affects general recursion, not just tail recursion.  
As the error message tries to explain, only top level functions can be recursive, but not those defined within `let` or `where` clauses.

Therefore, if you want to write recursive functions, only the first definition is valid and not the other two.
