-
-
Save emchateau/93ea46559289ec2dd6dc6ab7f17edc46 to your computer and use it in GitHub Desktop.
Recursive function to evaluate whether a phrase is a palindrome
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"; | |
(: 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