Learning Elixir in Action

April 19, 2024

Examples that might help us learn Elixir...

programming-language elixir fun
0

I don't know anything about Elixir language, but when I want to learn a programming language, I try to make the syntax more understandable for myself with examples... I'm not even sure at this point if the title of this post should be "Learning Elixir in Action" or "Learning OCaml in Action"!?

Examples

Here we want to implement about 6 programs with different levels of difficulty — there are definitely many ways to implement them, but we will only write some of them (both due to lack of knowledge, and maybe to prevent the post from getting too long)!

Fizzbuzz

Choose two numbers and two labels. Now, if a number is divisible by the first number you chose, assign the first label to it; if a number is divisible by the second number you chose, assign the second label to it; and if a number is divisible by both of them, merge the labels together and assign it to that number...

defp means that the function cannot be invoked outside the module. In the fizzbuzz function, we defined a loop for the given range, and then we piped the current labeled number to IO.puts to see the result in the console.

Version 2 of this ridiculous game:

As you can see in the above implementations, we can write comments in the code like most programming languages; but I will put the things that need more explanation in the main body of this post...

Implementations in Bash and C++ are available in my repository:
github.com/sheikhartin/fizzbuzz

Linear Search Algorithm

In search algorithms in general, we should return a Boolean to say whether the target element exists in the given list or not; and it's not bad to throw its index too...

Version 2:

Nonsense Authentication

Nothing, we just need to check the entered password in our database to find out whether the user exists or not. It is nonsense because by looking at the code you can see the username(s) and password(s)...

In version 2, we use a struct and another module to improve design and readability...

Calculator

The calculators that are implemented here should work based on user input to know more about IO...

With @spec, you can define the type of parameters in a function and its return value.

Version 2 is more sensitive to errors:

I don't have a specific style guide yet, and I also don't know if the above solution is standard or not!?

Bubble Sort Algorithm

"Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the input list element by element, comparing the current element with the one after it, swapping their values if needed."

Maybe it is better to use curr instead of acc, and tail instead of rest as the names of our variables.

For the first time, I want to analyze the code function by function!

  • bubble/1: This function initializes the bubble sort process for a single pass. It calls bubble/3 with the rest of the list, the head as the current element to be compared, and an empty list as the accumulator for sorted elements.

  • bubble/3: Reverses the accumulator list and prepends the current element to it. In fact, it puts the largest element at the end of the sorted list.

  • bubble/3 when acc <= head: It calls itself recursively with the rest of the list, the head as the new current element, and the accumulator with the current element added to it.

  • bubble/3: This function continues the bubble sort pass when the current element is greater than the next element.

  • sort/2: This function returns the list as is, indicating that the sorting is complete.

  • sort/2: Recursively calls itself, decrementing n by 1.

  • sort/1: Calls the sort/2 function with the list and its length, which determines how many passes are needed to sort the list.

Rock, Paper, Scissors

You know it better than me!

In the above implementation, I tried to break my code into smaller parts... This is very good, but I am not a fan of using variables too much!

Version 2 for easier recognition of win conditions:

The default value considered as the number of times to play the game is 5. And yeah, it doesn't look very functional!

Implementation in Bash:
github.com/sheikhartin/rock-paper-scissors

Last Words

In my opinion, to learn a language (of any kind), you have to work many hours so that you don't think about the syntax anymore... I'm still not sure if I should continue learning this language or not; but after a few years of procedural programming, it makes me think differently about how to code, which is lovely and interesting! Maybe I should write a more important project with Elixir and then decide what to do...

Other learning resources:


Comments on this post

Be the first to comment!

Leave a comment

Comments can only be deleted by the author of the post...