Going through different blogs reading about elixir and solving problems in general, I landed on this page. The problem seemed quite easy and I had already solved the same problem in other programming language before. I want to start learning elixir by solving this problem in elixir.
The task is to take integer
n and print all number from
1 to n in different lines. If n is divisible by 3, then instead of printing
Foo needs to be printed,
Bar if divisible by 5 and
Foobar if divisible by 15.
for n <- 1..15 do cond do rem(n, 15) == 0 -> IO.puts "Foobar" rem(n, 3) == 0 -> IO.puts "Foo" rem(n, 5) == 0 -> IO.puts "Bar" true -> IO.puts n end end
In most language, when required to check multiple conditions
if...else if/elsif...else...end would be used, however elixir provides
condfor such purpose. We can match any number of condition with
cond and check the final condition with
true -> which is equivalent to
If we only need to check only one condition, we use
iex(1)> if rem(10,2) == 0 do ...(1)> IO.puts "Even" ...(1)> else ...(1)> IO.puts "Odd" ...(1)> end Even :ok
You might also like...
Fixing mysql2 install error
Challenge for the 2017
How to install Ruby on Rails in Linux ?
Serving Content through Cloudflare network
One liner server command in ruby
Free website using jekyll and free github hosting
How to install Jekyll ?
How to register for GithubEducation Pack ?
How to install Ruby on Linux
Ubuntu system program problem detected[Solved]