Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created February 8, 2013 23:49
Show Gist options
  • Save iaintshine/985fab232ed6abf89c2c to your computer and use it in GitHub Desktop.
Save iaintshine/985fab232ed6abf89c2c to your computer and use it in GitHub Desktop.
Comparison between C style for loop and Erlang on a sum function example
//
// C - style
//
int sum( int boundary )
{
int i, sum = 0;
for( i = 1; i <= boundary; i++ )
sum += 1;
return sum;
}
%
% Erlang
%
sum( Boundary ) -> sum_acc( 1, Boundary, 0 ).
sum_acc(Index, Boundary, Sum) when Index =< Boundary ->
sum_acc( Index + 1, Boundary, Sum + Index );
sum_acc( _I, _B, Sum + Index ) ->
Sum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment