Basic arithmetic
iex> 1 + 2 |
Elixir also supports shortcut notations for entering binary, octal, and hexadecimal numbers:
iex> 0b1010 |
Scientific notation:
iex> 1.0 |
iex> round(3.58) |
Booleans
Elixir supports true
and false
as booleans:
iex> true |
Elixir provides a bunch of predicate functions to check for a value type:
is_boolean/1
is_integer/1
is_float/1
is_number/1
The
h
helper can also be used to access documentation for any function. For exampleh is_integer/1
orh ==/1
.
Atom
An atom is a constant whose name is its own value. Some other languages call these symbols:
iex> :hello |
The booleans true
and false
are, in fact, atoms:
iex> true == :true |
Finally, Elixir has a construct called aliases which we will explore later. Aliases start in upper case are also atoms:
iex> is_atom(Hello) |
String
Strings in Elixir are delimited by double quotes, and they are encoded in UTF-8:
iex> "hellö" |
We can print a string using the IO.puts/1
:
iex> IO.puts "hellonworld" |
Anonymous functions
iex> add = fn a, b -> a + b end |
(Linked)Lists
List operators never modify the existing list:
iex> [1, 2, true, 3] |
The functions hd/1
and tl/1
:
iex> list = [1, 2, 3] |
Tuples
iex> {:ok, "hello"} |
Basic Operators
++
, --
, <>
:
iex> [1, 2, 3] ++ [4, 5, 6] |
or
,and
,not
||
,&&
,!
==
,!=
,===
,!==
,<=
,>=
,<
,>
Note: If you are an Erlang developer, and and or in Elixir actually map to the andalso and orelse operators in Erlang.
iex> true and true |
number < atom < reference < function < port < pid < tuple < map < list < bitstring
近期评论