hello --

So I want to subtract one list of sentences from another list of
sentences.  (As a way of checking "what did processing do to this
document?"; we know it added some words, and if we added words to the
sentence it was at the start of the sentence, but none of the original
words should be changed or missing or reordered relative to the document.)

I have tested that I have the sequences of strings which are the list of
sentences before I use them to call:

declare function local:subtractSentences($before as xs:string*,$after as
xs:string*) as xs:string* {

  let $thisBefore as xs:string? := head($before)
  let $thisAfter as xs:string? := head($after)

  return
    if (not(exists($thisBefore)) or not(exists($thisAfter)))
  then $after (: we're done because we're out of strings, but if we have
any after left we want it :)
  else if (ends-with($thisAfter,$thisBefore))
  then
(replace($thisAfter,$thisBefore,''),local:subtractSentences(tail($before),tail($after)))
  else ($thisAfter,local:subtractSentences($before,tail($after)))
};

In ~150 ms, I get

Error: Stack Overflow: Try tail recursion?

so I have written the function wrong.  What's the more useful pattern for
this case?

Thanks!
Graydon

Reply via email to