Skip to content

Instantly share code, notes, and snippets.

@emchateau
Forked from CliffordAnderson/palindrome.xqy
Created May 20, 2020 23:52
Show Gist options
  • Save emchateau/93ea46559289ec2dd6dc6ab7f17edc46 to your computer and use it in GitHub Desktop.
Save emchateau/93ea46559289ec2dd6dc6ab7f17edc46 to your computer and use it in GitHub Desktop.
Recursive function to evaluate whether a phrase is a palindrome
xquery version "3.0";
(: Recursive function to evaluate whether a phrase is a palindrome :)
declare function local:test-palindrome($palindrome as xs:string?) as xs:boolean {
switch (fn:string-length($palindrome))
case 0 return fn:true()
case 1 return fn:true()
default return
let $first := fn:substring($palindrome, 1, 1)
let $last := fn:substring($palindrome, fn:string-length($palindrome), 1)
let $rest := fn:replace($palindrome, "^.", "")
let $rest := fn:replace($rest, ".$", "")
return
if ($first = $last) then local:test-palindrome($rest)
else fn:false()
};
let $palindrome := "شکر بترازوی وزارت برکش"
let $palindrome := fn:lower-case($palindrome)
let $palindrome := fn:replace($palindrome, " ", "")
return local:test-palindrome($palindrome)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment