Trusted answers to developer questions

What are the data types in Elixir?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Data Types

Elixir has all the regular programming language data types along with a few special ones of its own:

1. Integers

Integers are positive and negative numbers without any decimal points. They can also be written in binary, octal, and hexadecimal form.

2. Floats

Just like other programming languages, floats are numeric variables that can store decimals values.

3. Boolean

Boolean variables can store either True or False (true or false in Elixir syntax).

# Integers:
IO.puts "Integers"
IO.puts 1 # Integer in decimal form
IO.puts 0x1F # Integer in hexadecimal form
IO.puts 0b1010 # Integer in binary form
IO.puts 0o123 # Integer in octal form
IO.puts div(10, 5) # Div function returns integer
# Floats:
IO.puts "\nFloats"
IO.puts 1.0 # Float value
IO.puts 10 / 5 # / operator returns a float
# Boolean
IO.puts "\nBoolean"
IO.puts true # Boolean value
IO.puts 2 == 3 # Comparisons return boolean values

4. Atoms

Atoms in Elixir are constants whose values are the same as their names. They are denoted by a : before their names. The constants true, false, and nil are also atoms. These are special constants that can be used without the : operator.

5. Strings

Strings, similar to other languages, are sets of characters marked by double quotes (").

6. Anonymous Functions

Anonymous Functions are functions that can be stored and run in variables. The start of these functions are marked by fn and the ends are marked by end.

# Atoms
IO.puts "Atoms"
IO.puts :atom # Atom value
IO.puts is_atom(:atom) # is_atom tells whether something is an atom
IO.puts is_atom(nil) # nil is an atom
IO.puts is_boolean(:true) # :true is a boolean
IO.puts is_atom(0) # numbers aren't atoms
# Strings
IO.puts "\nStrings" # This title is a string, itself
IO.puts String.length("Educative") # This function returns the length of a string
# Anonymous Functions
# Defining the functions
sub = fn a, b -> a - b end
runfunc = fn a, b, fun -> fun.(a, b) end
IO.puts "\nAnonymous Functions"
IO.puts sub.(10, 5) # Calling the function
IO.puts runfunc.(10, 5, sub) # Anonymous functions can be passed as parameters

7. Lists

Lists are a number of values of the same or different data types stored in the same variable. Lists are stored as linked lists. Lists are marked by square brackets ([]).

8. Tuples

Tuples have the same function as lists but, like arrays, they are stored contiguously in memory, which makes them faster. Tuples are marked by curly brackets ({}).

# Lists
list = [1, 2, 3, 4, true, 5.5, 6, false, true] # List defined
IO.puts "Lists"
IO.puts "Initial List:"
IO.inspect list # Inspect is used to display the whole list
IO.puts "List Length:"
IO.puts length list
IO.puts "Adding Elements:"
IO.inspect list ++ [7, 8, 9] # ++ operator joins two lists
IO.puts "Removing Elements:"
IO.inspect list -- [true, false] # -- operator removes items from lists
# Tuples
tuple = {1, 2, 3, 4, true, 5.5, 6, false, true} # Tuple defined
IO.puts "\nTuples"
IO.puts "Initial Tuple:"
IO.inspect tuple # Inspect is used to display the whole tuple
IO.puts "Accessing Tuple Element:"
IO.puts elem(tuple, 5) # elem function gets the element at a certain index
IO.puts "Tuple Size:"
IO.puts tuple_size(tuple) # tuple_size gets the number of elements in a tuple
IO.puts "Adding Element:"
IO.inspect put_elem(tuple, 5, "Educative") # put_elem adds an element at a certain index of a tuple
IO.puts "Removing Element:"
IO.inspect Tuple.delete_at(tuple, 4) # delete_at removes an element from a tuple at a specified index

RELATED TAGS

elixir
functional programming
data types
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?