Introduction
Interactive mode: iex
Initial Commands:
iex> 40 + 2
42
iex> "hello" <> " world"
"hello world"
iex> 40 + 2
42
iex> "hello" <> " world"
"hello world"
Running Scripts: elixir hello-word.exs
Hello Word:
IO.puts "Hello Word"
IO.puts "Hello Word"
"Hello Word"
"Hello Word"
Basic types
Get Data Type: i data
iex> i 1
iex> i 1
will show something like this:
Term
1
Data type
Integer
Reference modules
Integer
Implemented protocols
IEx.Info, Inspect, List.Chars, String.Chars
Basic Types
iex> 1 # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> true # boolean
iex> :atom # atom / symbol
iex> "elixir" # string
iex> [1, 2, 3] # list
iex> {1, 2, 3} # tuple
iex> 1 # integer
iex> 0x1F # integer
iex> 1.0 # float
iex> true # boolean
iex> :atom # atom / symbol
iex> "elixir" # string
iex> [1, 2, 3] # list
iex> {1, 2, 3} # tuple
Math
iex> 1 + 2
3
iex> 5 * 5
25
iex> 10 / 2
5.0
iex> 1 + 2
3
iex> 5 * 5
25
iex> 10 / 2
5.0
10/2
always return a float number, to get an integer, use div
and rem
functions:
iex> div(10, 2)
5
iex> div 10, 2
5
iex> rem 10, 3
1
iex> div(10, 2)
5
iex> div 10, 2
5
iex> rem 10, 3
1
Shortcut Notations
iex> 0b1010 # binary
10
iex> 0o777 # octal
511
iex> 0x1F # hexadecimal
31
iex> 0b1010 # binary
10
iex> 0o777 # octal
511
iex> 0x1F # hexadecimal
31
Scientific Notation
iex> 1.0
1.0
iex> 1.0e-10
1.0e-10
iex> 1.0
1.0
iex> 1.0e-10
1.0e-10
Round Function
iex> round(3.58)
4
iex> trunc(3.58)
3
iex> round(3.58)
4
iex> trunc(3.58)
3