Learn to Code via Tutorials on Repl.it!

← Back to all posts
Elixir Basics: Part 1 - Data types, operators, functions
h
aa2dee2 (5)

Elixir Basics: Part 1 - Data Types, Operators and Functions

Elixir is probably the most beautiful programming language I've come across!

It's a pure functional language. This means:

  • there are no 'classes', 'objects' or 'instances' as exist in object-oriented languages like python and javascript
  • all data is immutable. Once a variable is created, it cannot be changed, operations on the variable create copies of it
  • all code is written using 'expressions' or functions

In Part 1, we cover some basics on data types and syntax in Elixir.

Note: If you're new to Replit, try out the first 5 lessons in Part 1 here. You can go through them really fast.

Start a new elixir repl.

In the console window you'll find "Erlang/OTP 23.0" written.
Erlang is a programming language created by Ericsson. Elixir has been created on top of Erlang.
"OTP" refers to "Open Telecom Platform", a set of libraries that ships with Erlang and allows developers to build quickly scalable applications.

Interactive Elixir (iex) - Working in the console

In Python, you can run commands and perform operations in the console.
In Elixir, you can enter an interactive mode in the console itself, by typing iex in the console window.

You will see the following appear in your console

'iex' or interactive elixir allows you to perform operations, call on functions, set variables, determine if a statement is true and do several other things right in the console. Without having to write any scripts!

In this tutorial, we will exclusively work in iex to introduce you to elixir functions and operations.

Type h() to get an overview of what you can do in the iex console.

Two commands - i and h - are extremely helpful while working in iex.

Type i 5 and see what you get.

h refers to the 'help' command. Type h round in your console.

Now try h 3

You get an error!

i allows you to get information on an anonymous function (covered later) or variable of any data type. The command h will return information only about named functions (including operators such as + ) and modules.

Now try h Kernel.+

The 'Kernel' module is elixir's default environment which defines basic math operators, data type handling and other basic process. The Kernel module is imported by default in 'iex' and you can use the h command to know about functions defined in Kernel. (Note: the 'K' in 'Kernel' must be uppercase.)


Data types

  • integer 1, -4, 5
  • float 3.0, 6.78
  • string "programming"
    • (double quotes only. Single quotes are used for a different data type)
  • boolean true, false
  • atom/symbol :start, :apple
    • (these are like symbols. think of these as a key-value pair where the value is equal to the key itself. i.e. a variable whose value is it's own name. e.g. a :table. The : differentiates atoms from variables.)
    • similar to for, while, else, in in python.
    • some atoms pre-defined in elixir are :ok, :error
    • The booleans :true, :false are also atoms
  • list [3, 4, 5]
  • tuple {1, 2, 3}
    • (denoted with curly braces here, unlike round braces in python)
    • tuples in elixir are 'immutable' - any operations performed on these result in a new tuple, without changing the original tuple
  • charlists, short for 'character lists' 'hello'
    • (elixir treats a string of characters in single quotes as a 'charlist' - a list of integers that correspond to the ASCII numeric representation of each of the characters in 'hello'. Type i 'hello' in your iex console to see more)


Operators

Math Operators

  • Addition 1 + 2
  • Subtraction 8 - 3
  • Multiplication 4 * 6
  • Division 8 / 4 (this will return a float 2.0)

Comparison operators

  • check whether left and right are equal ==
  • greater than >
  • less than <

Other operators

  • String concatenation <>
  • Adding lists ++
  • Subtracting lists --
  • Boolean operators or, and, not
    • Note: there are symbols also for these:
      • or is same as ||
      • and is same as &&
      • not is same as !

Let's get started with some quick operations.
In your console (you should see iex(1) here) try out the following:

  • 3 + 8
  • 6 * 2
  • 15 / 4
  • "hello" <> "world"
  • :stormtrooper

Try a few more:

  • is_integer(15)
  • is_atom(:stormtrooper)
  • 87.22 > 45
  • [10, 20] ++ [30,40,50]
  • fun = "writing in elixir"
  • String.length(fun)
  • is_binary(fun)

Functions

In your interactive elixir console, try out the following:

  • trunc(5.666)

    and

  • h trunc

'trunc' is a built-in function in elixir that takes a number as input and returns the integer part of the number as output.

Now, try the following:

  • trunc = fn x -> x + x + x end
  • trunc(5.666)
  • trunc.(5.666)

You were able to define another function called trunc, that takes a number as input and adds itself to it twice i.e. returns 3 times itself.

In elixir, you can create new functions that have names same as existing 'named functions', however these are known as 'anonymous functions'. (In most languages, e.g. python, you cannot create functions that have the same name as built-in functions e.g. print, input etc.)

What distinguishes named and anonymous functions?

  • named functions are created inside 'modules'

How does elixir know which function (when there are two of the same name) to execute?

  • the '.' between the function name and the parenthese with arguments tells elixir we're referring to an anonymous function that we defined.
  • if the '.' is missing, elixir will call upon the named function

Test Yourself!

Here are some quick questions:
(try all of these in your iex console. You will need to do the questions in order.)

  1. Create two lists called 'first' with elements [45, "jump", 66] and 'second' with elements ["ogre", 32, 89]. Create another list 'third' that is the sum of these two lists.
  2. Create a list 'fourth' that has all elements from 'third' except "ogre".
  3. Create a function called 'inverse' that takes a number as input and returns 1 divided by the number as output.
  4. Call the 'inverse' function using the following and store the result in corresponding variable names.
    • input is 20, variable name twenty
    • input is 51, variable name is fiftyone
  5. Add the variables twenty and fiftyone and store the result in a variable seventyone. Call the 'round' function on the result of the addition. (Yes. I know 1/20 + 1/51 is not the same as 1/71).
  6. Check whether the result of calling the trunc function and the round function on 'seventyone' is the same. (Use comparison operators here)

Solutions will be revealed in the Part 2 Tutorial.

We'll also cover

  • advanced data types
  • more functions
  • more on the Kernel module

Part 2 coming up early next week.

Ciao!

Comments
hotnewtop
DynamicSquid (4924)

This is an amazing tutorial! You don't see a lot of Elixir being used around here, so it's nice when it pops up :)

aa2dee2 (5)

thank you, @DynamicSquid
LMK if you have any specific ideas on what I should cover.