Skip to content

Instantly share code, notes, and snippets.

@xquery
Created October 2, 2013 07:21
Show Gist options
  • Save xquery/6790119 to your computer and use it in GitHub Desktop.
Save xquery/6790119 to your computer and use it in GitHub Desktop.
fully generalized recursion with Y-combinator in xquery 3.0 ... recursion via 'deferred execution'
xquery version "3.0";
let $makeFactorialFunc := function($givenFactFunc) {
let $fact := function($n) {
if( $n lt 2 )
then 1
else $n * $givenFactFunc($n - 1)
}
return $fact
}
let $y := function($le) {
function($f) {
$f($f)
}( function($f) {
$le(
function($x) { ($f($f))($x) }
)
})
}
let $factorial := $y($makeFactorialFunc)
return $factorial(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment