1

I have a simple Go for loop and i want to learn how to do this with Recursion in Elixir. I don't really know how the recursion loop works at this time but i am working on that part! I also don't know if this is even the right way to do recursive functions.

package main

import "fmt"

func main() {
    for i := 0; i < 10000000; i++ {
        fmt.Println(i)
    }
}



defmodule Looping do
      def loops(i, e) when e <= 0 do
        IO.puts i + e
      end

      def loops(i, e) do
        IO.puts i + e
        loops(i, e + 1)
      end
    end 

    Looping.loops(1, 1) 

Produces 1 + 1 + 1 on going in Elixir.

Martin over at the beginners slack channel suggested the following.

 1..1_000_000 |> Enum.each(&IO.puts/1)
4
  • I don't know Go - what's that initial 10000000 line print loop for? Commented Feb 15, 2016 at 9:17
  • It's the number we're printing up to. So print i + 1 up to 10000000. Commented Feb 15, 2016 at 10:03
  • ok - now that's clear - see my code below. Which is basically a more verbose version of Martin's that you have added Commented Feb 15, 2016 at 10:57
  • While Martin's code certainly works in Elixir, the solutions that both GavinBrelstaff and SashaFonseca have laid out below are both the more generic way of doing a "for" loop with recursion. Commented Feb 15, 2016 at 13:00

4 Answers 4

3

Well ignoring the main stuff and running your elixir code in Iex I get an infinite ascending series that begins

1
2
3
4

which suggests you may have intended to stop when you arrive at 10000000 not at zero - thus tidying up your code we have:

defmodule Looping do
  def loops(e) when e == 10000000 do
    IO.puts e
  end

  def loops(e) do
    IO.puts e
    loops(e + 1)
  end
end 

Looping.loops(0)
Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what i was looking for and it should have been easier for me to get. Now i have seen it, it is understandable. Thanks
2

I'm not exactly sure what you're looking for, but simulating a for loop with recursion in Elixir would look something like this:

def my_func(param1, 5), do: param1
def my_func(param1, count) do 
  my_func param1 * 2, count + 1
end

and you would call the function like this to run the loop from 1 to 5:

my_func(input_for_param1, 1)

I think your problem is that you still don't understand what recursion is and how Elixir (and Functional Programming languages) work. Try to first grasp these concepts (including pattern matching) and everything will come easier to you.

Another way to do a for loop would be by creating a construct for it through metaprogramming.

2 Comments

I still don't quite understand how your version works, but i will work on figuring out how. Thanks
It's basic recursion in Functional languages with pattern matching. In this case you define the same function with two signatures. First you define when you want it to finish, which in the example is when the second parameter is 5 (and the result from that is return param1). In the second def you call recursively the function so that it doubles param1 and increments count. When count reaches 5 and the function is called again, then the first def will be triggered and since it doesn't call the function again it will stop and execute the instruction (return param1).
0

There are a lot of simple recursion examples found here: http://elixir-lang.org/getting-started/recursion.html That also explain recursion and going through lists. What are you specifically after? Take a look at those and let me know if you are having a hard time figuring it out.

2 Comments

Already read the docs for recursion, but i still found it hard to do a simple for loop with addition.
Do you want the sum of a list? There are already answers to that, one example is this one: stackoverflow.com/questions/24728416/…
0

In your case using a range & a simple comprehension would work very well, but if you wanted to use recursion specifically you could do something like

defmodule SomeName do
    def addr(range), do: addr(Enum.to_list(range), 0)
    def addr([first|rest], sum), do: addr(rest, sum + first)
    def addr([], sum), do: sum
end

if you wanted to print each line you could throw an IO.inspect/1 in addr/2 to print out the sum like

def addr([first|rest], sum) do
  IO.inspect(sum)
  addr(rest, sum + first)
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.