diff options
Diffstat (limited to 'src/content/chapter0_basics')
30 files changed, 466 insertions, 0 deletions
diff --git a/src/content/chapter0_basics/lesson00_hello_world/code.gleam b/src/content/chapter0_basics/lesson00_hello_world/code.gleam new file mode 100644 index 0000000..30530b2 --- /dev/null +++ b/src/content/chapter0_basics/lesson00_hello_world/code.gleam @@ -0,0 +1,5 @@ +import gleam/io + +pub fn main() { + io.println("Hello, Joe!") +} diff --git a/src/content/chapter0_basics/lesson00_hello_world/text.html b/src/content/chapter0_basics/lesson00_hello_world/text.html new file mode 100644 index 0000000..cb41be9 --- /dev/null +++ b/src/content/chapter0_basics/lesson00_hello_world/text.html @@ -0,0 +1,26 @@ +<h2>Hello, friend ๐ซ</h2> +<p> + Welcome to Try Gleam! An interactive tour of the Gleam programming language. +</p> +<p> + It covers all aspects of the Gleam language, and assuming you have some + prior programming experience should teach you everything you need to write + real programs in Gleam. +</p> +<p> + The tour is interactive! The code shown is editable and will be compiled and + evaluated as you type. Anything you print using <code>io.println</code> or + <code>io.debug</code> will be shown in the bottom section, along with any + compile errors and warnings. To evaluate Gleam code the tour compiles Gleam to + JavaScript and runs it, all entirely within your browser window. +</p> +<p> + If at any point you get stuck or have a question do not hesitate to ask in + <a href="https://discord.gg/Fm8Pwmy">the Gleam Discord server</a>. We're here + to help, and if you find something confusing then it's likely others will too, + and we want to know about it so we can improve the tour. +</p> +<p> + OK, let's go. Click "Next" to get started, or click "Index" to jump to a + specific topic. +</p> diff --git a/src/content/chapter0_basics/lesson01_basics/code.gleam b/src/content/chapter0_basics/lesson01_basics/code.gleam new file mode 100644 index 0000000..67cc6b4 --- /dev/null +++ b/src/content/chapter0_basics/lesson01_basics/code.gleam @@ -0,0 +1,7 @@ +// Import a Gleam module from the standard library +import gleam/io + +pub fn main() { + // Print to the console + io.println("Hello, Joe!") +} diff --git a/src/content/chapter0_basics/lesson01_basics/text.html b/src/content/chapter0_basics/lesson01_basics/text.html new file mode 100644 index 0000000..8e2033a --- /dev/null +++ b/src/content/chapter0_basics/lesson01_basics/text.html @@ -0,0 +1,17 @@ +<p> + Here is a program that prints out the text "Hello, Joe!". +</p> +<p> + It does this by using the `println` function which has been imported from the + <a href="https://hexdocs.pm/gleam_stdlib/gleam/io.html"><code>gleam/io</code></a> + module, which is part of the Gleam standard library. +</p> +<p> + In a normal Gleam program this program would be run use the command + <code>gleam run</code> on the command line, but here in this tutorial the + program is automatically compiled and run as the code is edited. +</p> +<p> + Try changing the text being printed to <code>Hello, Mike!</code> and see what + happens. +</p> diff --git a/src/content/chapter0_basics/lesson02_unqualified_imports/code.gleam b/src/content/chapter0_basics/lesson02_unqualified_imports/code.gleam new file mode 100644 index 0000000..2708f25 --- /dev/null +++ b/src/content/chapter0_basics/lesson02_unqualified_imports/code.gleam @@ -0,0 +1,10 @@ +// Import the module and one of its functions +import gleam/io.{println} + +pub fn main() { + // Use the function in a qualified fashion + io.println("This is qualified") + + // Or an unqualified fashion + println("This is unqualified") +} diff --git a/src/content/chapter0_basics/lesson02_unqualified_imports/text.html b/src/content/chapter0_basics/lesson02_unqualified_imports/text.html new file mode 100644 index 0000000..8fda45e --- /dev/null +++ b/src/content/chapter0_basics/lesson02_unqualified_imports/text.html @@ -0,0 +1,15 @@ +<p> + Normally functions from other modules are used in a qualified fashion, with + the module qualifier before function name. For example, + <code>io.println("Hello!")</code>. +</p> +<p> + It is also possible to specify a list of functions to import from a module in + an unqualified fashion, such as the <code>println</code> function in the code + editor. Because it has been imported like this it can be referred to as just + <code>println</code>. +</p> +<p> + Generally it is best to use qualified imports, as this makes it clear where + the function is defined, making the code easier to read. +</p> diff --git a/src/content/chapter0_basics/lesson03_type_checking/code.gleam b/src/content/chapter0_basics/lesson03_type_checking/code.gleam new file mode 100644 index 0000000..e068f31 --- /dev/null +++ b/src/content/chapter0_basics/lesson03_type_checking/code.gleam @@ -0,0 +1,7 @@ +import gleam/io + +pub fn main() { + io.println("My lucky number is:") + // io.println(4) + // ๐๏ธ Uncomment this line +} diff --git a/src/content/chapter0_basics/lesson03_type_checking/text.html b/src/content/chapter0_basics/lesson03_type_checking/text.html new file mode 100644 index 0000000..fadfe64 --- /dev/null +++ b/src/content/chapter0_basics/lesson03_type_checking/text.html @@ -0,0 +1,19 @@ +<p> + Gleam has a robust static type system that is help you as you write and edit + code, catching mistakes and showing you where to make changes. +</p> +<p> + Uncomment the line <code>io.println(4)</code> and see how a compile time error + is reported as the <code>io.println</code> function only works with strings, + not ints. +</p> +<p> + To fix the code change the code to call the <code>io.debug</code> + function instead, as it will print a value of any type. +</p> +<p> + Gleam has no <code>null</code>, no implicit conversions, no exceptions, and + always performs full type checking. If the code compiles you can be reasonably + confident it does not have any inconsistencies that may cause bugs or + crashes. +</p> diff --git a/src/content/chapter0_basics/lesson04_ints/code.gleam b/src/content/chapter0_basics/lesson04_ints/code.gleam new file mode 100644 index 0000000..cb7991b --- /dev/null +++ b/src/content/chapter0_basics/lesson04_ints/code.gleam @@ -0,0 +1,25 @@ +import gleam/io +import gleam/int + +pub fn main() { + // Int arithmetic + io.debug(1 + 1) + io.debug(5 - 1) + io.debug(5 / 2) + io.debug(3 * 3) + io.debug(5 % 2) + + // Int comparisons + io.debug(2 > 1) + io.debug(2 < 1) + io.debug(2 >= 1) + io.debug(2 <= 1) + + // Equality works for any type + io.debug(1 == 1) + io.debug(2 == 1) + + // Standard library int functions + io.debug(int.max(42, 77)) + io.debug(int.clamp(5, 10, 20)) +} diff --git a/src/content/chapter0_basics/lesson04_ints/text.html b/src/content/chapter0_basics/lesson04_ints/text.html new file mode 100644 index 0000000..252496a --- /dev/null +++ b/src/content/chapter0_basics/lesson04_ints/text.html @@ -0,0 +1,17 @@ +<p>Gleam's <code>Int</code> type represents whole numbers.</p> +<p> + There are arithmetic and comparison operators for ints, as well as the + equality operator which works on all types. +</p> +<p> + When running on the Erlang virtual machine ints have no maximum and minimum + size. When running on JavaScript runtimes ints are represented using + JavaScript's 64 bit floating point numbers, +</p> +<p> + The + <a href="https://hexdocs.pm/gleam_stdlib/gleam/int.html" + ><code>gleam/int</code></a + > + standard library module contains functions for working with ints. +</p> diff --git a/src/content/chapter0_basics/lesson05_floats/code.gleam b/src/content/chapter0_basics/lesson05_floats/code.gleam new file mode 100644 index 0000000..8c4e89a --- /dev/null +++ b/src/content/chapter0_basics/lesson05_floats/code.gleam @@ -0,0 +1,24 @@ +import gleam/io +import gleam/float + +pub fn main() { + // Float arithmetic + io.debug(1.0 +. 1.5) + io.debug(5.0 -. 1.5) + io.debug(5.0 /. 2.5) + io.debug(3.0 *. 3.5) + + // Float comparisons + io.debug(2.2 >. 1.3) + io.debug(2.2 <. 1.3) + io.debug(2.2 >=. 1.3) + io.debug(2.2 <=. 1.3) + + // Equality works for any type + io.debug(1.1 == 1.1) + io.debug(2.1 == 1.2) + + // Standard library float functions + io.debug(float.max(2.0, 9.5)) + io.debug(float.ceiling(5.4)) +} diff --git a/src/content/chapter0_basics/lesson05_floats/text.html b/src/content/chapter0_basics/lesson05_floats/text.html new file mode 100644 index 0000000..497bb13 --- /dev/null +++ b/src/content/chapter0_basics/lesson05_floats/text.html @@ -0,0 +1,19 @@ +<p> + Gleam's <code>Float</code> type represents numbers that are not integers. +</p> +<p> + Unlike many languages Gleam does not have a `NaN` or `Infinity` float value. +</p> +<p> + Gleam's numerical operators are not overloaded, so there are dedictated + operators for working with floats. +</p> +<p> + Floats are represented as 64 bit floating point numbers on both Erlang and + JavaScript runtimes. +</p> +<p> + The <a href="https://hexdocs.pm/gleam_stdlib/gleam/float.html"><code>gleam/float</code></a> + standard library module contains functions for working with floats. +</p> + diff --git a/src/content/chapter0_basics/lesson06_number_formats/code.gleam b/src/content/chapter0_basics/lesson06_number_formats/code.gleam new file mode 100644 index 0000000..7307185 --- /dev/null +++ b/src/content/chapter0_basics/lesson06_number_formats/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + // Underscores + io.debug(1_000_000) + io.debug(10_000.01) + + // Binary, octal, and hex Int literals + io.debug(0b00001111) + io.debug(0o17) + io.debug(0xF) + + // Scientific notation Float literals + io.debug(7.0e7) + io.debug(3.0e-4) +} diff --git a/src/content/chapter0_basics/lesson06_number_formats/text.html b/src/content/chapter0_basics/lesson06_number_formats/text.html new file mode 100644 index 0000000..308219a --- /dev/null +++ b/src/content/chapter0_basics/lesson06_number_formats/text.html @@ -0,0 +1,13 @@ +<p> + Underscores can be added to numbers for clarity. For example, + <code>1000000</code> can be tricky to read quickly, while + <code>1_000_000</code> can be easier. +</p> +<p> + Ints can be written in binary, octal, or hexadecimal formats using the + <code>0b</code>, <code>0o</code>, and <code>0x</code> prefixes respectively. +</p> +<p> + Floats can be written in a scientific notation. +</p> + diff --git a/src/content/chapter0_basics/lesson07_strings/code.gleam b/src/content/chapter0_basics/lesson07_strings/code.gleam new file mode 100644 index 0000000..c77163e --- /dev/null +++ b/src/content/chapter0_basics/lesson07_strings/code.gleam @@ -0,0 +1,20 @@ +import gleam/io +import gleam/string + +pub fn main() { + // String literals + io.debug("๐ฉโ๐ป ใใใซใกใฏ Gleam ๐ณ๏ธโ๐") + io.debug( + "multi + line + string", + ) + 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/src/content/chapter0_basics/lesson07_strings/text.html b/src/content/chapter0_basics/lesson07_strings/text.html new file mode 100644 index 0000000..820f1b3 --- /dev/null +++ b/src/content/chapter0_basics/lesson07_strings/text.html @@ -0,0 +1,23 @@ +<p> + In Gleam Strings are written as text surrounded by double quotes, and + can span multiple lines and contain unicode characters. +</p> +<p> + The <code><></code> operator can be used to concatenate strings. +</p> +<p> + Several escape sequences are supported: +</p> +<ul> + <li><code>\"</code> - double quote</li> + <li><code>\\</code> - backslash</li> + <li><code>\f</code> - form feed</li> + <li><code>\n</code> - newline</li> + <li><code>\r</code> - carriage return</li> + <li><code>\t</code> - tab</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> + standard library module contains functions for working with strings. +</p> diff --git a/src/content/chapter0_basics/lesson08_bools/code.gleam b/src/content/chapter0_basics/lesson08_bools/code.gleam new file mode 100644 index 0000000..e5c1d98 --- /dev/null +++ b/src/content/chapter0_basics/lesson08_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/src/content/chapter0_basics/lesson08_bools/text.html b/src/content/chapter0_basics/lesson08_bools/text.html new file mode 100644 index 0000000..3f60743 --- /dev/null +++ b/src/content/chapter0_basics/lesson08_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/src/content/chapter0_basics/lesson09_assignments/code.gleam b/src/content/chapter0_basics/lesson09_assignments/code.gleam new file mode 100644 index 0000000..a030e43 --- /dev/null +++ b/src/content/chapter0_basics/lesson09_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/src/content/chapter0_basics/lesson09_assignments/text.html b/src/content/chapter0_basics/lesson09_assignments/text.html new file mode 100644 index 0000000..6d535de --- /dev/null +++ b/src/content/chapter0_basics/lesson09_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/src/content/chapter0_basics/lesson10_discard_patterns/code.gleam b/src/content/chapter0_basics/lesson10_discard_patterns/code.gleam new file mode 100644 index 0000000..fa2c0e3 --- /dev/null +++ b/src/content/chapter0_basics/lesson10_discard_patterns/code.gleam @@ -0,0 +1,4 @@ +pub fn main() { + // This variable is never used + let _score = 1000 +} diff --git a/src/content/chapter0_basics/lesson10_discard_patterns/text.html b/src/content/chapter0_basics/lesson10_discard_patterns/text.html new file mode 100644 index 0000000..46dc79b --- /dev/null +++ b/src/content/chapter0_basics/lesson10_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/src/content/chapter0_basics/lesson11_type_annotations/code.gleam b/src/content/chapter0_basics/lesson11_type_annotations/code.gleam new file mode 100644 index 0000000..1299c2f --- /dev/null +++ b/src/content/chapter0_basics/lesson11_type_annotations/code.gleam @@ -0,0 +1,7 @@ +pub fn main() { + let _name: String = "Gleam" + + let _is_cool: Bool = True + + let _version: Int = 1 +} diff --git a/src/content/chapter0_basics/lesson11_type_annotations/text.html b/src/content/chapter0_basics/lesson11_type_annotations/text.html new file mode 100644 index 0000000..8738a15 --- /dev/null +++ b/src/content/chapter0_basics/lesson11_type_annotations/text.html @@ -0,0 +1,15 @@ +<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> +<p> + Try changing a type annotation to something incorrect to see the compile + error. +</p> diff --git a/src/content/chapter0_basics/lesson12_blocks/code.gleam b/src/content/chapter0_basics/lesson12_blocks/code.gleam new file mode 100644 index 0000000..31e4729 --- /dev/null +++ b/src/content/chapter0_basics/lesson12_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/src/content/chapter0_basics/lesson12_blocks/text.html b/src/content/chapter0_basics/lesson12_blocks/text.html new file mode 100644 index 0000000..bc82e39 --- /dev/null +++ b/src/content/chapter0_basics/lesson12_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> diff --git a/src/content/chapter0_basics/lesson13_lists/code.gleam b/src/content/chapter0_basics/lesson13_lists/code.gleam new file mode 100644 index 0000000..646ad6e --- /dev/null +++ b/src/content/chapter0_basics/lesson13_lists/code.gleam @@ -0,0 +1,16 @@ +import gleam/io + +pub fn main() { + let ints = [1, 2, 3] + + io.debug(ints) + + // Immutably prepend + io.debug([-1, 0, ..ints]) + + // Uncomment this to see the error + // io.debug(["zero", ..ints]) + + // The original lists are unchanged + io.debug(ints) +} diff --git a/src/content/chapter0_basics/lesson13_lists/text.html b/src/content/chapter0_basics/lesson13_lists/text.html new file mode 100644 index 0000000..dd07cd7 --- /dev/null +++ b/src/content/chapter0_basics/lesson13_lists/text.html @@ -0,0 +1,19 @@ +<p> + Lists are ordered collections of values. +</p> +<p> + <code>List</code> is a generic type, having a type parameter + for the type of values it contains. A list of ints has the type + <code>List(Int)</code>, and a list of strings has the type + <code>List(String)</code>. +</p> +<p> + Lists are immutable single-linked lists, meaning they are very efficient to + add and remove elements from the front of the list. +</p> +<p> + Counting the length of a list or getting elements from other positions in the + list is expensive and rarely done. It is rare to write algorithms that index + into sequences in Gleam, but but when they are written a list is not the right + choice of data structure. +</p> diff --git a/src/content/chapter0_basics/lesson14_list_functions/code.gleam b/src/content/chapter0_basics/lesson14_list_functions/code.gleam new file mode 100644 index 0000000..92d1cc6 --- /dev/null +++ b/src/content/chapter0_basics/lesson14_list_functions/code.gleam @@ -0,0 +1,15 @@ +import gleam/io +import gleam/list + +pub fn main() { + let ints = [0, 1, 2, 3, 4, 5] + + let doubled = list.map(ints, fn(x) { x * 2 }) + io.debug(doubled) + + let even = list.filter(ints, fn(x) { x % 2 == 0 }) + io.debug(even) + + let total = list.fold(ints, 0, fn(count, e) { count + e }) + io.debug(total) +} diff --git a/src/content/chapter0_basics/lesson14_list_functions/text.html b/src/content/chapter0_basics/lesson14_list_functions/text.html new file mode 100644 index 0000000..e143654 --- /dev/null +++ b/src/content/chapter0_basics/lesson14_list_functions/text.html @@ -0,0 +1,25 @@ +<p> + The <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html"><code>gleam/list</code></a> + standard library module contains functions for working with lists. A Gleam + program will likely make heavy use of this module. +</p> + +<p> + <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#map"><code>map</code></a> + makes a new list by running a function on each element in a list. +</p> +<p> + <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#filter"><code>filter</code></a> + makes a new list containing only the elements for which a function returns + true. +</p> +<p> + <a href="https://hexdocs.pm/gleam_stdlib/gleam/list.html#fold"><code>fold</code></a> + combines all the elements in a list into a single value by running a function + left-to-right on each element, passing the result of the previous call to the + next call. +</p> +<p> + It's worth getting familiar with all the functions in this module when writing + Gleam code. +</p> |