Learn 12 programming languages in 23: Elixir

Samuel Teixeira
4 min readFeb 28, 2023

--

Learning my second programming language this year.

February: I learned my second language using Exercism as my primary source. #12in23

Elixir it’s the second functional language that I had contact with. The first one was Haskel when I was studying in college. I have just a distant memory of what is like to use Haskel as a programming language.

Using a functional paradigm is so different from what I use in my day-to-day work, that gave me the feeling of magic while resolving some problems with it.

Elixir is fluid and nice to learn

I didn’t have any problems learning the basics and understanding the functional parts to me was like understanding what are the stop conditions in a recursive program.

Elixir it’s not a boring language!

Elixir: You have modules that work like a class in Java, and inside the module functions.

defmodule HighSchoolSweetheart do
def first_letter(name) do
String.trim(name)
|> String.first()
end
end

Btw: Medium doesn't have syntax highlight for Elixir :(

The code above has two important things to learn. The functions don’t need a return and we have the |>(pipe operator).

The pipe operator |> passes the result of an expression as the first parameter of another expression.

Results of String.first function in Elixir

The String.first() function expected a string as an argument, but in the code above with the pipe, we the pipe sending it to that function. So the return of that function will be the first letter of that name.

Resolving a problem using functional programming

This is the definition of a problem guessing-game

You are creating a trivial online game where a friend can guess a secret number. You want to give some feedback, but not give away the answer with a guess. You need to devise a function to provide different responses depending on how the guess relates to the secret number.

Possible answers from the Guessing Game
defmodule GuessingGame do
def compare(s, g) when s === g, do: "Correct"
def compare(s, g) when s == g+1 or s == g-1 , do: "So close"
def compare(s, g) when g == :no_guess, do: "Make a guess"
def compare(s, g) when s < g, do: "Too high"
def compare(s, g) when s > g, do: "Too low"
def compare(s) when true, do: "Make a guess"
end

Elixir can solve it using six lines. Six lines and that’s it. If you don’t think that’s amazing I don’t know what it is.

It’s like solving a puzzle

When I was solving some of the problems and I could use the functional approach, was like solving a little puzzle.

defmodule BirdCount do
def today([]), do: nil
def today([h | _]), do: h

def increment_day_count([]), do: [1]
def increment_day_count([h | t]), do: [h + 1 | t]

def has_day_without_birds?([]), do: false
def has_day_without_birds?([0|t]), do: true
def has_day_without_birds?([_|t]), do: has_day_without_birds?(t)

def total([]), do: 0
def total([h|t]), do: h + total(t)

def busy_days([]), do: 0
def busy_days([h|t]) when h >= 5, do: 1 + busy_days(t)
def busy_days([_|t]), do: busy_days(t)
end

This is my solution to the BirdCount problem.

Can you say what it does?

I will explain two functions today and total, but first I need to explain a new concept to you.

Lists in Elixir are implemented as linked lists, and not as arrays of a contiguous memory location. Therefore, accessing an element in a list takes linear time depending on the length of the list.

Lists can be written in literal form, head-tail notation, (which uses the cons operator |), or a combination of both:

head and tail notation

Today method has two forms:

1 — If the list received it’s empty, it returns nil.

2 — If there is at least one element, it will get using the “head and tail”, notation, and returns the head. The _ means it can be anything as the tail.

Total method has two forms:

1 — If the list it’s empty returns zero. This will be the stop condition of the recursion.

2 — For the head and tail in that list, get the head + total(tail). That will sum up all elements in the list. This is a recursive definition.

My progress with Elixir

I could explore many concepts and finished 18 problems from the Elixir track. This is one that I will continue to learn more about because I enjoyed using it a lot.

Elixir interview with the creator

The Exercism team did an AMA with the creator of Elixir and it was fascinating the reasons why he decided to create this new language.

The full video it’s on youtube if you want to know more.

--

--

Samuel Teixeira
Samuel Teixeira

Written by Samuel Teixeira

Developer (15+ years) | Staff Engineer at IAS | Master of Computer Science | Interests — Backend Dev , Machine Learn, Code performance, System Design

Responses (1)