Tail Recursion

Saturday, September 1, 2007

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.

Tags:

| Permalink

1 Comment »

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

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

    Trackback by Xanax cocktail. — Saturday, 10 May 2008 @ 00:38:43

RSS feed for comments on this post. TrackBack URI

Leave a comment