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
Last revisionBoth sides next revision
programming:elixir:hello_world [2023/04/25 13:20] ateixeiraprogramming:elixir:hello_world [2023/04/26 13:26] ateixeira
Line 19: Line 19:
 h h
 </code> </code>
 +
 +> Kernel is Elixir's default environment
 +
 +> In Elixir, (almost) everything is a function
  
 ---- ----
Line 28: Line 32:
 IO.write("Salut " <> name) IO.write("Salut " <> name)
 </code> </code>
 +
 +----
 +
 +==== 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
 +</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>
 +cond do
 +    x > 10 -> :this_might_be_the_way
 +    y < 7 -> :or_that_might_be_the_way
 +    true -> :this_is_the_default_way
 +end
 +</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>
 +
 +----
 +
 +==== 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. > In elixir, values are not modified. Values are transformed on output.
  
-Example :+**Example** :
  
 <code elixir> <code elixir>
Line 38: Line 88:
  
 if (name == "Toto") do if (name == "Toto") do
-  IO.puts("Inside condition before being changed: " <> name)+  IO.puts("Inside condition before name being changed: " <> name)
   name = "Robin"   name = "Robin"
-  IO.puts("Inside condition after being changed: " <> name)+  IO.puts("Inside condition after name being changed: " <> name)
 end end
  
-IO.puts("Outside condition after being changed: " <> name)+IO.puts("Outside condition after name being changed: " <> name)
 </code> </code>
  
-Output : +**Output** 
  
 <code elixir> <code elixir>
-Inside condition before being changed: Toto +Inside condition before name being changed: Toto 
-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
 </code> </code>
  
-Example with date : +**Example with date** 
  
 <code elixir> <code elixir>
Line 64: Line 114:
 </code> </code>
  
-Output : +**Output** 
  
 <code elixir> <code elixir>
programming/elixir/hello_world.txt · Last modified: 2023/05/02 14:52 by ateixeira