You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If tests pass, you are ready to go. Otherwise, feel free to open an issue [in the issues tracker on Github](https://github.com/elixir-lang/elixir).
42
44
@@ -49,88 +51,110 @@ If tests pass, you are ready to go. Otherwise, feel free to open an issue [in th
49
51
50
52
We can start Elixir interactive mode by running `bin/iex` in the same directory you compiled Elixir or by simply running `iex` if you installed it from a distribution. In interactive mode, we can type any Elixir expression. Let's warm up with some basic arithmetic expressions:
51
53
52
-
iex> 1 + 1
53
-
2
54
-
iex> 10 - 5
55
-
5
56
-
iex> 10 / 2
57
-
5.0
54
+
{% highlight iex %}
55
+
iex> 1 + 1
56
+
2
57
+
iex> 10 - 5
58
+
5
59
+
iex> 10 / 2
60
+
5.0
61
+
{% endhighlight %}
58
62
59
63
Notice `10 / 2` returned a float `5.0` instead of an integer. This is expected, in Elixir the operator `/` always returns a float. In case you want to do integer division or get the division remainder, you can invoke the `div` and `rem` functions:
60
64
61
-
iex> div(10, 2)
62
-
5
63
-
iex> div 10, 2
64
-
5
65
-
iex> rem 10, 3
66
-
1
65
+
{% highlight iex %}
66
+
iex> div(10, 2)
67
+
5
68
+
iex> div 10, 2
69
+
5
70
+
iex> rem 10, 3
71
+
1
72
+
{% endhighlight %}
67
73
68
74
In the example above, we called two functions called `div` and `rem`. Notice that parenthesis are not required in order to invoke a function. We are going to discuss more about it later. Let's move forward and see which other data types we have in Elixir:
69
75
70
76
## 1.3 Basic types
71
77
72
78
Some basic types are:
73
79
74
-
iex> 1 # integer
75
-
iex> 1.0 # float
76
-
iex> :atom # atom / symbol
77
-
iex> {1,2,3} # tuple
78
-
iex> [1,2,3] # list
80
+
{% highlight iex %}
81
+
iex> 1 # integer
82
+
iex> 1.0 # float
83
+
iex> :atom # atom / symbol
84
+
iex> {1,2,3} # tuple
85
+
iex> [1,2,3] # list
86
+
{% endhighlight %}
79
87
80
88
Elixir also provides functions (note the dot between the variable and arguments when calling a function):
81
89
82
-
# function
83
-
iex> x = fn(a, b) -> a + b end
84
-
#Fun<erl_eval.12.111823515>
85
-
iex> x.(1, 2)
86
-
3
90
+
{% highlight iex %}
91
+
# function
92
+
iex> x = fn(a, b) -> a + b end
93
+
#Fun<erl_eval.12.111823515>
94
+
iex> x.(1, 2)
95
+
3
96
+
{% endhighlight %}
87
97
88
98
And double-quoted strings:
89
99
90
-
iex> "string"
91
-
"string"
100
+
{% highlight iex %}
101
+
iex> "string"
102
+
"string"
103
+
{% endhighlight %}
92
104
93
105
Strings in Elixir are UTF-8 binaries delimited by double quotes. A single-quoted string in Elixir is called a char list and is simply a list of characters:
94
106
95
-
iex> 'string'
96
-
'string'
107
+
{% highlight iex %}
108
+
iex> 'string'
109
+
'string'
110
+
{% endhighlight %}
97
111
98
112
There is also string interpolation:
99
113
100
-
iex> name = "world"
101
-
iex> "hello #{name}"
102
-
"hello world"
114
+
{% highlight iex %}
115
+
iex> name = "world"
116
+
iex> "hello #{name}"
117
+
"hello world"
118
+
{% endhighlight %}
103
119
104
120
We can use the `is_binary` and `is_list` helpers to detect if a given string is a binary (double-quoted) or a list (single-quoted):
105
121
106
-
iex> is_binary "string"
107
-
true
108
-
iex> is_list "string"
109
-
false
122
+
{% highlight iex %}
123
+
iex> is_binary "string"
124
+
true
125
+
iex> is_list "string"
126
+
false
110
127
111
-
iex> is_binary 'string'
112
-
false
113
-
iex> is_list 'string'
114
-
true
128
+
iex> is_binary 'string'
129
+
false
130
+
iex> is_list 'string'
131
+
true
132
+
{% endhighlight %}
115
133
116
134
Although they represent the same thing, double-quoted and single-quoted strings are different and best suited for different scenarios:
117
135
118
-
iex> "string" == 'string'
119
-
false
136
+
{% highlight iex %}
137
+
iex> "string" == 'string'
138
+
false
139
+
{% endhighlight %}
120
140
121
141
Most of the cases, developers should use double-quoted strings as their representation is more compact. We are going to discuss this subject with more detail in the next chapter.
122
142
123
143
Elixir also provides `true` and `false` as booleans:
124
144
125
-
iex> true
126
-
true
127
-
iex> is_boolean false
128
-
true
145
+
{% highlight iex %}
146
+
iex> true
147
+
true
148
+
iex> is_boolean false
149
+
true
150
+
{% endhighlight %}
129
151
130
152
Booleans are represented internally as atoms:
131
153
132
-
iex> is_atom(true)
133
-
true
154
+
{% highlight iex %}
155
+
iex> is_atom(true)
156
+
true
157
+
{% endhighlight %}
134
158
135
159
Elixir also provides Port, References and PIDs as data types (usually used in process communication) but they are out of the scope of a getting started tutorial. For now, let's take a look at the basic operators in Elixir before we move on to the next chapter.
136
160
@@ -140,70 +164,88 @@ As we saw earlier, Elixir provides `+`, `-`, `*`, `/` as arithmetic operators.
140
164
141
165
Elixir also provides `++` and `--` to manipulate lists:
142
166
143
-
iex> [1,2,3] ++ [4,5,6]
144
-
[1,2,3,4,5,6]
145
-
iex> [1,2,3] -- [2]
146
-
[1,3]
167
+
{% highlight iex %}
168
+
iex> [1,2,3] ++ [4,5,6]
169
+
[1,2,3,4,5,6]
170
+
iex> [1,2,3] -- [2]
171
+
[1,3]
172
+
{% endhighlight %}
147
173
148
174
Since single-quoted strings are lists, we can also use `++` and `--` as operators to manipulate them:
149
175
150
-
iex> 'some' ++ ' example'
151
-
'some example'
152
-
iex> 'some' -- 'oe'
153
-
'sm'
176
+
{% highlight iex %}
177
+
iex> 'some' ++ ' example'
178
+
'some example'
179
+
iex> 'some' -- 'oe'
180
+
'sm'
181
+
{% endhighlight %}
154
182
155
183
Notice that `++` and `--` cannot be used to manipulate double quoted strings, as they are binaries. That said, string concatenation is done via `<>`:
156
184
157
-
iex> "foo" <> "bar"
158
-
"foobar"
185
+
{% highlight iex %}
186
+
iex> "foo" <> "bar"
187
+
"foobar"
188
+
{% endhighlight %}
159
189
160
190
Elixir also provides three boolean operators: `or`, `and` and `not`. Those operators are strict in the sense those operators expects only booleans (true or false) as arguments:
161
191
162
-
iex> true and true
163
-
true
164
-
iex> false or is_atom(:example)
165
-
true
192
+
{% highlight iex %}
193
+
iex> true and true
194
+
true
195
+
iex> false or is_atom(:example)
196
+
true
197
+
{% endhighlight %}
166
198
167
199
Giving a non-boolean as argument will raise an exception:
168
200
169
-
iex> 1 and true
170
-
** (::ArgumentError) argument error
201
+
{% highlight iex %}
202
+
iex> 1 and true
203
+
** (::ArgumentError) argument error
204
+
{% endhighlight %}
171
205
172
206
`or` and `and` are short-circuit operators. They just execute the right side in case the left side is not enough to determine the result:
173
207
174
-
iex> false and error("This error will never be raised")
175
-
false
208
+
{% highlight iex %}
209
+
iex> false and error("This error will never be raised")
210
+
false
176
211
177
-
iex> true or error("This error will never be raised")
178
-
true
212
+
iex> true or error("This error will never be raised")
213
+
true
214
+
{% endhighlight %}
179
215
180
216
> Note: If you are an Erlang developer, `and` and `or` in Elixir actually map to the `andalso` and `orelse` operators in Erlang.
181
217
182
218
Elixir also provides `==`, `!=`, `===`, `!===`, `<=`, `>=`, `<` and `>` as comparison operators:
183
219
184
-
iex> 1 == 1
185
-
true
186
-
iex> 1 != 2
187
-
true
188
-
iex> 1 < 2
189
-
true
220
+
{% highlight iex %}
221
+
iex> 1 == 1
222
+
true
223
+
iex> 1 != 2
224
+
true
225
+
iex> 1 < 2
226
+
true
227
+
{% endhighlight %}
190
228
191
229
The difference between `==` and `===` is that the latter is more strict when comparing integers and floats:
192
230
193
-
iex> 1 == 1.0
194
-
true
195
-
iex> 1 === 1.0
196
-
false
231
+
{% highlight iex %}
232
+
iex> 1 == 1.0
233
+
true
234
+
iex> 1 === 1.0
235
+
false
236
+
{% endhighlight %}
197
237
198
238
In Elixir, we can compare two different data types:
199
239
200
-
iex> 1 < :atom
201
-
true
240
+
{% highlight iex %}
241
+
iex> 1 < :atom
242
+
true
243
+
{% endhighlight %}
202
244
203
245
The reason we can compare different data types is for pragmatism. Sorting algorithms don't need to worry about different data types in order to sort. The overall sorting order is defined below:
204
246
205
247
number < atom < reference < functions < port < pid < tuple < list < bit string
206
248
207
249
You actually don't need to memorize this ordering, it is important just to know an order exists.
208
250
209
-
Well, that is it for the introduction. In the next chapter, we are going to discuss some basic functions, data types conversions and a bit of control-flow.
251
+
Well, that is it for the introduction. In the next chapter, we are going to discuss some basic functions, data types conversions and a bit of control-flow.
0 commit comments