diff options
author | Louis Pilfold <louis@lpil.uk> | 2023-12-06 21:13:12 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2023-12-06 21:13:12 +0000 |
commit | 658de7ac8276cac1c4d8f89d5783b1faaef42871 (patch) | |
tree | 2e41a8d48c30b11d5869579901a645ed486f2476 /lessons/src | |
parent | 58efb89355676356ee1006bbd2c8b0ee44108358 (diff) | |
download | tour-658de7ac8276cac1c4d8f89d5783b1faaef42871.tar.gz tour-658de7ac8276cac1c4d8f89d5783b1faaef42871.zip |
More content!
Diffstat (limited to 'lessons/src')
-rw-r--r-- | lessons/src/lesson007_strings/code.gleam | 3 | ||||
-rw-r--r-- | lessons/src/lesson007_strings/text.html | 7 | ||||
-rw-r--r-- | lessons/src/lesson008_bools/code.gleam | 14 | ||||
-rw-r--r-- | lessons/src/lesson008_bools/text.html | 17 | ||||
-rw-r--r-- | lessons/src/lesson009_assignments/code.gleam | 17 | ||||
-rw-r--r-- | lessons/src/lesson009_assignments/text.html | 8 | ||||
-rw-r--r-- | lessons/src/lesson010_discard_patterns/code.gleam | 4 | ||||
-rw-r--r-- | lessons/src/lesson010_discard_patterns/text.html | 10 | ||||
-rw-r--r-- | lessons/src/lesson011_type_annotations/code.gleam | 12 | ||||
-rw-r--r-- | lessons/src/lesson011_type_annotations/text.html | 11 | ||||
-rw-r--r-- | lessons/src/lesson012_blocks/code.gleam | 13 | ||||
-rw-r--r-- | lessons/src/lesson012_blocks/text.html | 23 |
12 files changed, 136 insertions, 3 deletions
diff --git a/lessons/src/lesson007_strings/code.gleam b/lessons/src/lesson007_strings/code.gleam index c57cf9b..c77163e 100644 --- a/lessons/src/lesson007_strings/code.gleam +++ b/lessons/src/lesson007_strings/code.gleam @@ -11,6 +11,9 @@ pub fn main() { ) io.debug("\u{1F600}") + // String concatenation + io.debug("One " <> "Two") + // String functions io.debug(string.reverse("1 2 3 4 5")) io.debug(string.append("abc", "def")) diff --git a/lessons/src/lesson007_strings/text.html b/lessons/src/lesson007_strings/text.html index 21cfc12..820f1b3 100644 --- a/lessons/src/lesson007_strings/text.html +++ b/lessons/src/lesson007_strings/text.html @@ -1,8 +1,9 @@ <p> - In Gleam Strings are written as text surrounded by double quotes. + In Gleam Strings are written as text surrounded by double quotes, and + can span multiple lines and contain unicode characters. </p> <p> - They can span multiple lines and support unicode characters. + The <code><></code> operator can be used to concatenate strings. </p> <p> Several escape sequences are supported: @@ -14,7 +15,7 @@ <li><code>\n</code> - newline</li> <li><code>\r</code> - carriage return</li> <li><code>\t</code> - tab</li> - <li><code>\u{1F600}</code> - unicode codepoint</li> + <li><code>\u{xxxxxx}</code> - unicode codepoint</li> </ul> <p> The <a href="https://hexdocs.pm/gleam_stdlib/gleam/string.html"><code>gleam/string</code></a> diff --git a/lessons/src/lesson008_bools/code.gleam b/lessons/src/lesson008_bools/code.gleam new file mode 100644 index 0000000..e5c1d98 --- /dev/null +++ b/lessons/src/lesson008_bools/code.gleam @@ -0,0 +1,14 @@ +import gleam/io +import gleam/bool + +pub fn main() { + // Bool operators + io.debug(True && False) + io.debug(True && True) + io.debug(False || False) + io.debug(False || True) + + // Bool functions + io.debug(bool.to_string(True)) + io.debug(bool.to_int(False)) +} diff --git a/lessons/src/lesson008_bools/text.html b/lessons/src/lesson008_bools/text.html new file mode 100644 index 0000000..3f60743 --- /dev/null +++ b/lessons/src/lesson008_bools/text.html @@ -0,0 +1,17 @@ +<p> + A <code>Bool</code> is a either <code>True</code> or <code>False</code>. +</p> +<p> + The <code>||</code>, <code>&&</code>, and <code>!</code> operators can be used + to manipulate bools. +</p> +<p> + The <code>||</code> and <code>&&</code> operators are short-circuiting, + meaning that if the left hand side of the operator is <code>True</code> for + <code>||</code> or <code>False</code> for <code>&&</code> then the right hand + side of the operator will not be evaluated. +</p> +<p> + The <a href="https://hexdocs.pm/gleam_stdlib/gleam/bool.html"><code>gleam/bool</code></a> + standard library module contains functions for working with bools. +</p> diff --git a/lessons/src/lesson009_assignments/code.gleam b/lessons/src/lesson009_assignments/code.gleam new file mode 100644 index 0000000..a030e43 --- /dev/null +++ b/lessons/src/lesson009_assignments/code.gleam @@ -0,0 +1,17 @@ +import gleam/io + +pub fn main() { + let x = "Original" + io.debug(x) + + // Assign `y` to the value of `x` + let y = x + io.debug(y) + + // Assign `x` to a new value + let x = "New" + io.debug(x) + + // The `y` still refers to the original value + io.debug(y) +} diff --git a/lessons/src/lesson009_assignments/text.html b/lessons/src/lesson009_assignments/text.html new file mode 100644 index 0000000..6d535de --- /dev/null +++ b/lessons/src/lesson009_assignments/text.html @@ -0,0 +1,8 @@ +<p> + A value can be assigned to a variable using <code>let</code>. +</p> +<p> + Variable names can be reused by later let bindings, but the values they + reference are immutable, so the values themselves are not changed or mutated + in any way. +</p> diff --git a/lessons/src/lesson010_discard_patterns/code.gleam b/lessons/src/lesson010_discard_patterns/code.gleam new file mode 100644 index 0000000..fa2c0e3 --- /dev/null +++ b/lessons/src/lesson010_discard_patterns/code.gleam @@ -0,0 +1,4 @@ +pub fn main() { + // This variable is never used + let _score = 1000 +} diff --git a/lessons/src/lesson010_discard_patterns/text.html b/lessons/src/lesson010_discard_patterns/text.html new file mode 100644 index 0000000..46dc79b --- /dev/null +++ b/lessons/src/lesson010_discard_patterns/text.html @@ -0,0 +1,10 @@ +<p> + If a variable is assigned but not used then Gleam will emit a warning. +</p> +<p> + If a variable is intended not to be use then the name can be prefixed with an + underscore, silencing the warning. +</p> +<p> + Try changing the variable name to <code>score</code> to see the warning. +</p> diff --git a/lessons/src/lesson011_type_annotations/code.gleam b/lessons/src/lesson011_type_annotations/code.gleam new file mode 100644 index 0000000..dfba90e --- /dev/null +++ b/lessons/src/lesson011_type_annotations/code.gleam @@ -0,0 +1,12 @@ +import gleam/io + +pub fn main() { + let name: String = "Gleam" + io.debug(name) + + let is_cool: Bool = True + io.debug(is_cool) + + let version: Int = 1 + io.debug(version) +} diff --git a/lessons/src/lesson011_type_annotations/text.html b/lessons/src/lesson011_type_annotations/text.html new file mode 100644 index 0000000..583d7ce --- /dev/null +++ b/lessons/src/lesson011_type_annotations/text.html @@ -0,0 +1,11 @@ +<p> + Let assignments can be written with a type annotation after the name. +</p> +<p> + Type annotations may be useful for documentation purposes, but they do not + change how Gleam type checks the code beyond ensuring that the annotation is + correct. +</p> +<p> + Typically Gleam code will not have type annotations for assignments. +</p> diff --git a/lessons/src/lesson012_blocks/code.gleam b/lessons/src/lesson012_blocks/code.gleam new file mode 100644 index 0000000..31e4729 --- /dev/null +++ b/lessons/src/lesson012_blocks/code.gleam @@ -0,0 +1,13 @@ +import gleam/io + +pub fn main() { + let fahrenheit = { + let degrees = 64 + degrees + } + // io.debug(degrees) // <- This will not compile + + // Changing order of evaluation + let celsius = { fahrenheit - 32 } * 5 / 9 + io.debug(celsius) +} diff --git a/lessons/src/lesson012_blocks/text.html b/lessons/src/lesson012_blocks/text.html new file mode 100644 index 0000000..f943e11 --- /dev/null +++ b/lessons/src/lesson012_blocks/text.html @@ -0,0 +1,23 @@ +<p> + Blocks are one or more expressions grouped together with curly braces. Each + expression is evaluated in order and the value of the last expression is + returned. +</p> +<p> + Any variables assigned within the block can only be used within the block. +</p> +<p> + Try uncommenting <code>io.debug(degrees)</code> to see the compile error from + trying to use a variable that is not in scope. +</p> +<p> + Blocks can also be used to change the order of evaluation of binary operators + expressions. +</p> +<p> + <code>*</code> binds more tightly than <code>+</code>so the expression + <code>1 + 2 * 3</code> evaluates to 7. If the <code>1 + 2</code> should be + evaluated first to make the expression evaluate to 9 then the expression can be + wrapped in a block: <code>{ 1 + 2 } * 3</code>. This is similar to grouping + with parentheses in some other languages. +</p> |