On Elixir Pt. 3
Jan 4, 2025
·
elixir
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
list1andlist2, each containing values of different types. Even further,list2contains a list! - Here, we also define
trueand5.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.putsfunction. That’s why we use a specialized functionIO.inspectto inspect and print the values oflist1andlist2.