Skip to content

Instantly share code, notes, and snippets.

@xquery
Last active June 27, 2018 11:35
Show Gist options
  • Save xquery/6785050 to your computer and use it in GitHub Desktop.
Save xquery/6785050 to your computer and use it in GitHub Desktop.
the 'naked' Y-combinator in XQuery 3.0 using classic factorial example, providing the ability to recurse within anonymous functions
xquery version "3.0";
let $makeFact := function($givenFact) {
let $fact := function($n) {
if( $n lt 2 )
then 1
else $n * $givenFact($n - 1)
}
return $fact
}
let $makeRealFact := function($makeFact) {
let $getNextTryFact := function($getNextTryFactRef) {
let $tryFact := function($n) {
let $nextTryFact := $getNextTryFactRef($getNextTryFactRef)
let $result := $nextTryFact($n)
return $result
}
let $nextTryFact := $makeFact($tryFact)
return $nextTryFact
}
return $getNextTryFact($getNextTryFact)
}
let $factorial := $makeRealFact($makeFact)
return $factorial(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment