Last active
June 27, 2018 11:35
-
-
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
This file contains 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
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