title: Recursive macro Elixir
body: <p>I am playing around with Elixir macros - specifically macros that call themselves which is something that I do often in Scheme. I have created a little test macro below however it just hangs iex - nothing is printed to console. Does anyone have any insight into why and what could be done to correct it?</p>
  
  <pre class="lang-rb prettyprint-override"><code>defmodule RecMac do
    defmacro test_rec(x) do
      quote do
        IO.puts("Started?")
        if(unquote(x) &lt; 1) do
          IO.puts("Done?")
          "done"
        else
          IO.puts("Where are we")
          IO.puts(unquote(x))
          RecMac.test_rec(unquote(x) - 1)
        end
      end
    end
  end
  </code></pre>
  
  <p>EDIT!!</p>
  
  <p>OK, so it turns out you can define recursive macros where there is a structural difference to match on (eg lists). The following is working for me. And to confirm @Aleksei Matiushkin below, the above will not work and does indeed not work in scheme!</p>
  
  <pre><code>  defmacro test_rec([h | t]) do
      quote do
        IO.inspect([unquote(h) | unquote(t)])
        RecMac.test_rec(unquote(t))
      end
    end
  
    defmacro test_rec([]) do
      quote do
        IO.puts "Done"
      end
    end
  end
  </code></pre>
  
  <p>I am very happy to have dug into this as I learned something about two languages!</p>