Elixir has all the regular programming language data types along with a few special ones of its own:
Integers are positive and negative numbers without any decimal points. They can also be written in binary, octal, and hexadecimal form.
Just like other programming languages, floats are numeric variables that can store decimals values.
Boolean variables can store either True or False (true
or false
in Elixir syntax).
# Integers:IO.puts "Integers"IO.puts 1 # Integer in decimal formIO.puts 0x1F # Integer in hexadecimal formIO.puts 0b1010 # Integer in binary formIO.puts 0o123 # Integer in octal formIO.puts div(10, 5) # Div function returns integer# Floats:IO.puts "\nFloats"IO.puts 1.0 # Float valueIO.puts 10 / 5 # / operator returns a float# BooleanIO.puts "\nBoolean"IO.puts true # Boolean valueIO.puts 2 == 3 # Comparisons return boolean values
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.
Strings, similar to other languages, are sets of characters marked by double quotes ("
).
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
.
# AtomsIO.puts "Atoms"IO.puts :atom # Atom valueIO.puts is_atom(:atom) # is_atom tells whether something is an atomIO.puts is_atom(nil) # nil is an atomIO.puts is_boolean(:true) # :true is a booleanIO.puts is_atom(0) # numbers aren't atoms# StringsIO.puts "\nStrings" # This title is a string, itselfIO.puts String.length("Educative") # This function returns the length of a string# Anonymous Functions# Defining the functionssub = fn a, b -> a - b endrunfunc = fn a, b, fun -> fun.(a, b) endIO.puts "\nAnonymous Functions"IO.puts sub.(10, 5) # Calling the functionIO.puts runfunc.(10, 5, sub) # Anonymous functions can be passed as parameters
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 ([]
).
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 ({}
).
# Listslist = [1, 2, 3, 4, true, 5.5, 6, false, true] # List definedIO.puts "Lists"IO.puts "Initial List:"IO.inspect list # Inspect is used to display the whole listIO.puts "List Length:"IO.puts length listIO.puts "Adding Elements:"IO.inspect list ++ [7, 8, 9] # ++ operator joins two listsIO.puts "Removing Elements:"IO.inspect list -- [true, false] # -- operator removes items from lists# Tuplestuple = {1, 2, 3, 4, true, 5.5, 6, false, true} # Tuple definedIO.puts "\nTuples"IO.puts "Initial Tuple:"IO.inspect tuple # Inspect is used to display the whole tupleIO.puts "Accessing Tuple Element:"IO.puts elem(tuple, 5) # elem function gets the element at a certain indexIO.puts "Tuple Size:"IO.puts tuple_size(tuple) # tuple_size gets the number of elements in a tupleIO.puts "Adding Element:"IO.inspect put_elem(tuple, 5, "Educative") # put_elem adds an element at a certain index of a tupleIO.puts "Removing Element:"IO.inspect Tuple.delete_at(tuple, 4) # delete_at removes an element from a tuple at a specified index
Free Resources