Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Recursive types #4715

Merged
merged 10 commits into from
Oct 7, 2024
2 changes: 1 addition & 1 deletion doc/md/writing-motoko/async-star.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 27
sidebar_position: 28
---

# Abstracting asynchronous code
Expand Down
27 changes: 26 additions & 1 deletion doc/md/writing-motoko/local-objects-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
jessiemongeon1 marked this conversation as resolved.
Show resolved Hide resolved
c += 1;
};
return c;
};
};
}
```

The type arguments, if any, parameterize both the type and the constructor function for the class.

Expand Down
43 changes: 43 additions & 0 deletions doc/md/writing-motoko/recursive-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
sidebar_position: 29
---

# Recursive types

## Overview

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.
jessiemongeon1 marked this conversation as resolved.
Show resolved Hide resolved

Motoko supports linked lists, one data structure that implements recursive types.
jessiemongeon1 marked this conversation as resolved.
Show resolved Hide resolved

## Recursive lists

``` motoko no-repl
type List<T> = ?(T, List<T>);
```

In this example, the generic type `List<T>` is defined with one type parameter. `List<T>` is a tuple with two components: the first component is the type parameter `T` , and the second component is `List<T>`. The second component is the recursive type, as the generic type `List<T>` 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<T>`.

## Recursive functions

A recursive function can be used to retrieve the last element of a given list:

```motoko no-repl
func last<T>(l : List<T>) : ?T {
switch l {
case null { null };
case (?(x, null)) { ?x };
case (?(_, t)) { last<T>(t) };
};
};
```

This generic function `last<T>` takes one argument `l` of type `List<T>`, 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.
jessiemongeon1 marked this conversation as resolved.
Show resolved Hide resolved

In this switch statement, the `last<T>` 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.

jessiemongeon1 marked this conversation as resolved.
Show resolved Hide resolved
## Resources

- [Recursive types](https://github.com/Web3NL/motoko-book/blob/main/src/advanced-types/recursive-types.md).