https://peq42.com/blog/gleam-a-basic-introduction/ Skip to content Peq42 Peq42 * Peq42 Peq42 * [ ] Search Search for: * Home * Account * Blog * Random Post * Games & Projects * Forums * Commissions * Support me Gleam: A Basic Introduction Home >> Blog >> Gleam: A Basic Introduction Gleam: A Basic Introduction March 12, 2024 Other tutorials and tips Gleam: A Basic Introduction Read Time: 4 min. peq42 Leave a comment on Gleam: A Basic Introduction Gleam is a statically-typed functional programming language, which recently had its v1 released, designed for building scalable and maintainable software. It is inspired by languages like Elm and Rust and compiles down to Erlang, making it well-suited for building concurrent and distributed systems. In this tutorial, we'll cover the basics of Gleam to help you get started with this expressive and powerful language. Preparing the environment Before diving into Gleam, you need to set up the development environment. The language compiles to Erlang and Javascript, and while the second doesn't require anything but a browser and the gleam compiler to run, the first will need the compiler, Erlang and Rebar3. Let's focus on that: 1. First, grab a pre-compiled version of Gleam here: https:// github.com/gleam-lang/gleam/releases 2. Then, install Erlang from the Erlang-Solutions website. 3. Finally, go to rebar3 "getting started" page and follow their instructions. This tool will be necessary in certain cases, such as Web Servers and HTTP clients. With everything in place, install a plugin to support syntax highlighting in your editor of choice. Your First Gleam Program Let's create a simple "Hello, World!" program to get started. Open your favorite text editor and create a file named hello.gleam with the following content: // hello.gleam import gleam/io pub fn main() { io.println("Hello, Gleam!") } Save the file and run it using the Gleam compiler: gleam run hello.gleam You should see the output: Hello, Gleam! Congratulations! You've just written and executed your first Gleam program. Variables and Types Gleam is a statically-typed language, meaning you must declare the type of a variable before using it. Here's an example demonstrating variable declaration and basic types: // variables.gleam import gleam/io pub fn main() { let message: String = "Hello, Gleam!" let number: Int = 42 let is_true: Bool = True let ints = [1, 2, 3] io.debug(ints) io.debug(message) io.debug(number) io.debug(is_true) } Run the program to see the output: gleam run variables.gleam As you may have noticed, this time we used io.debug instead of io.println . That is because the later only works with strings, while the first will print any type. Arithmetic Operations Gleam supports standard arithmetic operations for numerical values. Here's a concise example demonstrating addition: 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)) //highest number io.debug(int.clamp(5, 10, 20)) } In this program, the + operator adds the numbers 3 and 5. Run the program: gleam run arithmetic.gleam This should output: 2 4 2 9 1 True False True False True False 77 10 Functions Functions are a fundamental part of Gleam. Here's an example of defining and calling a function: // functions.gleam import gleam/io pub fn main() { let result = add(3, 5) io.debug(result) //8 } fn add(x: Int, y: Int) -> Int { x + y } Run the program and observe the output: gleam run functions.gleam Case expressions In Gleam, case expressions offer a concise and expressive way to handle multiple conditions. Here's a simple example demonstrating how to use case expressions for pattern matching: import gleam/io import gleam/int pub fn main() { let x = int.random(5) io.debug(x) let result = case x { // Match specific values 0 -> "Zero" 1 -> "One" // Match any other value _ -> "Other" } io.debug(result) } This code generates a random integer between 0 and 4 (inclusive) and then uses a case expression to match and print a corresponding message based on the generated value. Conclusion In conclusion, Gleam emerges as a compelling language for developers seeking a balance between the expressiveness of functional programming and the performance of concurrent and distributed systems. Drawing inspiration from Elm and Rust, Gleam's statically-typed nature ensures robustness and maintainability in software development, while being simple and easy to learn over an afternoon. If you wish to learn more, Gleam has a language tour available on their website! [surfshark-] Get articles on your E-mail [ ][Subscribe] I accept the privacy policy programming tutorial You may also like: * alvr linux quest tutorial Tutorial - VR on Linux with Meta Quest and ALVR September 8, 2024 * DNS hijacking What is DNS Hijacking? August 25, 2024 * Zig: A Basic Introduction Zig: A Basic Introduction August 10, 2024 peq42 Owner of this website, programmer, 3D artist, video editor, lazy gamer and always trying to be friendly Leave a Reply Cancel reply Your email address will not be published. Required fields are marked * [ ] [ ] [ ] [ ] [ ] [ ] [ ] Comment [ ] Name * [ ] Email * [ ] [Post Comment] (c) 2024 * Home * About me * Privacy Policy * Submit a post * Contact [ ] Search Search for: Go to top