DEV Community

Cover image for An Introduction to Basic Types in Elixir
MKilmer
MKilmer

Posted on

An Introduction to Basic Types in Elixir

Before all, this post it aims to share and learn a new programming language ( called Elixir) and new knowledges about functional programming world.Like any begin in any programming language, it important start learn about syntax and the basic data types. So, let's go!

just wait a little longer, I swear it will be important.I need talk about print to the console in Elixir.

Console

If you are running on Windows, there is a chance your terminal does not use UTF-8 by default. You can change the encoding of your current session by running chcp 65001 before entering IEx.

Basicaly, exists two way for print to console in Elixir :

IO.puts : requires the argument to be either a string, or a struct that implements the String.Chars protocol.

IO.inspect : can print an arbitrary elixir term, like a list of strings.

ok, now let's go!

Integer

Represent numbers without floating point.These are the methods of this kind :

sum

sum = 7 + 1
IO.puts(sum)

output : 8
Enter fullscreen mode Exit fullscreen mode

division ( div( ) )

division = div(9,2)
IO.puts(division)

output : 4
# the div() method will always take integers as parameters and return integer values.

Enter fullscreen mode Exit fullscreen mode

rem ( division remainder )

remMethod = rem(10,3)
IO.puts remMethod

output : 1
# Elixir allows you to drop the parentheses when invoking named function.
Enter fullscreen mode Exit fullscreen mode

subtraction

sub = 9-18
IO.puts sub

output : -9
Enter fullscreen mode Exit fullscreen mode

You can use is_integer() method to verify if a value is integer

isOrNotInteger = "9"
IO.puts is_integer(isOrNotInteger)

output : false

Enter fullscreen mode Exit fullscreen mode

Binary, Octal and Hexadecimal

binary : begins with 0b
octal : begins with 0o
hex : begins with 0x

bin = 0b1010
octal = 0o666
hex = 0x1D
Enter fullscreen mode Exit fullscreen mode

Both binary, octal and hexadecimal types allow the use of the is_integer () method. If you use IO.puts, you can represent these values ​​in decimal basis.

IO.puts is_integer(bin)
IO.puts is_integer(octal)
IO.puts is_integer(hex)

output : 
true
true
true

IO.puts (bin)
IO.puts (octal)
IO.puts (hex)

output :
10
438
29
Enter fullscreen mode Exit fullscreen mode

Float
Represent numbers with floating point

sum

sum = 12.0 + 1
IO.puts(sum)

output : 13.0
# note that if you have an operation that has a floating point number, the end result will be a float.

Enter fullscreen mode Exit fullscreen mode

subtraction

sub = 12.01 - 7.89
IO.puts(sub)

output : 4.12
Enter fullscreen mode Exit fullscreen mode

division ( / )

div = 9/2
IO.puts(div)

output : 4.5
# note that it's allow use integers in operator / , but always return a float number.
Enter fullscreen mode Exit fullscreen mode

rem method does not work with floating point numbers

IO.puts( rem(12.9,2) )
output :
 (ArithmeticError) bad argument in arithmetic expression: rem(12.9, 2)
Enter fullscreen mode Exit fullscreen mode

Booleans
Elixir basicaly have two values for represent booelans values, true and false

IO.puts true
IO.puts false
IO.puts false == false

output :
true
false
true
Enter fullscreen mode Exit fullscreen mode

Atom

An atom is a constant whose value is its own name, like Symbols in others languages. Often they are used to express the state of an operation.

If you enter in Interative mode ( run iex ), after the end of a successful operation, an :ok is always returned.

iex(1)> IO.puts "atoms"
atoms
:ok
Enter fullscreen mode Exit fullscreen mode

Elixir allows you to skip the leading : for the atoms : nil , false, true

IO.puts :nil == nil
IO.puts :false == false
IO.puts :true == true

output :
true
true
true

IO.puts :elixir == "elixir"
IO.puts :elixir == :elixir

output :
false
true
Enter fullscreen mode Exit fullscreen mode

String

Strings in Elixir are delimited by double quotes, and they are encoded in UTF-8.The String module contains a bunch of functions that operate on strings, see in HexDocs.

Strings are represented internally by binaries which are sequences of bytes. Single quotes aren't represented like binaries.

doubleQuotes = "DEV"
singleQuotes = 'DEV'
IO.puts is_binary(doubleQuotes)
IO.puts is_binary(singleQuotes)

output : 
true
false
Enter fullscreen mode Exit fullscreen mode

concatenation

Unlike some languages, elixir does not allow the + operator to join strings, in this case, we use <>

IO.puts "DEV" + "Community"

output : 
(ArithmeticError) bad argument in arithmetic expression: "DEV" + "Community"
Enter fullscreen mode Exit fullscreen mode
IO.puts "DEV" <> "Community"

output :
"DEVCommunity"
Enter fullscreen mode Exit fullscreen mode

string interpolation

Elixir allow you to use string interpolation

world = :world
IO.puts "hello #{world}"

output :
hello world
Enter fullscreen mode Exit fullscreen mode

break line and multiple lines

# use \n for break line
IO.puts "hello\nworld"

output :
hello
world

# multiple lines
multiLine = "
breathe
think
continues"
IO.puts multiLine

output :
breathe
think
continues
Enter fullscreen mode Exit fullscreen mode

preview of some methods

dev = "DEV"
IO.puts is_binary dev 
IO.puts byte_size dev
IO.puts String.length dev

output : 
true
3
3

split = String.split("Led Zeppelin", " ")
IO.inspect split

output : 
["Led", "Zeppelin"]

Enter fullscreen mode Exit fullscreen mode

It was a pleasure learning and writing a little about Elixir, any questions or criticism, please comment below 🤘 🤘 🤘

Top comments (0)