{"id":"tA-aEDJ2tJc","title":"Lesson 1: Functional Programming Part 1","description":"For a better learning experience, sign up and enroll in this free course at: https://daml.talentlms.com/plus/catalog/courses/150","channel_name":"Digital Asset Training","published_at":"2025-02-20T00:00:00.000Z","thumbnail_url":"https://i.ytimg.com/vi/tA-aEDJ2tJc/hqdefault.jpg","transcript_text":"Hi Damlers, welcome to the Daml Contract Developer program. Before we dive into the main content, let's take a moment to review some of the most essential concepts that you should have mastered prior to starting this program. The purpose of these review lessons is to remind you of the main concepts introduced throughout the Daml Fundamentals at a high level. As you ::pro-GRESS:: through the lessons, if there are any concepts that you don't feel comfortable with, you can pause the video and revisit the relevant lessons in the Fundamentals program. The review lessons are designed to provide you with an opportunity to refresh your understanding of the concepts before delving into the more advanced material. Then, you can focus on learning new concepts. If you’re ready, let's get started. This course won’t go over every data type in Daml, but let’s take a look at some of the most commonly used ones. Int values are positive and negative whole numbers that range between the two large numbers you see in the table. You can add underscores for readability. The Decimal data type is short for the Numeric 10 data type and is used for creating decimal numbers. If you’re interested in learning more about Numeric data types, you can explore “Numeric En” for fixed decimal numbers and “BigNumeric” for large fixed point decimal numbers. The Text data type is a sequence of unicode code points wrapped in a pair of double quotes. The Bool type is for storing True or False. There is also a Date type for creating date values and a Time type for time values. To create a Date value, you must first import the DA.Date library to use the “date” function. To create a Time value, you must import the DA.Time library in addition to the DA.Date library. This is necessary because to create a time value, you must call the “time” function, which takes a date value as its first argument. RelTime values are used to indicate the differences between two time values. “subTime” takes two arguments that are Time values and returns the difference of the two as RelTime. Let’s also review some of the data types that are most frequently used for updating a ledger. First, there’s the Party type. In Daml, Party represents an entity, and is a built-in data type. For writing test scripts, you can call the function allocateParty or allocatePartyWithHint to create a Party and interact with a ledger. Contract ID is an opaque data type that represents an ID for a contract created from a template, ::AE::. When we create a contract using the create function, the Contract ID of the newly created contract is returned as an Update. One thing to note is that the Contract ID that is returned is a temporary identifier. It is not the same as the one that will be publicly visible if the transaction is successfully committed to the ledger. This is why the Contract ID type must be opaque in Daml. For the purpose of this course, you can focus on the fact that Contract ID is a reference to a contract, and you can exercise a choice or fetch a contract using a Contract ID. Update represents an action executed within a Daml application running on the ledger. On the other hand, Script simulates the submission of Commands from a Daml client application to the ledger, as the client application is executed off the ledger. Commands is a set of commands that are submitted as a single transaction. The submit function that is often used in a test script takes two arguments: Party and Commands. You can pass a set of commands to the submit function as its second argument to test creating and exercising commands. The create and exercise functions that have a “C-M-D” suffix indicate that they evaluate to the type Commands. From a user standpoint, this set of commands produces what’s called an “atomic ledger transaction” ensuring that operations either succeed entirely or fail without partial changes. You will explore this concept in more depth later in the course. Finally, let’s look at the collection data types. To use the functions to manipulate collection data types, make sure to import the corresponding DA libraries, such as “DA”List, DA.tuhpl, “DA”Set, and “DA”Map. List can hold many elements of one data type. You can use the “head” function to get the first element of a non-empty List and “tail” to get the rest. The “last” function gives you the last element of a List, and you can use the “sort” function to sort a List. There are many List functions, and this course won’t be exploring all of them, but you can find them in the DA.List section of the official Daml Documentation. There are twenty tuhpl types for various combinations of values, and each is distinct from the others. Depending on the type of a tuhpl, you must use different functions. For instance “FST”, as in “first,” is used for a two-tuhpl, but “FST 3” for a three-tuhpl. You can also simply use dot _1, dot _2, and so on, which can be called on all types of tuples. There are certain functions that work up to a five-tuhpl, but for anything beyond a two-tuhpl, you should use records, which will be covered later. Note that a tuhpl is a fixed length, while a List can be of any length. As a result, you can call functions such as append, head, and tail on a List, but there are no equivalent functions for Tuples because Tuples are different. The Set type holds unique elements of a certain data type, and the Map type is used for storing key-value pairs. In a Map, all the keys must be of the same type and the same is true for the values. You can’t store keys or values of different data types. Using the “fromList” function, you can create a Map from a list of key-value pairs. Notice that Set and Map are labeled with the words “Set” and “Map,” unlike List and tuhpl. Without those labels, Set is essentially a List of unique elements, and Map is a list of Tuples. Moving on to functions. A Daml function consists of two parts: the function signature and the function definition. The function signature specifies the input type and the return type of the function. For instance, the addTwoNums function takes two Int values as arguments, and it evaluates to an Int. A function signature is right associative, meaning it evaluates from right to left, and the right arrow reeads as “maps to.” So the correct way to read this function signature is “Int maps to a function that maps ‘Int’ to ‘Int.’” Function definition is where the actual logic of the function is defined. This function takes two input parameters “A and B”, and the sum value of “A and B” is what the function evaluates to. Unlike the function signature, the function definition is left associative, meaning it evaluates from left to right. In other words, in the “add-Two-Nums-A-B” example, “add-Two-Nums-A” is evaluated first to return a new function that takes “Be” as an argument. Although it seems like you are passing both “A and B” at the same time, the way a Daml function works is that it precisely only takes one argument at a time, returning a new function that takes the next argument. This behavior is repeated until it takes all of the arguments that it’s designed to take. Once all the arguments are passed in, the function finally evaluates to a return value. This behavior of repetitively taking in arguments and returning a new function until it’s ready to evaluate is called “currying” in programming. All Daml functions are curried, and currying is an important concept to internalize for you to understand more advanced concepts later on. In addition to the regular function you saw in the previous slide, there are lambda and infix functions. A lambda function is written in inline notation to define an anonymous function for a single time usage. A lambda function is defined in parentheses with a backslash. After the backslash, you specify a parameter followed by a right arrow. After the right arrow, you write the function definition. For example, if you write an increment function as a lambda function, you first open up a parenthesis and add backslash and En, the parameter. After the right arrow, you write “N + 1”, which is what the function evaluates to. Lambda functions can help make code concise, especially if you know that the function won’t be used anywhere else. Functions can also be written using infix notation. One example is the math operator “+” sign. Common math operators such as plus, minus, multiply, and divide can be used as you normally would in any programming language. For named binary functions like “add-Two-Nums,” which takes two arguments, Daml supports writing functions using infix-notation by enclosing the function in back-ticks. In the “infix-Add” function, “add-Two-Nums” is wrapped in a pair of back-ticks and placed between the two parameters “X and Y”. Calling the add-Two-Nums function this way is the same as calling it using the prefix notation where you write the function name first, followed by all of its arguments. You can also do the opposite: the math operator “+” can be called in the same way any other named functions are, rather than placing it between two numbers. Be sure to wrap the + sign in puhrentheseez. Let’s review some of the most commonly used iterative functions. Daml is a declarative programming language, which means that it is designed to describe what a program should do, rather than how to do it. As a result, it does not have traditional imperative constructs such as “for-loops” or “while-loops” that specify the details of how a program should perform its computations. Instead of using loops, in Daml, you can use recursion or higher-order functions that take other functions as arguments, such as maps and folds. A recursive function calls itself in its definition, and there must be a base case to exist out of the recursive call. One thing to remember is that, in Daml, recursion works only at the top-level, and recursive sub-functions are not supported. For example, if you define and call a recursive sub-function within a recursive function, you will get an error that “this feature is not supported, recursion can only happen at the top level.” There are variations of mapping functions. You have been introduced to map, which is specifically used for mapping a List, and “F-map”, which is the general version of map. map takes two arguments: a function and a List. It maps a function over every element in a List, and it evaluates to a new List with the updated elements in it. For example, the modulo-Using-Map function takes two arguments: a List of Ints and an Int, and it returns a new List of Ints. As the map function iterates through the List of Ints, each element is modded by 3. As a result, it evaluates to a new List of two zero one two. Folds are similar to map in the way that they apply a given function to each element of a List. However, unlike map, folds accumulate into a single result as they iterate through a List to evaluate to a single aggregated value instead of returning a new List. There is “fold-L”, which iterates the List input from left to right, and there’s “fold-R”, which iterates from right to left. This lesson will use “fold-L”. “fold-L” takes three arguments: a binary function, which is a function that takes two arguments, an initial value as an accumulator, and a List. As the List is iterated, the function is applied to the accumulator and the current element of the List. For example, the “sum-List” function takes a List of Ints. As the List of one two three four is iterated, each element of the List is accumulated to the initial value of 0. As a result, it evaluates to 10. Let’s review conditional expressions. The first one is the if-then-else construct. In some other languages, an if statement works without an else, but in Daml, you must provide an else to cover both sides of the boolean condition. That’s because if-then-else is not a function or a statement in Daml; it’s an expression. You should treat it as a function call where you must provide all of the valid arguments to get an output. If you have several conditions to check, you can use Guards for readability instead of if-then-else. You can specify all the conditions using the pipe symbol, including the “otherwise” at the end as a catchall. To keep your code dry, you can open a where-block and bind the repeated formula to a variable. You can also use the case statement, which relies on pattern matching instead of boolean conditions. For example, the “is-Even” function takes an Int and returns a Text. It matches the remainder of input divided by 2 with one of the listed patterns. If the remainder is 0, it will evaluate to the text “even.” In all other cases, it will evaluate to “not even.” Just like the if-then-else expression, both guards and case-of require a catchall at the end. There are also informative types to help further elaborate a conditional workflow. The Either data type contains a value that can potentially be either of two specified data types. The Either type is represented as “Either-A-B”, indicating that it can hold a value of either “type A” or “type B”. The Either type has two constructors, 'Left' and 'Right'. The 'Left' constructor encapsulates a value of “type A”, and the 'Right' constructor encapsulates a value of “type B”. For example, the “some-Function” takes an Int and returns either Text or Int. If you pass negative 2 as the “X-value”, which is less than 0, it will return the Text “Error” wrapped in the Left constructor. If you pass 2, it will increment the value by 2 and return 4 as an Int wrapped in the Right constructor. The Either data type can also be used in error handling. In that case, by convention, the Left constructor holds the error value and the Right constructor holds the correct value. On a similar note, the Optional data type is used when a variable may either hold a specific value or may not hold anything. The Optional data type helps define safe functions that otherwise may return an error. For instance, the “safe-Div” function takes two Ints and returns an Optional Int. The initial pattern-match in the function definition uses an underscore to represent the dividend, which acts as a catch-all, and sets the divisor as 0. If this condition is met, the function will return 'None', which is the closest Daml has to a null value. Otherwise, as per the second pattern-match, the function will return 'Some' value of type Int. If you pass 6 and 0 as arguments, it will evaluate to None, since the divisor is 0. If you pass 6 and 2 as arguments, it will evaluate to Some 3. Data types, functions, and conditionals are concepts that are shared across a majority of programming languages. This lesson reviewed how those concepts are implemented differently in Daml to refresh your memory of the basics. Unlike other languages, Daml has data types dedicated to represent various aspects of contracts and testing contracts, such as Contract ID, Commands, Party, Script, and Update. All Daml functions are curried, and there are multiple ways to write conditionals, as well as special data types to further elaborate them.","transcript_synced_at":"2026-10-26T04:01:12.818Z","segments":[{"start_seconds":0,"text":"Hi Damlers, welcome to the Daml Contract Developer program.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=0s"},{"start_seconds":5,"text":"Before we dive into the main content, let's take a moment to review some of the most essential concepts that you should have mastered prior to starting this program.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=5s"},{"start_seconds":14,"text":"The purpose of these review lessons is to remind you of the main concepts introduced throughout the Daml Fundamentals at a high level.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=14s"},{"start_seconds":23,"text":"As you ::pro-GRESS:: through the lessons, if there are any concepts that you don't feel comfortable with, you can pause the video and revisit the relevant lessons in the Fundamentals program.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=23s"},{"start_seconds":34,"text":"The review lessons are designed to provide you with an opportunity to refresh your understanding of the concepts before delving into the more advanced material.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=34s"},{"start_seconds":44,"text":"Then, you can focus on learning new concepts. If you’re ready, let's get started.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=44s"},{"start_seconds":51,"text":"This course won’t go over every data type in Daml, but let’s take a look at some of the most commonly used ones.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=51s"},{"start_seconds":59,"text":"Int values are positive and negative whole numbers that range between the two large numbers you see in the table.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=59s"},{"start_seconds":66,"text":"You can add underscores for readability.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=66s"},{"start_seconds":70,"text":"The Decimal data type is short for the Numeric 10 data type and is used for creating decimal numbers.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=70s"},{"start_seconds":77,"text":"If you’re interested in learning more about Numeric data types, you can explore “Numeric En” for fixed decimal numbers and “BigNumeric” for large fixed point decimal numbers.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=77s"},{"start_seconds":90,"text":"The Text data type is a sequence of unicode code points wrapped in a pair of double quotes.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=90s"},{"start_seconds":97,"text":"The Bool type is for storing True or False. There is also a Date type for creating date values and a Time type for time values.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=97s"},{"start_seconds":107,"text":"To create a Date value, you must first import the DA.Date library to use the “date” function.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=107s"},{"start_seconds":114,"text":"To create a Time value, you must import the DA.Time library in addition to the DA.Date library.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=114s"},{"start_seconds":121,"text":"This is necessary because to create a time value, you must call the “time” function, which takes a date value as its first argument.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=121s"},{"start_seconds":130,"text":"RelTime values are used to indicate the differences between two time values.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=130s"},{"start_seconds":136,"text":"“subTime” takes two arguments that are Time values and returns the difference of the two as RelTime.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=136s"},{"start_seconds":143,"text":"Let’s also review some of the data types that are most frequently used for updating a ledger. First, there’s the Party type.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=143s"},{"start_seconds":153,"text":"In Daml, Party represents an entity, and is a built-in data type.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=153s"},{"start_seconds":158,"text":"For writing test scripts, you can call the function allocateParty or allocatePartyWithHint to create a Party and interact with a ledger.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=158s"},{"start_seconds":168,"text":"Contract ID is an opaque data type that represents an ID for a contract created from a template, ::AE::.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=168s"},{"start_seconds":175,"text":"When we create a contract using the create function, the Contract ID of the newly created contract is returned as an Update. One thing to note is that the Contract ID that is returned is a temporary identifier.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=175s"},{"start_seconds":183,"text":"It is not the same as the one that will be publicly visible if the transaction is successfully committed to the ledger. This is why the Contract ID type must be opaque in Daml.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=183s"},{"start_seconds":190,"text":"For the purpose of this course, you can focus on the fact that Contract ID is a reference to a contract, and you can exercise a choice or fetch a contract using a Contract ID. Update represents an action executed within a Daml application running on the ledger.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=190s"},{"start_seconds":201,"text":"On the other hand, Script simulates the submission of Commands from a Daml client application to the ledger, as the client application is executed off the ledger.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=201s"},{"start_seconds":212,"text":"Commands is a set of commands that are submitted as a single transaction.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=212s"},{"start_seconds":217,"text":"The submit function that is often used in a test script takes two arguments: Party and Commands.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=217s"},{"start_seconds":224,"text":"You can pass a set of commands to the submit function as its second argument to test creating and exercising commands.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=224s"},{"start_seconds":232,"text":"The create and exercise functions that have a “C-M-D” suffix indicate that they evaluate to the type Commands.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=232s"},{"start_seconds":240,"text":"From a user standpoint, this set of commands produces what’s called an “atomic ledger transaction” ensuring that operations either succeed entirely or fail without partial changes.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=240s"},{"start_seconds":252,"text":"You will explore this concept in more depth later in the course. Finally, let’s look at the collection data types.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=252s"},{"start_seconds":260,"text":"To use the functions to manipulate collection data types, make sure to import the corresponding DA libraries, such as “DA”List, DA.tuhpl, “DA”Set, and “DA”Map.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=260s"},{"start_seconds":274,"text":"List can hold many elements of one data type.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=274s"},{"start_seconds":277,"text":"You can use the “head” function to get the first element of a non-empty List and “tail” to get the rest.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=277s"},{"start_seconds":284,"text":"The “last” function gives you the last element of a List, and you can use the “sort” function to sort a List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=284s"},{"start_seconds":291,"text":"There are many List functions, and this course won’t be exploring all of them, but you can find them in the DA.List section of the official Daml Documentation.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=291s"},{"start_seconds":300,"text":"There are twenty tuhpl types for various combinations of values, and each is distinct from the others.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=300s"},{"start_seconds":306,"text":"Depending on the type of a tuhpl, you must use different functions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=306s"},{"start_seconds":311,"text":"For instance “FST”, as in “first,” is used for a two-tuhpl, but “FST 3” for a three-tuhpl.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=311s"},{"start_seconds":317,"text":"You can also simply use dot _1, dot _2, and so on, which can be called on all types of tuples.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=317s"},{"start_seconds":322,"text":"There are certain functions that work up to a five-tuhpl, but for anything beyond a two-tuhpl, you should use records, which will be covered later.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=322s"},{"start_seconds":333,"text":"Note that a tuhpl is a fixed length, while a List can be of any length.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=333s"},{"start_seconds":341,"text":"As a result, you can call functions such as append, head, and tail on a List, but there are no equivalent functions for Tuples because Tuples are different.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=341s"},{"start_seconds":350,"text":"The Set type holds unique elements of a certain data type, and the Map type is used for storing key-value pairs.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=350s"},{"start_seconds":355,"text":"In a Map, all the keys must be of the same type and the same is true for the values.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=355s"},{"start_seconds":364,"text":"You can’t store keys or values of different data types. Using the “fromList” function, you can create a Map from a list of key-value pairs.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=364s"},{"start_seconds":374,"text":"Notice that Set and Map are labeled with the words “Set” and “Map,” unlike List and tuhpl.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=374s"},{"start_seconds":379,"text":"Without those labels, Set is essentially a List of unique elements, and Map is a list of Tuples.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=379s"},{"start_seconds":392,"text":"Moving on to functions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=392s"},{"start_seconds":399,"text":"A Daml function consists of two parts: the function signature and the function definition.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=399s"},{"start_seconds":406,"text":"The function signature specifies the input type and the return type of the function.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=406s"},{"start_seconds":412,"text":"For instance, the addTwoNums function takes two Int values as arguments, and it evaluates to an Int.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=412s"},{"start_seconds":420,"text":"A function signature is right associative, meaning it evaluates from right to left, and the right arrow reeads as “maps to.”","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=420s"},{"start_seconds":429,"text":"So the correct way to read this function signature is “Int maps to a function that maps ‘Int’ to ‘Int.’” Function definition is where the actual logic of the function is defined.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=429s"},{"start_seconds":441,"text":"This function takes two input parameters “A and B”, and the sum value of “A and B” is what the function evaluates to.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=441s"},{"start_seconds":452,"text":"Unlike the function signature, the function definition is left associative, meaning it evaluates from left to right.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=452s"},{"start_seconds":460,"text":"In other words, in the “add-Two-Nums-A-B” example, “add-Two-Nums-A” is evaluated first to return a new function that takes “Be” as an argument.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=460s"},{"start_seconds":472,"text":"Although it seems like you are passing both “A and B” at the same time, the way a","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=472s"},{"start_seconds":477,"text":"Daml function works is that it precisely only takes one argument at a time, returning a new function that takes the next argument.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=477s"},{"start_seconds":486,"text":"This behavior is repeated until it takes all of the arguments that it’s designed to take.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=486s"},{"start_seconds":492,"text":"Once all the arguments are passed in, the function finally evaluates to a return value.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=492s"},{"start_seconds":499,"text":"This behavior of repetitively taking in arguments and returning a new function until it’s ready to evaluate is called “currying” in programming.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=499s"},{"start_seconds":508,"text":"All Daml functions are curried, and currying is an important concept to internalize for you to understand more advanced concepts later on.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=508s"},{"start_seconds":518,"text":"In addition to the regular function you saw in the previous slide, there are lambda and infix functions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=518s"},{"start_seconds":526,"text":"A lambda function is written in inline notation to define an anonymous function for a single time usage.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=526s"},{"start_seconds":534,"text":"A lambda function is defined in parentheses with a backslash. After the backslash, you specify a parameter followed by a right arrow.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=534s"},{"start_seconds":543,"text":"After the right arrow, you write the function definition.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=543s"},{"start_seconds":547,"text":"For example, if you write an increment function as a lambda function, you first open up a parenthesis and add backslash and En, the parameter.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=547s"},{"start_seconds":557,"text":"After the right arrow, you write “N + 1”, which is what the function evaluates to.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=557s"},{"start_seconds":564,"text":"Lambda functions can help make code concise, especially if you know that the function won’t be used anywhere else.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=564s"},{"start_seconds":572,"text":"Functions can also be written using infix notation. One example is the math operator “+” sign.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=572s"},{"start_seconds":579,"text":"Common math operators such as plus, minus, multiply, and divide can be used as you normally would in any programming language.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=579s"},{"start_seconds":589,"text":"For named binary functions like “add-Two-Nums,” which takes two arguments, Daml supports writing functions using infix-notation by enclosing the function in back-ticks.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=589s"},{"start_seconds":601,"text":"In the “infix-Add” function, “add-Two-Nums” is wrapped in a pair of back-ticks and placed between the two parameters “X and Y”.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=601s"},{"start_seconds":611,"text":"Calling the add-Two-Nums function this way is the same as calling it using the prefix notation where you write the function name first, followed by all of its arguments.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=611s"},{"start_seconds":622,"text":"You can also do the opposite: the math operator “+” can be called in the same way any other named functions are, rather than placing it between two numbers.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=622s"},{"start_seconds":633,"text":"Be sure to wrap the + sign in puhrentheseez. Let’s review some of the most commonly used iterative functions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=633s"},{"start_seconds":642,"text":"Daml is a declarative programming language, which means that it is designed to describe what a program should do, rather than how to do it.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=642s"},{"start_seconds":651,"text":"As a result, it does not have traditional imperative constructs such as “for-loops” or “while-loops” that specify the details of how a program should perform its computations.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=651s"},{"start_seconds":664,"text":"Instead of using loops, in Daml, you can use recursion or higher-order functions that take other functions as arguments, such as maps and folds.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=664s"},{"start_seconds":674,"text":"A recursive function calls itself in its definition, and there must be a base case to exist out of the recursive call.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=674s"},{"start_seconds":683,"text":"One thing to remember is that, in Daml, recursion works only at the top-level, and recursive sub-functions are not supported.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=683s"},{"start_seconds":693,"text":"For example, if you define and call a recursive sub-function within a recursive function,","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=693s"},{"start_seconds":699,"text":"you will get an error that “this feature is not supported, recursion can only happen at the top level.” There are variations of mapping functions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=699s"},{"start_seconds":709,"text":"You have been introduced to map, which is specifically used for mapping a List, and “F-map”, which is the general version of map.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=709s"},{"start_seconds":718,"text":"map takes two arguments: a function and a List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=718s"},{"start_seconds":722,"text":"It maps a function over every element in a List, and it evaluates to a new List with the updated elements in it.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=722s"},{"start_seconds":730,"text":"For example, the modulo-Using-Map function takes two arguments: a List of Ints and an Int, and it returns a new List of Ints.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=730s"},{"start_seconds":740,"text":"As the map function iterates through the List of Ints, each element is modded by 3.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=740s"},{"start_seconds":745,"text":"As a result, it evaluates to a new List of two zero one two.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=745s"},{"start_seconds":751,"text":"Folds are similar to map in the way that they apply a given function to each element of a List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=751s"},{"start_seconds":758,"text":"However, unlike map, folds accumulate into a single result as they iterate through a List to evaluate to a single aggregated value instead of returning a new List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=758s"},{"start_seconds":768,"text":"There is “fold-L”, which iterates the List input from left to right, and there’s “fold-R”, which iterates from right to left.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=768s"},{"start_seconds":777,"text":"This lesson will use “fold-L”. “fold-L” takes three arguments: a binary function, which is a function that takes two arguments, an initial value as an accumulator, and a List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=777s"},{"start_seconds":790,"text":"As the List is iterated, the function is applied to the accumulator and the current element of the List.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=790s"},{"start_seconds":797,"text":"For example, the “sum-List” function takes a List of Ints.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=797s"},{"start_seconds":810,"text":"As the List of one two three four is iterated, each element of the List is accumulated to the initial value of 0.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=810s"},{"start_seconds":824,"text":"As a result, it evaluates to 10.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=824s"},{"start_seconds":829,"text":"Let’s review conditional expressions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=829s"},{"start_seconds":836,"text":"The first one is the if-then-else construct.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=836s"},{"start_seconds":840,"text":"In some other languages, an if statement works without an else, but in Daml, you must provide an else to cover both sides of the boolean condition.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=840s"},{"start_seconds":851,"text":"That’s because if-then-else is not a function or a statement in Daml; it’s an expression.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=851s"},{"start_seconds":857,"text":"You should treat it as a function call where you must provide all of the valid arguments to get an output.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=857s"},{"start_seconds":864,"text":"If you have several conditions to check, you can use Guards for readability instead of if-then-else.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=864s"},{"start_seconds":871,"text":"You can specify all the conditions using the pipe symbol, including the “otherwise” at the end as a catchall.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=871s"},{"start_seconds":879,"text":"To keep your code dry, you can open a where-block and bind the repeated formula to a variable.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=879s"},{"start_seconds":886,"text":"You can also use the case statement, which relies on pattern matching instead of boolean conditions.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=886s"},{"start_seconds":893,"text":"For example, the “is-Even” function takes an Int and returns a Text.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=893s"},{"start_seconds":898,"text":"It matches the remainder of input divided by 2 with one of the listed patterns.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=898s"},{"start_seconds":904,"text":"If the remainder is 0, it will evaluate to the text “even.” In all other cases, it will evaluate to “not even.”","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=904s"},{"start_seconds":912,"text":"Just like the if-then-else expression, both guards and case-of require a catchall at the end.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=912s"},{"start_seconds":919,"text":"There are also informative types to help further elaborate a conditional workflow.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=919s"},{"start_seconds":925,"text":"The Either data type contains a value that can potentially be either of two specified data types.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=925s"},{"start_seconds":932,"text":"The Either type is represented as “Either-A-B”, indicating that it can hold a value of either “type A” or “type B”.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=932s"},{"start_seconds":942,"text":"The Either type has two constructors, 'Left' and 'Right'.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=942s"},{"start_seconds":946,"text":"The 'Left' constructor encapsulates a value of “type A”, and the 'Right' constructor encapsulates a value of “type B”.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=946s"},{"start_seconds":956,"text":"For example, the “some-Function” takes an Int and returns either Text or Int.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=956s"},{"start_seconds":962,"text":"If you pass negative 2 as the “X-value”, which is less than 0, it will return the Text “Error” wrapped in the Left constructor.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=962s"},{"start_seconds":971,"text":"If you pass 2, it will increment the value by 2 and return 4 as an Int wrapped in the Right constructor.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=971s"},{"start_seconds":979,"text":"The Either data type can also be used in error handling.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=979s"},{"start_seconds":983,"text":"In that case, by convention, the Left constructor holds the error value and the Right constructor holds the correct value.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=983s"},{"start_seconds":992,"text":"On a similar note, the Optional data type is used when a variable may either hold a specific value or may not hold anything.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=992s"},{"start_seconds":1001,"text":"The Optional data type helps define safe functions that otherwise may return an error.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1001s"},{"start_seconds":1015,"text":"For instance, the “safe-Div” function takes two Ints and returns an Optional Int.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1015s"},{"start_seconds":1021,"text":"The initial pattern-match in the function definition uses an underscore to represent the dividend, which acts as a catch-all, and sets the divisor as 0.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1021s"},{"start_seconds":1031,"text":"If this condition is met, the function will return 'None', which is the closest Daml has to a null value.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1031s"},{"start_seconds":1039,"text":"Otherwise, as per the second pattern-match, the function will return 'Some' value of type Int.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1039s"},{"start_seconds":1046,"text":"If you pass 6 and 0 as arguments, it will evaluate to None, since the divisor is 0.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1046s"},{"start_seconds":1052,"text":"If you pass 6 and 2 as arguments, it will evaluate to Some 3.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1052s"},{"start_seconds":1058,"text":"Data types, functions, and conditionals are concepts that are shared across a majority of programming languages.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1058s"},{"start_seconds":1065,"text":"This lesson reviewed how those concepts are implemented differently in Daml to refresh your memory of the basics.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1065s"},{"start_seconds":1073,"text":"Unlike other languages, Daml has data types dedicated to represent various aspects of contracts and testing contracts, such as Contract ID, Commands, Party, Script, and Update.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1073s"},{"start_seconds":1086,"text":"All Daml functions are curried, and there are multiple ways to write conditionals, as well as special data types to further elaborate them.","url":"https://www.youtube.com/watch?v=tA-aEDJ2tJc&t=1086s"}],"youtube_url":"https://youtube.com/watch?v=tA-aEDJ2tJc","url":"https://ccpedia.xyz/videos/tA-aEDJ2tJc"}