CPS506 - Comparative Programming Languages - Winter 2022

Lab 4 - A Brief Introduction to Elixir

Objectives

  • Gain an understanding of the importance of concurrency
  • Understand how newer languages such as elixir provide features to support concurrent programming

Introduction

Concurrent programming, where a program or programs run multiple threads of execution in parallel, is nothing new. The concepts behind it go back decades, as Operating Systems researchers will atest to. But many programs (and programmers) to this day do not fully exploit the benefits of multithreading due to it's percieved complexity in implementation and debugging.

However, as we approach the end of Moore's law there is a new immpetus to find techniques to keep up with the ever expanding amount of data and performance we expect from modern technology. Languages like Elixir make what is old new again and try to provide tools for programmers to tame concurrency and it's percieved complexity.

Elixir

Work through the Elixir introduction up to processes

Mix

Mix is a build tool that provides tasks for creating, compiling and testing your application.

$ mix new lab4 --module LAB4

Mix will create a directory named lab4 with a few files in it. A file named mix.exs will be generated inside the folder lab4 and its responsibility is to configure the project.

  • mix compile - compiles the current project
  • mix test- runs tests for the given project
  • mix run- runs a particular command inside the project
$ cd lab4
$ mix compile

Will output:

Compiling 1 file (.ex)
Generated lab4 app

Once the project is compiled, start an iex (Interactive Shell) session inside the project by running:

$ iex -S mix

Further Information:

Expressions

Open up iex and type the following expressions:

iex> 30 +2
iex> 45-5
iex> "hello" + "world"
iex> “hello” <> “ world”
iex> list = [1, 2, 3]
iex> hd(list)
iex> tl(list)
iex> [a|b] = list
iex> a
iex> b
iex> [1, 2, 3] ++ [4, 5, 6]
iex> tuple = {:ok, "hello"}
iex> {x,y} = tuple
iex> x
iex> y
iex> map = %{:a => 10, 20 => :b}
iex> map[:a]
iex> map[20]
iex> map[:c]

Lab questions

  1. Write a program which calculates the total number of numbers which are divisible by 5 in the given list. [1,2,5,3,9,10,25,7,44,30] Also, calculate the sum of the list.
  2. Write a program to find the transpose of a matrix. (Any multidimensional array)