From 06cb2385ecc8e76c4b68e50f4b372cda7d54fb5d Mon Sep 17 00:00:00 2001 From: Carlos Alonso Ferreira Date: Thu, 3 Oct 2024 22:50:24 +0200 Subject: [PATCH] Add [head, ..tail] patter to List patterns page --- .../lesson04_list_patterns/code.gleam | 1 + .../lesson04_list_patterns/en.html | 5 ++++- .../lesson07_list_recursion/en.html | 10 ++++------ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam b/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam index 1b71feb..a2c1fe3 100644 --- a/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam +++ b/src/content/chapter2_flow_control/lesson04_list_patterns/code.gleam @@ -10,6 +10,7 @@ pub fn main() { [] -> "Empty list" [1] -> "List of just 1" [4, ..] -> "List starting with 4" + [3, ..rest] -> "Starts with a 3, captures the rest" [_, _] -> "List of 2 elements" _ -> "Some other list" } diff --git a/src/content/chapter2_flow_control/lesson04_list_patterns/en.html b/src/content/chapter2_flow_control/lesson04_list_patterns/en.html index de55eef..4d68138 100644 --- a/src/content/chapter2_flow_control/lesson04_list_patterns/en.html +++ b/src/content/chapter2_flow_control/lesson04_list_patterns/en.html @@ -11,5 +11,8 @@ The spread pattern .. can be used to match the rest of the list. The pattern [1, ..] matches any list that starts with 1. The pattern [_, _, ..] matches any list that has - at least two elements. + at least two elements. By adding a variable name after the .., you + can capture the remaining elements of the list. The pattern [_, ..rest] + will match a list with at least one element and assign the rest of the elements + to the variable rest.

diff --git a/src/content/chapter2_flow_control/lesson07_list_recursion/en.html b/src/content/chapter2_flow_control/lesson07_list_recursion/en.html index 8a3a7d3..70fbbdc 100644 --- a/src/content/chapter2_flow_control/lesson07_list_recursion/en.html +++ b/src/content/chapter2_flow_control/lesson07_list_recursion/en.html @@ -2,16 +2,14 @@ While it is more common to use functions in the gleam/list - + module to iterate across a list, at times you may prefer to work with the list directly.

- The [first, ..rest] pattern matches on a list with at least one - element, assigning the first element to the variable first and - the rest of the list to the variable rest. By using this pattern - and a pattern for the empty list [] a function can run code on - each element of a list until the end is reached. + By using the [first, ..rest] pattern and a pattern for the empty + list [] a function can run code on each element of a list until + the end is reached.

This code sums a list by recursing over the list and adding each int to a