Created
March 29, 2016 21:04
-
-
Save apb2006/df4452ba69efd95c6018a1e8ed28aabf to your computer and use it in GitHub Desktop.
tail recursive
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
(:~ | |
: tail recursion example | |
:) | |
declare function local:sum-a($n){ | |
if($n eq 0) then 1 else $n+ local:sum-a($n - 1) | |
}; | |
declare function local:sum-b($n){ | |
local:sum-iter(1,$n) | |
}; | |
declare function local:sum-iter($sum,$n){ | |
if($n < 2) then $sum else local:sum-iter($sum+$n,$n -1) | |
}; | |
(: sum-a: max value for arg is stacksize=2983 :) | |
local:sum-b(30000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment