Skip to content
CCPEDIAby Unity Nodes
VideosLesson 10: Iterative Control Flow

Lesson 10: Iterative Control Flow

Nothing is loaded from YouTube until you press play, and then without tracking cookies.

About this video

00:27 Recursion 01:42 Map function 02:32 Folds 05:43 Review

For a better learning experience, sign up and enroll in this free course at: daml.talentlms.com/plus/catalog/courses/141

Transcript

1,146 words

Captions as YouTube publishes them, usually machine-generated, so names such as Daml and Canton can be misheard.

  1. 0:01

    There's an important difference between the loops you may have seen in other languages and what we have in Daml. Variables in Daml are immutable. So we can't use loop constructs such as a while or for-loops because we don't have variables that can change their value iterably.

  2. 0:17

    We're gonna look at three ways you can perform iterations in Daml using recursion, map functions and finally, the use of folds. In a recursive function, the function calls itself with an argument value different from what it was called with previously. It's often used in mathematical definitions of functions.

  3. 0:36

    For example, in calculating a Fibonacci number. The mathematical definition of a Fibonacci function is that given N, it calculates the sum of the last two numbers in the series. For example, the sum of numbers at the position, N minus one and N minus two in the series.

  4. 0:53

    So to compute Fibonacci N, you need to compute Fibonacci N minus one and Fibonacci N minus two. The function recursively returns until you hit the base case or edge case for N equal to two or when the function simply returns one.

  5. 1:09

    The code here shows the Fibonacci function implemented using guards. If N is less than or equal to two, the function simply returns one. This is the base. If it's any other number, it calls the Fibonacci function twice. First with N minus two as the argument, and second with N minus one as the argument.

  6. 1:27

    Each of these calls recursively goes back until it hits the base case and delivers the final results. Recursion is often easy to read, but hard to implement in code. So if you find this difficult, there are other ways to perform iterations in Daml.

  7. 1:42

    Daml has functions that we can use to iterate over a structure such as a list or set. One such function is map, which takes two inputs, a function and a list, and then it applies the function to each element of the list and returns a new list.

  8. 1:58

    The example here demonstrates creating a function called modulaUsingMap that takes a list of ints X and an int Y. It needs to return a list that contains modula y of each element in the list. So it uses the map function, which takes modula Y as the function and X as the list.

  9. 2:17

    When the function modulaUsingMap is called in the debug statement below with a list and a number three, the result is a list where each number is taken at a time and computed for modula three. Let's look at the third approach to a looping and will using folds. Folds are similar to the map function, but with an accumulator.

  10. 2:36

    Map functions aren't well suited for cases where iterating over a list you need to use the result of the previous iteration; folds work well for this use case. With each iteration, they store the result in the accumulator and then use it in the following iteration.

  11. 2:51

    Daml provides several functions that perform folds to iterate over a structure such as a list. The two we're using in this example are foldl and foldr. The former iterates over a list from left to right and the later from right to left.

  12. 3:06

    A fold function takes three arguments; a binary function, a starting value for the accumulator, and a list. It starts with the initial value in the accumulator, it then takes a value from the list and applies the binary function to the two values.

  13. 3:21

    The accumulator continues to collect the result of the function until all list elements have been covered. It then returns the final value of the accumulator as the return value of the function. In this example, let's look at sumList.

  14. 3:36

    Its signature shows that it takes a list of ints and returns an int. In the definition it has foldl followed by the plus operator enclosed in parentheses. The plus symbol is usually used as infixed function, but when we enclose it in parentheses, it works just like a regular function or prefix function where the function name comes first, followed by its argument.

  15. 3:58

    Then we have zero, which is the starting value of the accumulator for foldl to work with. With this accumulator, foldl adds each element in the list X. Starting from the left in the script below, we pass the range list 1-4 and it prints 10.

  16. 4:14

    Now we know there is already a library function called sum that does this, so let's look at a slightly different example that uses foldl. The second function here is listToText, which takes a list of text and returns a flat text string.

  17. 4:29

    Following the same approach to parsing foldl statements, we name the function that needs to be applied to each element in the list, then the initial value of the accumulator, and then the actual list. Notice that the function to be applied here is actually a Lambda function that takes two parameters. Acc, which is the accumulator, and Y, which is an element.

  18. 4:50

    It concatenates ACC with show Y. Since the value and Y is going to be an int, we need to convert it to text by using show. So the fold function will take an empty string and the first element of the list, pass them to the Lambda function, and store the return value of the Lambda function in the accumulator.

  19. 5:08

    Then it'll take the value of the accumulator and the second element of the list, pass them to the Lambda function, and update the value of the accumulator with the return value of the lambda. This process is repeated for the remaining elements of the list, and then the final value of the accumulator is used with the return value of the foldl.

  20. 5:27

    When you call this function with the same range, list 1-4, you get the list flattened as string 1, 2, 3, 4. You can replace foldl with foldr and switch the position of the accumulator and element. This change will help you get the reverse list to text functionality.

  21. 5:43

    So let's look at what we've covered. We've looked at writing code that can be repeated using recursion, maps, or folds. Recursive functions call itself in a definition and need a base case to make an exit. Map takes a function and applies it to a list, returning a new list that contains the result of the function application to each element of the list.

  22. 6:05

    Foldl does a similar thing, except it takes a binary function in addition to a list, and it also takes an initial value for the accumulator. At each iteration of the list, the binary function is applied to the accumulator and the current element of the list and the value of the accumulator is updated with the return value of the binary function.