programming:elixir:hello_world
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
programming:elixir:hello_world [2023/04/25 07:09] – Small changes ateixeira | programming:elixir:hello_world [2023/04/26 13:26] – ateixeira | ||
---|---|---|---|
Line 3: | Line 3: | ||
<code elixir> | <code elixir> | ||
IO.puts(" | IO.puts(" | ||
+ | |||
+ | # Same as IO.puts but do not add a new line at the end | ||
+ | IO.write(" | ||
</ | </ | ||
Line 16: | Line 19: | ||
h | h | ||
</ | </ | ||
+ | |||
+ | > Kernel is Elixir' | ||
+ | |||
+ | > In Elixir, (almost) everything is a function | ||
---- | ---- | ||
- | > On elixir, functions do not modify values. Functions transforms values on output. | + | ==== Reading values ==== |
+ | <code elixir> | ||
+ | name = IO.gets(" | ||
- | Example : | + | IO.write(" |
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== Atom ==== | ||
+ | > An atom type represents a fixed constant. The atom value is simply its own name. | ||
<code elixir> | <code elixir> | ||
- | iex(40)> d1 = ~D[2023-04-25] | + | # All atoms are preceded with a : |
- | ~D[2023-04-25] | + | variable = :an_atom |
</ | </ | ||
+ | |||
+ | ==== Cond === | ||
+ | > Cond follows the first path that evaluates to true. If no path evaluates to true, an error is raised by the runtime. | ||
<code elixir> | <code elixir> | ||
- | iex(41)> Date.add(d1, | + | cond do |
- | ~D[2023-04-30] | + | x > 10 -> : |
+ | y < 7 -> : | ||
+ | true -> : | ||
+ | end | ||
</ | </ | ||
+ | |||
+ | ==== _ (Underscore) ==== | ||
+ | > When there is a _ instead of a variable or a value, it means the value will be ignored and any value will be accepted | ||
+ | |||
+ | <code elixir> | ||
+ | {_, denominator} = Float.ratio(0.25) | ||
+ | # => {1, 4} | ||
+ | # the variable `denominator` is bounded to the value 4 | ||
+ | </ | ||
+ | <code elixir> | ||
+ | [1, _, 3] = [1, " | ||
+ | # => [1, " | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== For ==== | ||
+ | <code elixir> | ||
+ | number = IO.gets(" | ||
+ | |||
+ | for i <- 1..number do | ||
+ | IO.puts("# | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | > In elixir, values are not modified. Values are transformed on output. | ||
+ | |||
+ | **Example** : | ||
+ | |||
+ | <code elixir> | ||
+ | name = " | ||
+ | |||
+ | if (name == " | ||
+ | IO.puts(" | ||
+ | name = " | ||
+ | IO.puts(" | ||
+ | end | ||
+ | |||
+ | IO.puts(" | ||
+ | </ | ||
+ | |||
+ | **Output** : | ||
+ | |||
+ | <code elixir> | ||
+ | Inside condition before name being changed: Toto | ||
+ | Inside condition after name being changed: Robin | ||
+ | Outside condition after name being changed: Toto | ||
+ | </ | ||
+ | |||
+ | **Example with date** : | ||
+ | |||
+ | <code elixir> | ||
+ | d1 = ~D[2023-04-25] | ||
+ | |||
+ | IO.puts(d1) | ||
+ | IO.puts(Date.add(d1, | ||
+ | IO.puts(d1) | ||
+ | </ | ||
+ | |||
+ | **Output** : | ||
<code elixir> | <code elixir> | ||
- | iex(42)> d1 | + | 2023-04-25 |
- | ~D[2023-04-25] | + | 2023-04-30 |
+ | 2023-04-25 | ||
</ | </ | ||
programming/elixir/hello_world.txt · Last modified: 2023/05/02 14:52 by ateixeira