|
| 1 | +# Chapter 2: Variables |
| 2 | + |
| 3 | +```elixir |
| 4 | +Mix.install([ |
| 5 | + {:kino, "~> 0.12.0"} |
| 6 | +]) |
| 7 | + |
| 8 | +import IEx.Helpers |
| 9 | +``` |
| 10 | + |
| 11 | +## Values and types |
| 12 | + |
| 13 | +A value is one of the basic things a program works with, like a letter or a number. The values we have seen so far are 1, 2, and “Hello, World!” |
| 14 | + |
| 15 | +These values belong to different types: 2 is an integer, and “Hello, World!” is a string, so called because it contains a “string” of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks. |
| 16 | + |
| 17 | +``` |
| 18 | +Note: While Elixir doesn't have a specific "string" type, binaries can efficiently store |
| 19 | +and manipulate text data. They offer additional functionalities compared to traditional |
| 20 | +strings in other languages. |
| 21 | +``` |
| 22 | + |
| 23 | +##### Printing in Elixir |
| 24 | + |
| 25 | +If you want to play in your local box, use `iex` commnad to start the Elixir REPL |
| 26 | + |
| 27 | +```cmd |
| 28 | +$>iex |
| 29 | +Erlang/OTP 26 [erts-14.2.5] [source] [64-bit] [smp:20:20] [ds:20:20:10] [async-threads:1] [jit:ns] |
| 30 | +
|
| 31 | +Interactive Elixir (1.16.2) - press Ctrl+C to exit (type h() ENTER for help) |
| 32 | +iex(1)> |
| 33 | +``` |
| 34 | + |
| 35 | +Unlike some languages, Elixir doesn't have a built-in print statement. However, you can achieve the same functionality using the IO (Input/Output) module. |
| 36 | + |
| 37 | +<!-- livebook:{"force_markdown":true} --> |
| 38 | + |
| 39 | +```elixir |
| 40 | +iex(1)> IO.puts(4) |
| 41 | +4 |
| 42 | +:ok |
| 43 | +``` |
| 44 | + |
| 45 | +You can try it here |
| 46 | + |
| 47 | +```elixir |
| 48 | +IO.puts(4) |
| 49 | +``` |
| 50 | + |
| 51 | +If you are not sure what type a value has, you can use `i` helper in IEx |
| 52 | + |
| 53 | +* i/0 - prints information about the last value |
| 54 | +* i/1 - prints information about the given term |
| 55 | + |
| 56 | +i/n Indicates the arity, in this case we can call `i` so it will show the information of the last value or we can call `i term` so it will show the information about `term`. |
| 57 | + |
| 58 | +<!-- livebook:{"force_markdown":true} --> |
| 59 | + |
| 60 | +```elixir |
| 61 | +iex(2)> a = 1 |
| 62 | +1 |
| 63 | +iex(3)> i |
| 64 | +Term |
| 65 | + 1 |
| 66 | +Data type |
| 67 | + Integer |
| 68 | +Reference modules |
| 69 | + Integer |
| 70 | +Implemented protocols |
| 71 | + IEx.Info, Inspect, List.Chars, String.Chars |
| 72 | +iex(4)> str = "Hello World" |
| 73 | +"Hello World" |
| 74 | +iex(5)> i |
| 75 | +Term |
| 76 | + "Hello World" |
| 77 | +Data type |
| 78 | + BitString |
| 79 | +Byte size |
| 80 | + 11 |
| 81 | +Description |
| 82 | + This is a string: a UTF-8 encoded binary. It's printed surrounded by |
| 83 | + "double quotes" because all UTF-8 encoded code points in it are printable. |
| 84 | +Raw representation |
| 85 | + <<72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100>> |
| 86 | +Reference modules |
| 87 | + String, :binary |
| 88 | +Implemented protocols |
| 89 | + Collectable, IEx.Info, Inspect, List.Chars, String.Chars |
| 90 | +``` |
| 91 | +
|
| 92 | +You can try it here |
| 93 | +
|
| 94 | +```elixir |
| 95 | +a = 1 |
| 96 | +i(a) |
| 97 | +``` |
| 98 | +
|
| 99 | +```elixir |
| 100 | +str = "Hello World" |
| 101 | +i(str) |
| 102 | +``` |
| 103 | +
|
| 104 | +Strings belong to the type BitString and integers belong to the type Integer. Less obviously, numbers with a decimal point belong to a type called Float, because these numbers are represented in a format called floating point. |
| 105 | +
|
| 106 | +<!-- livebook:{"force_markdown":true} --> |
| 107 | +
|
| 108 | +```elixir |
| 109 | +iex(1)> i 3.2 |
| 110 | +Term |
| 111 | + 3.2 |
| 112 | +Data type |
| 113 | + Float |
| 114 | +Reference modules |
| 115 | + Float |
| 116 | +Implemented protocols |
| 117 | + IEx.Info, Inspect, List.Chars, String.Chars |
| 118 | +``` |
| 119 | +
|
| 120 | +```elixir |
| 121 | +i(3.2) |
| 122 | +``` |
| 123 | +
|
| 124 | +What about values like “17” and “3.2”? They look like numbers, but they are in quotation marks like strings. |
| 125 | +
|
| 126 | +```elixir |
| 127 | +i("17") |
| 128 | +i("3.2") |
| 129 | +``` |
| 130 | +
|
| 131 | +They’re strings. When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000. This is not a legal integer in Elixir |
| 132 | +
|
| 133 | +<!-- livebook:{"force_markdown":true} --> |
| 134 | +
|
| 135 | +```elixir |
| 136 | +iex(8)> i = 1,000,000 |
| 137 | +** (SyntaxError) invalid syntax found on iex:8:6: |
| 138 | + error: syntax error before: ',' |
| 139 | + │ |
| 140 | + 8 │ i = 1,000,000 |
| 141 | + │ ^ |
| 142 | + │ |
| 143 | + └─ iex:8:6 |
| 144 | + (iex 1.16.2) lib/iex/evaluator.ex:295: IEx.Evaluator.parse_eval_inspect/4 |
| 145 | + (iex 1.16.2) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1 |
| 146 | + (iex 1.16.2) lib/iex/evaluator.ex:32: IEx.Evaluator.init/5 |
| 147 | + (stdlib 5.2.3) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 |
| 148 | +``` |
| 149 | +
|
| 150 | +But you can write |
| 151 | +
|
| 152 | +<!-- livebook:{"force_markdown":true} --> |
| 153 | +
|
| 154 | +```elixir |
| 155 | + iex(8)> i = 1_000_000 |
| 156 | + 1000000 |
| 157 | +``` |
| 158 | +
|
| 159 | +## Variables |
| 160 | +
|
| 161 | +Elixir is a functional programming language that encourages immutability and pattern matching. In Elixir, the `=` symbol is not used for assignment. Instead, we use pattern matching to bind values to names (variables). |
| 162 | +
|
| 163 | +Examples of Binding Values to Variables in Elixir: |
| 164 | +
|
| 165 | +<!-- livebook:{"force_markdown":true} --> |
| 166 | +
|
| 167 | +```elixir |
| 168 | + message = "And now for something completely different" |
| 169 | + n = 17 |
| 170 | + pi = 3.1415926535897931 # Note: This is an approximation of pi |
| 171 | +``` |
| 172 | +
|
| 173 | +This example creates three bindings. |
| 174 | +
|
| 175 | +* The first binds a string to a new variable named `message` |
| 176 | +* The second binds the integer `17` to `n` |
| 177 | +* The third binds the (approximate) value of `π` to `pi`. |
| 178 | +
|
| 179 | +To display the value of a variable, you can use a `IO.puts/1` function: |
| 180 | +
|
| 181 | +<!-- livebook:{"force_markdown":true} --> |
| 182 | +
|
| 183 | +```elixir |
| 184 | +iex(6)> IO.puts n |
| 185 | +17 |
| 186 | +:ok |
| 187 | +iex(7)> IO.puts message |
| 188 | +And now for something completely different |
| 189 | +:ok |
| 190 | +``` |
| 191 | +
|
| 192 | +#### Playtime |
| 193 | +
|
| 194 | +```elixir |
| 195 | +# Bind a value to a variable |
| 196 | +# Display the value |
| 197 | +# Displat the info of the value |
| 198 | +``` |
| 199 | +
|
| 200 | +## Variable names and keywords |
| 201 | +
|
| 202 | +Programmers generally choose names for their variables that are meaningful and document what the variable is used for. |
| 203 | +
|
| 204 | +Variable Names |
| 205 | +
|
| 206 | +* Can start with lowercase letters or underscores(e.g., `user_name` `_ignored_value`). |
| 207 | +* Can start with an underscore character, but it's onlyused to indicate that the value of the variable should be ignored. |
| 208 | +* Can be arbitrarily long. |
| 209 | +* Can contain letters, numbers and underscores. |
| 210 | +* The underscore character `_` can appear in a name. It is often used in names with multiple words, such as `my_name` or `airspeed_of_unladen_swallow`. |
| 211 | +* Cannot start with a number or upper case. |
| 212 | + |
| 213 | +<!-- livebook:{"force_markdown":true} --> |
| 214 | + |
| 215 | +```elixir |
| 216 | + 76trombones = "big parade" |
| 217 | +** (SyntaxError) invalid syntax found on iex:7:1: |
| 218 | + error: invalid character "t" after number 76. If you intended to write a number, make sure to separate the number from the character (using comma, space, etc). If you meant to write a function name or a variable, note that identifiers in Elixir cannot start with numbers. Unexpected token: t |
| 219 | + │ |
| 220 | + 7 │ 76trombones = "big parade" |
| 221 | + │ ^ |
| 222 | + │ |
| 223 | + └─ iex:7:1 |
| 224 | + (iex 1.16.2) lib/iex/evaluator.ex:295: IEx.Evaluator.parse_eval_inspect/4 |
| 225 | + (iex 1.16.2) lib/iex/evaluator.ex:187: IEx.Evaluator.loop/1 |
| 226 | + (iex 1.16.2) lib/iex/evaluator.ex:32: IEx.Evaluator.init/5 |
| 227 | + (stdlib 5.2.3) proc_lib.erl:241: :proc_lib.init_p_do_apply/3 |
| 228 | +``` |
| 229 | + |
| 230 | +## Statements |
| 231 | + |
| 232 | +## Operators and operands |
| 233 | + |
| 234 | +## Expressions |
| 235 | + |
| 236 | +## Order of operations |
| 237 | + |
| 238 | +## Modulus operator |
| 239 | + |
| 240 | +## String operations |
| 241 | + |
| 242 | +## Asking the user for input |
| 243 | + |
| 244 | +## Comments |
| 245 | + |
| 246 | +## Choosing mnemonic variable names |
| 247 | + |
| 248 | +## Debugging |
| 249 | + |
| 250 | +## Glossary |
| 251 | + |
| 252 | +## Exercises |
0 commit comments