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).
44
42
@@ -51,110 +49,88 @@ If tests pass, you are ready to go. Otherwise, feel free to open an issue [in th
51
49
52
50
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:
53
51
54
-
{% highlight iex %}
55
-
iex> 1 + 1
56
-
2
57
-
iex> 10 - 5
58
-
5
59
-
iex> 10 / 2
60
-
5.0
61
-
{% endhighlight %}
52
+
iex> 1 + 1
53
+
2
54
+
iex> 10 - 5
55
+
5
56
+
iex> 10 / 2
57
+
5.0
62
58
63
59
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:
64
60
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 %}
61
+
iex> div(10, 2)
62
+
5
63
+
iex> div 10, 2
64
+
5
65
+
iex> rem 10, 3
66
+
1
73
67
74
68
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:
75
69
76
70
## 1.3 Basic types
77
71
78
72
Some basic types are:
79
73
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 %}
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
87
79
88
80
Elixir also provides functions (note the dot between the variable and arguments when calling a function):
89
81
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 %}
82
+
# function
83
+
iex> x = fn(a, b) -> a + b end
84
+
#Fun<erl_eval.12.111823515>
85
+
iex> x.(1, 2)
86
+
3
97
87
98
88
And double-quoted strings:
99
89
100
-
{% highlight iex %}
101
-
iex> "string"
102
-
"string"
103
-
{% endhighlight %}
90
+
iex> "string"
91
+
"string"
104
92
105
93
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:
106
94
107
-
{% highlight iex %}
108
-
iex> 'string'
109
-
'string'
110
-
{% endhighlight %}
95
+
iex> 'string'
96
+
'string'
111
97
112
98
There is also string interpolation:
113
99
114
-
{% highlight iex %}
115
-
iex> name = "world"
116
-
iex> "hello #{name}"
117
-
"hello world"
118
-
{% endhighlight %}
100
+
iex> name = "world"
101
+
iex> "hello #{name}"
102
+
"hello world"
119
103
120
104
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):
121
105
122
-
{% highlight iex %}
123
-
iex> is_binary "string"
124
-
true
125
-
iex> is_list "string"
126
-
false
106
+
iex> is_binary "string"
107
+
true
108
+
iex> is_list "string"
109
+
false
127
110
128
-
iex> is_binary 'string'
129
-
false
130
-
iex> is_list 'string'
131
-
true
132
-
{% endhighlight %}
111
+
iex> is_binary 'string'
112
+
false
113
+
iex> is_list 'string'
114
+
true
133
115
134
116
Although they represent the same thing, double-quoted and single-quoted strings are different and best suited for different scenarios:
135
117
136
-
{% highlight iex %}
137
-
iex> "string" == 'string'
138
-
false
139
-
{% endhighlight %}
118
+
iex> "string" == 'string'
119
+
false
140
120
141
121
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.
142
122
143
123
Elixir also provides `true` and `false` as booleans:
144
124
145
-
{% highlight iex %}
146
-
iex> true
147
-
true
148
-
iex> is_boolean false
149
-
true
150
-
{% endhighlight %}
125
+
iex> true
126
+
true
127
+
iex> is_boolean false
128
+
true
151
129
152
130
Booleans are represented internally as atoms:
153
131
154
-
{% highlight iex %}
155
-
iex> is_atom(true)
156
-
true
157
-
{% endhighlight %}
132
+
iex> is_atom(true)
133
+
true
158
134
159
135
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.
160
136
@@ -164,88 +140,70 @@ As we saw earlier, Elixir provides `+`, `-`, `*`, `/` as arithmetic operators.
164
140
165
141
Elixir also provides `++` and `--` to manipulate lists:
166
142
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 %}
143
+
iex> [1,2,3] ++ [4,5,6]
144
+
[1,2,3,4,5,6]
145
+
iex> [1,2,3] -- [2]
146
+
[1,3]
173
147
174
148
Since single-quoted strings are lists, we can also use `++` and `--` as operators to manipulate them:
175
149
176
-
{% highlight iex %}
177
-
iex> 'some' ++ ' example'
178
-
'some example'
179
-
iex> 'some' -- 'oe'
180
-
'sm'
181
-
{% endhighlight %}
150
+
iex> 'some' ++ ' example'
151
+
'some example'
152
+
iex> 'some' -- 'oe'
153
+
'sm'
182
154
183
155
Notice that `++` and `--` cannot be used to manipulate double quoted strings, as they are binaries. That said, string concatenation is done via `<>`:
184
156
185
-
{% highlight iex %}
186
-
iex> "foo" <> "bar"
187
-
"foobar"
188
-
{% endhighlight %}
157
+
iex> "foo" <> "bar"
158
+
"foobar"
189
159
190
160
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:
191
161
192
-
{% highlight iex %}
193
-
iex> true and true
194
-
true
195
-
iex> false or is_atom(:example)
196
-
true
197
-
{% endhighlight %}
162
+
iex> true and true
163
+
true
164
+
iex> false or is_atom(:example)
165
+
true
198
166
199
167
Giving a non-boolean as argument will raise an exception:
200
168
201
-
{% highlight iex %}
202
-
iex> 1 and true
203
-
** (::ArgumentError) argument error
204
-
{% endhighlight %}
169
+
iex> 1 and true
170
+
** (::ArgumentError) argument error
205
171
206
172
`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:
207
173
208
-
{% highlight iex %}
209
-
iex> false and error("This error will never be raised")
210
-
false
174
+
iex> false and error("This error will never be raised")
175
+
false
211
176
212
-
iex> true or error("This error will never be raised")
213
-
true
214
-
{% endhighlight %}
177
+
iex> true or error("This error will never be raised")
178
+
true
215
179
216
180
> Note: If you are an Erlang developer, `and` and `or` in Elixir actually map to the `andalso` and `orelse` operators in Erlang.
217
181
218
182
Elixir also provides `==`, `!=`, `===`, `!===`, `<=`, `>=`, `<` and `>` as comparison operators:
219
183
220
-
{% highlight iex %}
221
-
iex> 1 == 1
222
-
true
223
-
iex> 1 != 2
224
-
true
225
-
iex> 1 < 2
226
-
true
227
-
{% endhighlight %}
184
+
iex> 1 == 1
185
+
true
186
+
iex> 1 != 2
187
+
true
188
+
iex> 1 < 2
189
+
true
228
190
229
191
The difference between `==` and `===` is that the latter is more strict when comparing integers and floats:
230
192
231
-
{% highlight iex %}
232
-
iex> 1 == 1.0
233
-
true
234
-
iex> 1 === 1.0
235
-
false
236
-
{% endhighlight %}
193
+
iex> 1 == 1.0
194
+
true
195
+
iex> 1 === 1.0
196
+
false
237
197
238
198
In Elixir, we can compare two different data types:
239
199
240
-
{% highlight iex %}
241
-
iex> 1 < :atom
242
-
true
243
-
{% endhighlight %}
200
+
iex> 1 < :atom
201
+
true
244
202
245
203
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:
246
204
247
205
number < atom < reference < functions < port < pid < tuple < list < bit string
248
206
249
207
You actually don't need to memorize this ordering, it is important just to know an order exists.
250
208
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.
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.
0 commit comments