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
Copy file name to clipboardExpand all lines: chapters/chapter_4.livemd
+182-1Lines changed: 182 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ import IEx.Helpers
10
10
11
11
## Function calls
12
12
13
-
In the context of programming, a function is a named sequence of expressions that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can invoke the function by name. We have already seen one example of a function invocation:
13
+
In the context of programming, a function is a named sequence of expressions that performs a computation. When you define a function, you specify the name and the sequence of expressions. Later, you can invoke the function by name. We have already seen one example of a function invocation:
14
14
15
15
<!-- livebook:{"force_markdown":true} -->
16
16
@@ -25,10 +25,191 @@ It is common to say that a function "takes" an argument and "returns" a result.
25
25
26
26
## Built-in functions
27
27
28
+
In Elixir, there are also built-in functions that can be used without providing a function definition. These functions are part of the [Elixir standard library](https://hexdocs.pm/elixir/Kernel.html#content) and are designed to solve common problems.
29
+
30
+
The `List.first/1` and `List.last/1` functions give us the first and last value in a list, respectively:
31
+
32
+
<!-- livebook:{"force_markdown":true} -->
33
+
34
+
```elixir
35
+
iex(1)>List.first([1, 2, 3, 4, 5])
36
+
1
37
+
iex(2)>List.last([1, 2, 3, 4, 5])
38
+
5
39
+
```
40
+
41
+
The `List.first/1` function tells us the "first element" in the List (which turn out to be the integer "1") and the `List.last/1` shows the "last element" in the List (which turn out to be the integer "2")
42
+
43
+
Another common built-in function is the `String.length/1` function, which can be used to find the length of a string:
44
+
45
+
<!-- livebook:{"force_markdown":true} -->
46
+
47
+
```elixir
48
+
iex(5)>String.length("Hello world")
49
+
11
50
+
```
51
+
52
+
The `String.length/1` function returns the number of characters in the string (which is 11 in this case).
53
+
28
54
## Type conversion functions
29
55
56
+
Elixir also provides built-in functions that convert values from one type to another. The `String.to_integer/1` function takes string representation and converts it to an integer, if it can, or complains otherwise:
57
+
58
+
<!-- livebook:{"force_markdown":true} -->
59
+
60
+
```elixir
61
+
iex(10)>String.to_integer("32")
62
+
32
63
+
iex(11)>String.to_integer("Hello")
64
+
** (ArgumentError) errors were found at the given arguments:
65
+
66
+
* 1st argument:not a textual representation of an integer
The `String.to_float/1` function takes string representation and converts it to an integer, if it can, or complains otherwise:
81
+
82
+
<!-- livebook:{"force_markdown":true} -->
83
+
84
+
```elixir
85
+
iex(1)>String.to_float("3.999")
86
+
3.999
87
+
iex(2)>String.to_float("Hello")
88
+
** (ArgumentError) errors were found at the given arguments:
89
+
90
+
* 1st argument:not a textual representation of a float
91
+
92
+
:erlang.binary_to_float("Hello")
93
+
iex:2: (file)
94
+
iex(2)>String.to_float("3")
95
+
** (ArgumentError) errors were found at the given arguments:
96
+
97
+
* 1st argument:not a textual representation of a float
98
+
99
+
:erlang.binary_to_float("3")
100
+
iex:2: (file)
101
+
```
102
+
103
+
Finally `Integer.to_string/1` takes an `integer` as its argument and convert to a String, in the same way `Float.to_string/1` takes a `float` as its argument and convert to a String. In both cases if it can't it will complains.
104
+
105
+
<!-- livebook:{"force_markdown":true} -->
106
+
107
+
```elixir
108
+
iex(2)>Integer.to_string(3)
109
+
"3"
110
+
iex(3)>Integer.to_string(3.3)
111
+
** (ArgumentError) errors were found at the given arguments:
112
+
113
+
* 1st argument:not an integer
114
+
115
+
:erlang.integer_to_binary(3.3)
116
+
iex:3: (file)
117
+
iex(3)>Float.to_string(3.3)
118
+
"3.3"
119
+
iex(4)>Float.to_string(3)
120
+
** (FunctionClauseError) no function clause matching inFloat.to_string/1
121
+
122
+
The following arguments were given to Float.to_string/1:
**Exercise1**: UsingIExor here in the following Elixir cell explore the the [ElixirStandardLibrary](https://hexdocs.pm/elixir/1.17.0/Kernel.html).
137
+
138
+
* `exports Kernel`
139
+
* `exports String`
140
+
* `exports String`
141
+
142
+
```elixir
143
+
exports(Kernel)
144
+
h(String.to_integer())
145
+
```
146
+
30
147
## Math functions
31
148
149
+
InElixir, the `:math` module offers various mathematical functions that you can usein your programs. This module is part of the Erlang standard library. Elixir has seamless interoperability withErlang libraries, and it encourages direct interaction withErlang code. Keepin mind that Erlang conventions differ slightly: variables start with an uppercase letter, andatoms (simple lowercase names) are used. So, to access the Erlang `math` module inElixir, you simply write `:math`.
150
+
151
+
When using the `i` helper in the IEx (InteractiveElixir) shell, you can retrieve detailed information about an Erlang module. It provides a concise summary of the module, including its data type, bytecode location, source file, version, compile options, and implemented protocols.
152
+
153
+
**Exercise2** : Try to use `i (:math)` and `h :math` to access its documentation.
154
+
155
+
Touse a function from the `:math` module, you can use the dot notation to specify the name of the module and the name of the function, separated by a dot. Forexample:
156
+
157
+
<!--livebook:{"force_markdown":true} -->
158
+
159
+
```elixir
160
+
iex> ratio =10.0/1.0
161
+
10.0
162
+
iex> decibels =10*:math.log10(ratio)
163
+
10.0
164
+
```
165
+
166
+
The first example computes the logarithm base 10 of the signal-to-noise ratio using the `:math.log10/1` function. The `:math` module also provides a function called `:math.log/1` that computes logarithms base e.
167
+
168
+
The second example finds the sine of a value in radians using the `:math.sin/1` function:
169
+
170
+
<!--livebook:{"force_markdown":true} -->
171
+
172
+
```elixir
173
+
iex> radians =0.7
174
+
0.7
175
+
iex> height =:math.sin(radians)
176
+
0.644217687237691
177
+
```
178
+
179
+
The `:math.sin/1` function, as well as the other trigonometric functions (`:math.cos/1`, `:math.tan/1`, etc.), take arguments in radians. To convert from degrees to radians, you can use the following formula:
180
+
181
+
<!--livebook:{"force_markdown":true} -->
182
+
183
+
```elixir
184
+
iex> degrees =45
185
+
45
186
+
iex> radians = degrees *:math.pi/180
187
+
0.7853981633974483
188
+
iex>:math.sin(radians)
189
+
0.7071067811865476
190
+
```
191
+
192
+
The expression `:math.pi` gets the value of π from the `:math` module. The value of this variable is an approximation of π, accurate to about 15 digits.
193
+
194
+
If you know your trigonometry, you can check the previous result by comparing it to the square root of two divided by two:
195
+
196
+
<!--livebook:{"force_markdown":true} -->
197
+
198
+
```elixir
199
+
iex>:math.sqrt(2) /2.0
200
+
0.7071067811865476
201
+
```
202
+
203
+
It is a good practice to familiarize yourself with the functions available in the `:math` module and to use them in your programs when needed. This can save you time and help you avoid errors in your mathematical
204
+
205
+
To learn more about [math module](https://hexdocs.pm/elixir/main/erlang-libraries.html#the-math-module)
0 commit comments