Skip to content

Commit dcfaf55

Browse files
committed
Updated Chapter 4
First iteration on Built-in functions, Type conversion and Math functions sections.
1 parent 1d54f46 commit dcfaf55

1 file changed

Lines changed: 182 additions & 1 deletion

File tree

chapters/chapter_4.livemd

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import IEx.Helpers
1010

1111
## Function calls
1212

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:
1414

1515
<!-- livebook:{"force_markdown":true} -->
1616

@@ -25,10 +25,191 @@ It is common to say that a function "takes" an argument and "returns" a result.
2525

2626
## Built-in functions
2727

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+
2854
## Type conversion functions
2955

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
67+
68+
erlang.erl:4719: :erlang.binary_to_integer("Hello")
69+
iex:11: (file)
70+
iex(12)> String.to_integer("3.99")
71+
** (ArgumentError) errors were found at the given arguments:
72+
73+
* 1st argument: not a textual representation of an integer
74+
75+
erlang.erl:4719: :erlang.binary_to_integer("3.99")
76+
iex:12: (file)
77+
78+
```
79+
80+
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 in Float.to_string/1
121+
122+
The following arguments were given to Float.to_string/1:
123+
124+
# 1
125+
3
126+
127+
Attempted function clauses (showing 1 out of 1):
128+
129+
def to_string(float) when is_float(float)
130+
131+
(elixir 1.16.2) lib/float.ex:624: Float.to_string/1
132+
iex:4: (file)
133+
134+
```
135+
136+
**Exercise 1**: Using IEx or here in the following Elixir cell explore the the [Elixir Standard Library](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+
30147
## Math functions
31148

149+
In Elixir, the `:math` module offers various mathematical functions that you can use in your programs. This module is part of the Erlang standard library. Elixir has seamless interoperability with Erlang libraries, and it encourages direct interaction with Erlang code. Keep in mind that Erlang conventions differ slightly: variables start with an uppercase letter, and atoms (simple lowercase names) are used. So, to access the Erlang `math` module in Elixir, you simply write `:math`.
150+
151+
When using the `i` helper in the IEx (Interactive Elixir) 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+
**Exercise 2** : Try to use `i (:math)` and `h :math` to access its documentation.
154+
155+
To use 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. For example:
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)
206+
207+
```elixir
208+
i(:math)
209+
210+
h(:math)
211+
```
212+
32213
## Random numbers
33214

34215
## Adding new functions

0 commit comments

Comments
 (0)