Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Created June 29, 2022 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CliffordAnderson/7d53e8fd0dc23ce295531125334e5956 to your computer and use it in GitHub Desktop.
Save CliffordAnderson/7d53e8fd0dc23ce295531125334e5956 to your computer and use it in GitHub Desktop.
Fibonacci w/continuations
(: Thanks to friends at NashFP, especially Mike K. and Mark Wutka :)
declare function local:fibC($n as xs:integer, $memo as map(*), $con) {
if (map:contains($memo, $n)) then $con(map:get($memo, $n), $memo)
else
local:fibC($n - 1, $memo,
(: n1 = fib(n - 1) :)
function($n1 as xs:integer, $memo as map(*)) {
local:fibC($n - 2, $memo,
(: n2 = fib(n - 2) :)
function($n2 as xs:integer, $memo as map(*)) {
$con($n1 + $n2, map:put($memo, $n, $n1 + $n2))
} )
} )
};
local:fibC(30, map{1:1, 2:1}, function($x as xs:integer, $memo as map(*)) { $x })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment