On Elixir Pt. 4

Jan 4, 2025

Now, you know a few primitive data types such as Integer, Floating-point numbers, Boolean, and String. Let’s move on and talk about a few composite data types, starting with Lists.

Lists are defined using [] and can hold any type of value.

Go ahead and create a file named list.ex. Open this file in your favorite text editor and write the following code in it:

list1 = [1, true, 3, "four", 5.0]
list2 = [ [0, 1], 1, 2.0]

IO.inspect(list1)
IO.inspect(list2)

Let’s run this program, and then I’ll explain. Use the following command to run the program:

$ elixir list.ex 
[1, true, 3, "four", 5.0]
[[0, 1], 1, 2.0]

Explanation

  • The first two lines define the lists list1 and list2, each containing values of different types. Even further, list2 contains a list!
  • Here, we also define true and 5.0. These are examples of the Boolean and Floating-point data types in Elixir.
  • Finally, we cannot directly print the List data type values using the IO.puts function. That’s why we use a specialized function IO.inspect to inspect and print the values of list1 and list2.
Tags: elixir