Tail recursion (last call optimization).


%%% Book example (procedure):
declare
proc {Loop10 I}
   if I == 10 then skip
   else
      {Browse I}
      {Loop10 I + 1}
   end
end

{Loop10 0}

%%% Finding the sum of list elements
declare
fun {Sum L}
   case L of H|T then H+ {Sum T} else 0 end 
end

%{Browse {Sum [2 3 4 5 1]}}

%%% Finding the sum of list elements, tail-recursive
declare
fun {SumTR L Res}
   case L of H|T then {SumTR T (Res + H)} else Res end
end

%{Browse {SumTR [2 3 4 5 1] 0}}
  

CSci 4651 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.