Skip to content

Instantly share code, notes, and snippets.

@ngnguyen1
Last active June 28, 2016 10:58
Show Gist options
  • Save ngnguyen1/abbd4cd3626cfe8b0797bdb87efb3b89 to your computer and use it in GitHub Desktop.
Save ngnguyen1/abbd4cd3626cfe8b0797bdb87efb3b89 to your computer and use it in GitHub Desktop.
%% This is linear recursion
plus(X,0) -> X;
plus(X,Y) -> 1 + plus(X,Y-1).
%% This is tail recursion
tail_plus(X,Y) -> tail_plus(X,Y,X).
tail_plus(_,0,Acc) -> Acc;
tail_plus(X,Y,Acc) ->
tail_plus(X,Y-1,1+Acc).
%% Modified by Mr.Thang =))
tail_plus(X,0) -> X;
tail_plus(X,Y) ->
tail_plus(1+X,Y-1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment