Created
February 8, 2013 23:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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