Best way to simulate a for loop in Elixir?

Viewed 17723

Common imperative programming languages provide a loop structure for running a block of code many times? e.g. for loop: pseudo code:

for(i=0;i<256;++i){
  expression one;
  statement one;
  many code goes here
}

The large block of code in the for loop will be executed many times. But as a functional programming langage, Elixir provide no loop. I know Enum module provides many functions for iterating code many times. e.g.

Enum.each(0..99, fn(x) -> IO.puts "hello, world!" end)

The code above will be executed 100 times. Print 100 "hello, world!" But it's just a one-line statement. How to execute a large block of code n times in Elixir? What is the right Elixir way to do so? I'm fresh new to Elixir. Could you guys give me a small sample code for learning? Thank all of you guys so much!

4 Answers
Related