From e2415f6205037dc16aa51dbc24e513d22a1529b8 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon Date: Fri, 27 Sep 2024 14:21:03 -0500 Subject: [PATCH 1/8] Recursive types --- doc/md/writing-motoko/async-star.md | 2 +- .../writing-motoko/local-objects-classes.md | 27 +++++++++++- doc/md/writing-motoko/recursive-types.md | 43 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 doc/md/writing-motoko/recursive-types.md diff --git a/doc/md/writing-motoko/async-star.md b/doc/md/writing-motoko/async-star.md index dcd9f3af8f1..5de60c71bd6 100644 --- a/doc/md/writing-motoko/async-star.md +++ b/doc/md/writing-motoko/async-star.md @@ -1,5 +1,5 @@ --- -sidebar_position: 27 +sidebar_position: 28 --- # Abstracting asynchronous code diff --git a/doc/md/writing-motoko/local-objects-classes.md b/doc/md/writing-motoko/local-objects-classes.md index d3175e3835a..50a7318d890 100644 --- a/doc/md/writing-motoko/local-objects-classes.md +++ b/doc/md/writing-motoko/local-objects-classes.md @@ -162,7 +162,32 @@ The `class` keyword syntax shown above is a shorthand for these two definitions An object class defines a constructor function that may carry zero or more data arguments and zero or more type arguments. -The `Counter` example above has zero of each. +The `Counter` example above has zero of each. The example below takes two data arguments, `arg1` and `arg2`, with `Type1` and `Type2` as the types of these arguments, respectively. + +``` motoko no-repl +class MyClass(arg1: Type1, arg2: Type2) { + // class body here +}; +``` + +For example, you can write a `Counter` class that takes an argument of type `Nat` and an argument of type `Bool`: + +``` motoko no-repl +import Nat "mo:base/Nat"; + +actor { + class Counter(init : Nat, flag : Bool) { + var c = init; + var f = flag; + public func inc() : Nat { + if (f) { + c += 1; + }; + return c; + }; + }; +} +``` The type arguments, if any, parameterize both the type and the constructor function for the class. diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md new file mode 100644 index 00000000000..bac0c0719d4 --- /dev/null +++ b/doc/md/writing-motoko/recursive-types.md @@ -0,0 +1,43 @@ +--- +sidebar_position: 29 +--- + +# Recursive types + +## Overview + +A recursive type are types that contain the values of the same type. Recursive types enable you to create complex recursive data structures, such as linked lists or trees. + +Motoko supports linked lists, one data structure that implements recursive types. + +## Recursive lists + +``` motoko no-repl +type List = ?(T, List); +``` + +In this example, the generic type `List` is defined with one type parameter. `List` is a tuple with two components: the first component is the type parameter `T` , and the second component is `List`. The second component is the recursive type, as the generic type `List` contains a value of itself within its own definition. + +`List` is a repeating pattern, where each repeated component is a tuple that contains a value of type `T` and a reference to the tail of `List`. + +## Recursive functions + +A recursive function can be used to retrieve the last element of a given list: + +```motoko no-repl +func last(l : List) : ?T { + switch l { + case null { null }; + case (?(x, null)) { ?x }; + case (?(_, t)) { last(t) }; + }; +}; +``` + +This generic function `last` takes one argument `l` of type `List`, which refers to the head of a list. If this function returns the last element of a list, it returns an optional value `?T`. If there isn't a last element, it will return `null`. The body of the function uses a `switch` statement to determine if the list passed as an argument is a null list, the last element of a list, or if it is the tail of a list with a next value. + +In this switch statement, the `last` function is used recursively, since it is called within itself with `t` as the argument. The function is called again each time the case statement is satisfied and the function receives a list head that it can switch on until the last element is returned. + +## Resources + +- [Recursive types](https://github.com/Web3NL/motoko-book/blob/main/src/advanced-types/recursive-types.md). \ No newline at end of file From 951825a004ab278e3d0a5595a2d495cafe1f3558 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Fri, 27 Sep 2024 14:24:17 -0500 Subject: [PATCH 2/8] Update recursive-types.md --- doc/md/writing-motoko/recursive-types.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index bac0c0719d4..e291a144f41 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -10,7 +10,7 @@ A recursive type are types that contain the values of the same type. Recursive t Motoko supports linked lists, one data structure that implements recursive types. -## Recursive lists +## Recursive types ``` motoko no-repl type List = ?(T, List); @@ -40,4 +40,4 @@ In this switch statement, the `last` function is used recursively, since it i ## Resources -- [Recursive types](https://github.com/Web3NL/motoko-book/blob/main/src/advanced-types/recursive-types.md). \ No newline at end of file +- [Recursive types](https://github.com/Web3NL/motoko-book/blob/main/src/advanced-types/recursive-types.md). From d2070faf5eb0c5edc0663bb4032442d3875c4173 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon Date: Fri, 27 Sep 2024 14:26:13 -0500 Subject: [PATCH 3/8] Recursive types --- doc/md/writing-motoko/recursive-types.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index bac0c0719d4..f87df2d44dd 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -6,7 +6,7 @@ sidebar_position: 29 ## Overview -A recursive type are types that contain the values of the same type. Recursive types enable you to create complex recursive data structures, such as linked lists or trees. +A recursive type is a type that contains the values of the same type. Recursive types enable you to create complex recursive data structures, such as linked lists or trees. Motoko supports linked lists, one data structure that implements recursive types. @@ -26,17 +26,17 @@ A recursive function can be used to retrieve the last element of a given list: ```motoko no-repl func last(l : List) : ?T { - switch l { - case null { null }; - case (?(x, null)) { ?x }; - case (?(_, t)) { last(t) }; - }; +switch l { +case null { null }; +case (?(x, null)) { ?x }; +case (?(_, t)) { last(t) }; +}; }; ``` This generic function `last` takes one argument `l` of type `List`, which refers to the head of a list. If this function returns the last element of a list, it returns an optional value `?T`. If there isn't a last element, it will return `null`. The body of the function uses a `switch` statement to determine if the list passed as an argument is a null list, the last element of a list, or if it is the tail of a list with a next value. -In this switch statement, the `last` function is used recursively, since it is called within itself with `t` as the argument. The function is called again each time the case statement is satisfied and the function receives a list head that it can switch on until the last element is returned. +In this switch statement, the `last` function is used recursively, since it is called within itself with `t` as the argument. The function is called again each time the case statement is satisfied, and the function receives a list head that it can switch on until the last element is returned. ## Resources From 82c880fbdd54beed7ba50bf0fa53e517e896c3a1 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:46:10 -0500 Subject: [PATCH 4/8] Update recursive-types.md --- doc/md/writing-motoko/recursive-types.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index 6e5d0bc6255..1d4f2942afa 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -26,11 +26,11 @@ A recursive function can be used to retrieve the last element of a given list: ```motoko no-repl func last(l : List) : ?T { -switch l { -case null { null }; -case (?(x, null)) { ?x }; -case (?(_, t)) { last(t) }; -}; + switch l { + case null { null }; + case (?(x, null)) { ?x }; + case (?(_, t)) { last(t) }; + }; }; ``` From 8a8c40e87d925175b98e6aee78c08b8535e82820 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Fri, 27 Sep 2024 16:48:58 -0500 Subject: [PATCH 5/8] Update recursive-types.md --- doc/md/writing-motoko/recursive-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index 1d4f2942afa..649486ae1f6 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -10,7 +10,7 @@ A recursive type is a type that contains the values of the same type. Recursive Motoko supports linked lists, one data structure that implements recursive types. -## Recursive types +## Recursive lists ``` motoko no-repl type List = ?(T, List); From 9cf8ae2eec13dc828045a98b7b7bc19de0448715 Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:25:41 -0500 Subject: [PATCH 6/8] Update doc/md/writing-motoko/recursive-types.md Co-authored-by: Gabor Greif --- doc/md/writing-motoko/recursive-types.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index 649486ae1f6..c0183351431 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -8,7 +8,7 @@ sidebar_position: 29 A recursive type is a type that contains the values of the same type. Recursive types enable you to create complex recursive data structures, such as linked lists or trees. -Motoko supports linked lists, one data structure that implements recursive types. +Motoko supports linked lists, a data structure that is an example of a recursive type. ## Recursive lists From 4b199b235f826c051dd07be8e1df62fb6de7f88b Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:31:46 -0500 Subject: [PATCH 7/8] Update local-objects-classes.md --- doc/md/writing-motoko/local-objects-classes.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/md/writing-motoko/local-objects-classes.md b/doc/md/writing-motoko/local-objects-classes.md index 50a7318d890..53e16bd7d81 100644 --- a/doc/md/writing-motoko/local-objects-classes.md +++ b/doc/md/writing-motoko/local-objects-classes.md @@ -180,7 +180,7 @@ actor { var c = init; var f = flag; public func inc() : Nat { - if (f) { + if f { c += 1; }; return c; @@ -349,4 +349,4 @@ extend existing objects with new functionality. For more details, see the [language manual](../reference/language-manual#object-combinationextension). -Logo \ No newline at end of file +Logo From 3b1b863dd258167efe9d52bf1894a8715ccccd1d Mon Sep 17 00:00:00 2001 From: Jessie Mongeon <133128541+jessiemongeon1@users.noreply.github.com> Date: Mon, 30 Sep 2024 09:33:22 -0500 Subject: [PATCH 8/8] Update recursive-types.md --- doc/md/writing-motoko/recursive-types.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/md/writing-motoko/recursive-types.md b/doc/md/writing-motoko/recursive-types.md index c0183351431..01906425196 100644 --- a/doc/md/writing-motoko/recursive-types.md +++ b/doc/md/writing-motoko/recursive-types.md @@ -34,10 +34,14 @@ func last(l : List) : ?T { }; ``` -This generic function `last` takes one argument `l` of type `List`, which refers to the head of a list. If this function returns the last element of a list, it returns an optional value `?T`. If there isn't a last element, it will return `null`. The body of the function uses a `switch` statement to determine if the list passed as an argument is a null list, the last element of a list, or if it is the tail of a list with a next value. +This generic function `last` takes one argument `l` of type `List`, which refers to the head of a list. If this function returns the last element of a list, it returns an optional value `?T`. If there isn't a last element, it will return `null`. The body of the function uses a `switch` statement to determine if the list passed as an argument is an empty list, the last element of a list, or if it is the tail of a list with a next value. In this switch statement, the `last` function is used recursively, since it is called within itself with `t` as the argument. The function is called again each time the case statement is satisfied, and the function receives a list head that it can switch on until the last element is returned. +:::info +Note that you will need to use recursive functions to access all data in a recursive type. +::: + ## Resources - [Recursive types](https://github.com/Web3NL/motoko-book/blob/main/src/advanced-types/recursive-types.md).