User Tools

Site Tools


programming:elixir:hello_world

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:elixir:hello_world [2023/04/25 07:09] – Small changes ateixeiraprogramming:elixir:hello_world [2023/05/02 14:52] (current) ateixeira
Line 3: Line 3:
 <code elixir> <code elixir>
 IO.puts("Hello world!") IO.puts("Hello world!")
 +
 +# Same as IO.puts but do not add a new line at the end
 +IO.write("Hello world !")
 </code> </code>
  
Line 16: Line 19:
 h h
 </code> </code>
 +
 +> Kernel is Elixir's default environment
 +
 +> In Elixir, (almost) everything is a function
  
 ---- ----
  
-On elixirfunctions do not modify values. Functions transforms values on output.+==== Reading values ==== 
 +<code elixir> 
 +name = IO.gets("Salutcomment tu t'appelles ? ")
  
-Example :+IO.write("Salut " <> name) 
 +</code> 
 + 
 +---- 
 + 
 +==== 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
 </code> </code>
 +
 +==== 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,5) +cond do 
-~D[2023-04-30]+    x 10 -> :this_might_be_the_way 
 +    y < 7 -> :or_that_might_be_the_way 
 +    true -> :this_is_the_default_way 
 +end
 </code> </code>
 +
 +==== _ (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>
 +<code elixir>
 +[1, _, 3] = [1, "toto", 3]
 +# => [1, "toto", 3]
 +</code>
 +
 +==== 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 <...>, {...}, [...], (...), |...|, /.../, "...", and '...'
 +
 +<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:mm:ss[.ddd]
 +~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:mm:ss[.dddd]
 +~W  A list of whitespace-delimited words, with no escaping or interpolation
 +~w  A list of whitespace-delimited words, with escaping and interpolation
 +</code>
 +
 +----
 +
 +==== For ====
 +<code elixir>
 +number = IO.gets("Salut, entre un nombre : ") |> String.trim() |> String.to_integer()
 +
 +for i <- 1..number do
 +  IO.puts("#{i}")
 +end
 +</code>
 +
 +----
 +
 +> In elixir, values are not modified. Values are transformed on output.
 +
 +**Example** :
 +
 +<code elixir>
 +name = "Toto"
 +
 +if (name == "Toto") do
 +  IO.puts("Inside condition before name being changed: " <> name)
 +  name = "Robin"
 +  IO.puts("Inside condition after name being changed: " <> name)
 +end
 +
 +IO.puts("Outside condition after name being changed: " <> name)
 +</code>
 +
 +**Output** : 
 +
 +<code elixir>
 +Inside condition before name being changed: Toto
 +Inside condition after name being changed: Robin
 +Outside condition after name being changed: Toto
 +</code>
 +
 +**Example with date** : 
 +
 +<code elixir>
 +d1 = ~D[2023-04-25]
 +
 +IO.puts(d1)
 +IO.puts(Date.add(d1, 5))
 +IO.puts(d1)
 +</code>
 +
 +**Output** : 
  
 <code elixir> <code elixir>
-iex(42)> d1 +2023-04-25 
-~D[2023-04-25]+2023-04-30 
 +2023-04-25
 </code> </code>
  
  
  
programming/elixir/hello_world.1682406588.txt.gz · Last modified: 2023/04/25 07:09 by ateixeira