programming:elixir:hello_world
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programming:elixir:hello_world [2023/04/25 07:37] – Adding examples ateixeira | programming:elixir:hello_world [2023/05/02 14:52] (current) – 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 15: | Line 18: | ||
<code elixir> | <code elixir> | ||
h | h | ||
+ | </ | ||
+ | |||
+ | > Kernel is Elixir' | ||
+ | |||
+ | > In Elixir, (almost) everything is a function | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== Reading values ==== | ||
+ | <code elixir> | ||
+ | name = IO.gets(" | ||
+ | |||
+ | IO.write(" | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== Atom ==== | ||
+ | > An atom type represents a fixed constant. The atom value is simply its own name. | ||
+ | |||
+ | <code elixir> | ||
+ | # All atoms are preceded with a : | ||
+ | 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> | ||
+ | cond do | ||
+ | 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, " | ||
+ | </ | ||
+ | |||
+ | ==== Sigils ( symbols with magical powers ) ==== | ||
+ | |||
+ | >A sigil is an alternative syntax for representing and working with literals | ||
+ | |||
+ | >A sigil starts with a tilde (~), followed by an upper- or lowercase letter, some delimited content, and perhaps some options | ||
+ | |||
+ | > The delimiters can be < | ||
+ | |||
+ | <code elixir> | ||
+ | The letter determines the sigil’s type: | ||
+ | |||
+ | ~C A character list with no escaping or interpolation | ||
+ | ~c A character list, escaped and interpolated just like a single-quoted string | ||
+ | ~D A Date in the format yyyy-mm-dd | ||
+ | ~N A naive (raw) DateTime in the format yyyy-mm-dd hh: | ||
+ | ~R A regular expression with no escaping or interpolation | ||
+ | ~r A regular expression, escaped and interpolated | ||
+ | ~S A string with no escaping or interpolation | ||
+ | ~s A string, escaped and interpolated just like a double-quoted string | ||
+ | ~T A Time in the format hh: | ||
+ | ~W A list of whitespace-delimited words, with no escaping or interpolation | ||
+ | ~w A list of whitespace-delimited words, with escaping and interpolation | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ==== For ==== | ||
+ | <code elixir> | ||
+ | number = IO.gets(" | ||
+ | |||
+ | for i <- 1..number do | ||
+ | IO.puts("# | ||
+ | end | ||
</ | </ | ||
---- | ---- | ||
- | > On elixir, | + | > In elixir, |
- | Example : | + | **Example** : |
<code elixir> | <code elixir> | ||
Line 27: | Line 112: | ||
if (name == " | if (name == " | ||
- | IO.puts(" | + | IO.puts(" |
name = " | name = " | ||
- | IO.puts(" | + | IO.puts(" |
end | end | ||
- | IO.puts(" | + | IO.puts(" |
</ | </ | ||
- | Output : | + | **Output** : |
<code elixir> | <code elixir> | ||
- | Inside condition before being changed: Toto | + | Inside condition before |
- | Inside condition after being changed: Robin | + | Inside condition after name being changed: Robin |
- | Outside condition after being changed: Toto | + | Outside condition after name being changed: Toto |
</ | </ | ||
- | Example with date : | + | **Example with date** : |
<code elixir> | <code elixir> | ||
Line 53: | Line 138: | ||
</ | </ | ||
- | Output : | + | **Output** : |
<code elixir> | <code elixir> |
programming/elixir/hello_world.1682408237.txt.gz · Last modified: 2023/04/25 07:37 by ateixeira