Getting Started with Elixir: The Basics

March 8, 2024

Welcome to our beginner’s guide to Elixir! In this post, we will dive into the basics of Elixir, including its syntax, data types, and basic operations. By the end of this lesson, you will have a solid understanding of the fundamental building blocks of Elixir.

Syntax

Elixir has a clean and straightforward syntax that is easy to read and write. Let’s start by looking at a simple ‘Hello, World!’ example:

<span class="keyword">IO.puts</span>("Hello, World!")

In Elixir, functions are called using the dot notation. The IO.puts function is used to output text to the console. The semicolon at the end of the line is optional.

Data Types

Elixir has several built-in data types, including integers, floats, booleans, atoms, strings, lists, tuples, and maps. Let’s take a quick look at a few examples:

<span class="keyword">i</span> = 10
<span class="keyword">f</span> = 3.14
<span class="keyword">b</span> = true
<span class="keyword">a</span> = :ok
<span class="keyword">s</span> = "Elixir"
<span class="keyword">l</span> = [1, 2, 3]
<span class="keyword">t</span> = {1, 2, 3}
<span class="keyword">m</span> = %{name: "John", age: 30}

In the above examples, i is an integer, f is a float, b is a boolean, a is an atom, s is a string, l is a list, t is a tuple, and m is a map.

Basic Operations

Elixir supports a wide range of basic operations for working with data. Here are a few common operations:

<span class="keyword">a</span> = 10
<span class="keyword">b</span> = 20

<span class="keyword">sum</span> = <span class="keyword">a</span> + <span class="keyword">b</span>
<span class="keyword">difference</span> = <span class="keyword">b</span> - <span class="keyword">a</span>
<span class="keyword">product</span> = <span class="keyword">a</span> * <span class="keyword">b</span>
<span class="keyword">quotient</span> = <span class="keyword">b</span> / <span class="keyword">a</span>

In the above examples, we perform basic arithmetic operations using the +, -, *, and / operators.

Congratulations! You’ve now learned the basics of Elixir, including its syntax, data types, and basic operations. Stay tuned for more advanced topics in our future posts!