{"id":"ntiCTp3yw-8","title":"Lesson 5: Working with Functions","description":"00:28 Associativity\n02:22 Carrying\n03:24 Infix notation\n04:08 Lambda functions\n05:02 Review\n\nFor a better learning experience, sign up and enroll in this free course at: https://daml.talentlms.com/plus/catalog/courses/141","channel_name":"Digital Asset Training","published_at":"2025-02-19T00:00:00.000Z","thumbnail_url":"https://i.ytimg.com/vi/ntiCTp3yw-8/hqdefault.jpg","transcript_text":"Daml has a number of useful conventions that make code easier to readd, but you have to understand these conventions. So let's dig into functions and learn about left and right associativity, currying, in-fixed notations, and lambda functions. We won't go in depth into any of these concepts, but they should all be terms and ideas you're comfortable with. You may want to take a moment to review these later or do some study on your own. With that said, let's get started. Associativity is a property of binary operations or functions. If a function is associative, it means that rearranging the order of the expressions won't change the results. For example, the adddition function is associative. You can parse it from left to right or from right to left, and the result will come out to be the same. But some functions are required to be parsed from left to right. This is called left associativity. For example, subtraction and division are left associative function. . The correct way to compute X minus Y minus Z is to first compute X minus Y and then subtract Z from the result. Changing the placement of parentheses as shown here for two minus five, minus three will produce different results. In a similar way, a right associative function needs to be evaluated from right to left. An example of this is the exponential. given X, raise to Y, raise to Z, we compute Y, raise to Z and then raise X to the result. Changing the placement of parentheses in the expression two, raise to three, raise to four will produce two different results. So how is this applied in Daml? Going back to the addd function example, the functions application is left associative. That means that given addd X Y, we first take add, which becomes a new intermediate function to which Y is supplied as the argument. The intermediate function add X is what's known as the partial application of the add function, but when parsing a function signature, we go from right to left. This means that in the add function signature, we interpret it as a function that takes one argument and returns another function that takes the rest of the arguments. Interpreting a function signature this way and combining it with a concept of partial application leadds us to this concept of curing. Curing is the process of converting a function with multiple arguments to a function that takes a single argument and returns another function. Let's go back to the add function. When evaluating, we take add X as partial application of add and then supply Y to this intermediate function. We also know that the increment function takes one argument and addds one. So effectively increment y is add 1 Y, and that means increment is partial application of add. So we could say increment Y is the curried form of add one Y. So why is associativity, partial application and currying important for us to understand? It's because all functions in Daml are curried. All Daml functions can be viewed as taking one argument and returning a function that takes the rest of the argument. Currying when used well, allows us to write complex expressions in a reader-friendly way. Since functions are the core of Daml, it's important to understand all the ways they're used in the language. So far, we've looked at the add function where we write add, and then it's two operands. Functions can also be written using infix notation. For example, add function's infix notation is the plus symbol between the two operands. Common math symbols such as plus, minus, multiply, and divide can be used as you would expect them in any programming language. For named functions like add and increment, Daml supports writing functions using infox notation by enclosing the function in back ticks. And since we can apply any function with fixed notation, we can also take any function and apply using prefix notation. Want to use the plus symbol as a prefix notation? Just enclose it in parentheses. Let's wrap this all up by reviewing Lambda functions. A Lambda function has a few unique characteristics. They use inline notation. They don't have a name and are used only once in the place where they're written. Since Lambda functions are anonymous, they can't be called later or reused. A Lambda function definition is enclosed in parentheses with a back slash as the first charact. Then we write the arguments the function takes, followed by a right arrow and the function definition. As an example, we can write the increment function as open parenthesis slash N. Here, N is the input argument. After the right arrow, we write N plus one, which is the result to be produced. Like many of the conventions we've gone over, Lambda functions can help us make our code more concise, especially if we know that the function won't be used anywhere else. Let's review what we covered. Associativity is a property of functions and describes how a function is parsed in Daml when evaluating a function. We parse them from left to right, but when we parse their signature, we go from right to left. Associativity introduced us to the concept of partial application, which then leadds to the idea of currying. Currying is when we take a function with several arguments and turn it into a function that takes only one argument and maps it to another function that accepts the rest of the original function; creating a chain of single argument functions. Binary functions, such as add, that take two arguments can be written with prefix or infix notation. We can write the add function followed by its two arguments, or we can write it as an in ixed notation with the function enclosed in back ticks and surrounded by its arguments on both sides. Finally, the Lambda function is a concise way of writing functions that we only need to use once; helping us keep our code clean and readdable.","transcript_synced_at":"2026-10-26T04:03:32.408Z","segments":[{"start_seconds":0,"text":"Daml has a number of useful conventions that make code easier to readd, but you have to understand these conventions.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=0s"},{"start_seconds":7,"text":"So let's dig into functions and learn about left and right associativity, currying, in-fixed notations, and lambda functions.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=7s"},{"start_seconds":15,"text":"We won't go in depth into any of these concepts, but they should all be terms and ideas you're comfortable with. You may want to take a moment to review these later or do some study on your own.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=15s"},{"start_seconds":25,"text":"With that said, let's get started. Associativity is a property of binary operations or functions.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=25s"},{"start_seconds":33,"text":"If a function is associative, it means that rearranging the order of the expressions won't change the results. For example, the adddition function is associative.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=33s"},{"start_seconds":42,"text":"You can parse it from left to right or from right to left, and the result will come out to be the same. But some functions are required to be parsed from left to right.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=42s"},{"start_seconds":50,"text":"This is called left associativity. For example, subtraction and division are left associative function.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=50s"},{"start_seconds":57,"text":". The correct way to compute X minus Y minus Z is to first compute X minus Y and then subtract Z from the result.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=57s"},{"start_seconds":66,"text":"Changing the placement of parentheses as shown here for two minus five, minus three will produce different results.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=66s"},{"start_seconds":74,"text":"In a similar way, a right associative function needs to be evaluated from right to left. An example of this is the exponential.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=74s"},{"start_seconds":83,"text":"given X, raise to Y, raise to Z, we compute Y, raise to Z and then raise X to the result.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=83s"},{"start_seconds":92,"text":"Changing the placement of parentheses in the expression two, raise to three, raise to four will produce two different results. So how is this applied in Daml?","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=92s"},{"start_seconds":101,"text":"Going back to the addd function example, the functions application is left associative.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=101s"},{"start_seconds":106,"text":"That means that given addd X Y, we first take add, which becomes a new intermediate function to which Y is supplied as the argument.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=106s"},{"start_seconds":115,"text":"The intermediate function add X is what's known as the partial application of the add function, but when parsing a function signature, we go from right to left.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=115s"},{"start_seconds":125,"text":"This means that in the add function signature, we interpret it as a function that takes one argument and returns another function that takes the rest of the arguments.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=125s"},{"start_seconds":135,"text":"Interpreting a function signature this way and combining it with a concept of partial application leadds us to this concept of curing.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=135s"},{"start_seconds":142,"text":"Curing is the process of converting a function with multiple arguments to a function that takes a single argument and returns another function.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=142s"},{"start_seconds":150,"text":"Let's go back to the add function.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=150s"},{"start_seconds":152,"text":"When evaluating, we take add X as partial application of add and then supply Y to this intermediate function.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=152s"},{"start_seconds":160,"text":"We also know that the increment function takes one argument and addds one.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=160s"},{"start_seconds":164,"text":"So effectively increment y is add 1 Y, and that means increment is partial application of add.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=164s"},{"start_seconds":172,"text":"So we could say increment Y is the curried form of add one Y.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=172s"},{"start_seconds":178,"text":"So why is associativity, partial application and currying important for us to understand? It's because all functions in Daml are curried.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=178s"},{"start_seconds":187,"text":"All Daml functions can be viewed as taking one argument and returning a function that takes the rest of the argument.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=187s"},{"start_seconds":193,"text":"Currying when used well, allows us to write complex expressions in a reader-friendly way.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=193s"},{"start_seconds":198,"text":"Since functions are the core of Daml, it's important to understand all the ways they're used in the language.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=198s"},{"start_seconds":204,"text":"So far, we've looked at the add function where we write add, and then it's two operands. Functions can also be written using infix notation.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=204s"},{"start_seconds":212,"text":"For example, add function's infix notation is the plus symbol between the two operands.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=212s"},{"start_seconds":218,"text":"Common math symbols such as plus, minus, multiply, and divide can be used as you would expect them in any programming language.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=218s"},{"start_seconds":226,"text":"For named functions like add and increment, Daml supports writing functions using infox notation by enclosing the function in back ticks.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=226s"},{"start_seconds":235,"text":"And since we can apply any function with fixed notation, we can also take any function and apply using prefix notation.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=235s"},{"start_seconds":242,"text":"Want to use the plus symbol as a prefix notation? Just enclose it in parentheses. Let's wrap this all up by reviewing Lambda functions.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=242s"},{"start_seconds":251,"text":"A Lambda function has a few unique characteristics. They use inline notation. They don't have a name and are used only once in the place where they're written.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=251s"},{"start_seconds":262,"text":"Since Lambda functions are anonymous, they can't be called later or reused.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=262s"},{"start_seconds":266,"text":"A Lambda function definition is enclosed in parentheses with a back slash as the first charact.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=266s"},{"start_seconds":273,"text":"Then we write the arguments the function takes, followed by a right arrow and the function definition. As an example, we can write the increment function as open parenthesis slash N.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=273s"},{"start_seconds":284,"text":"Here, N is the input argument. After the right arrow, we write N plus one, which is the result to be produced.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=284s"},{"start_seconds":293,"text":"Like many of the conventions we've gone over, Lambda functions can help us make our code more concise, especially if we know that the function won't be used anywhere else.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=293s"},{"start_seconds":302,"text":"Let's review what we covered. Associativity is a property of functions and describes how a function is parsed in Daml when evaluating a function.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=302s"},{"start_seconds":312,"text":"We parse them from left to right, but when we parse their signature, we go from right to left.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=312s"},{"start_seconds":318,"text":"Associativity introduced us to the concept of partial application, which then leadds to the idea of currying.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=318s"},{"start_seconds":324,"text":"Currying is when we take a function with several arguments and turn it into a function that takes only one argument and maps it to another function that accepts the rest of the original function; creating a chain of single argument functions.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=324s"},{"start_seconds":336,"text":"Binary functions, such as add, that take two arguments can be written with prefix or infix notation.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=336s"},{"start_seconds":343,"text":"We can write the add function followed by its two arguments, or we can write it as an in ixed notation with the function enclosed in back ticks and surrounded by its arguments on both sides.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=343s"},{"start_seconds":353,"text":"Finally, the Lambda function is a concise way of writing functions that we only need to use once; helping us keep our code clean and readdable.","url":"https://www.youtube.com/watch?v=ntiCTp3yw-8&t=353s"}],"youtube_url":"https://youtube.com/watch?v=ntiCTp3yw-8","url":"https://ccpedia.xyz/videos/ntiCTp3yw-8"}