Tail Recursion

Ponder this:

fact (0) ->
	1;
fact (N) ->
	N * fact (N - 1).

versus this:

fact(N) ->
	fact_helper(N, 1).
fact_helper(1, T) ->
	T;
fact_helper(N, T) ->
	fact_helper(N - 1, T * N).

The advantage of learning Erlang (albeit very slowly, with lots of interruptions) is that it directly introduces a lot of concepts I’ve been marginally aware of before. For instance, the second example implements factorial using tail recursion. The advantage is that a compiler doesn’t have to implement a call-stack when playing with arguments. See wikipedia entry.

One response to “Tail Recursion”

  1. How long does 1mg of xanax stay in your system….

    Xanax bars. Buy xanax without prescription in usa. Xanax. Generic xanax….

Leave a Reply

Create a website or blog at WordPress.com

%d bloggers like this: