On Sat, Jun 12, 2021 at 10:36:13PM +0200, Christian Grün scripsit:
Thanks. Here’s one way how you could handle that:
[snip]
This is similar to the word-level recursive approach I am currently taking. (though there is something to learn in the double function approach for sure; thank you!)
Here’s another solution, which avoids recursive calls and the need to optimize it for tail calls. It’s based on the handsome built-in higher-order function hof:until. Once again, it was Leo (Wörteler) who introduced it to BaseX [1]:
declare function local:correct($old, $new) { let $result := hof:until( (: test if there’s something to compare :) function($m) { empty($m?old) or empty($m?new) }, (: compare, create new input :) function($m) { map { 'old': if(head($m?old) = head($m?new)) then tail($m?old) else $m?old, 'new': tail($m?new) } }, (: initial input :) map { 'old': $old, 'new': $new } ) (: check if there’s old input left :) return empty($result?old) };
This is tremendously cool and I think it will be useful; thank you!
(I also think I am going to have to pour a few buckets of water over my head in the process of trying to fully comprehend it. :)
Much appreciated!
Graydon