Hi,
as far as I have understood, a tail call function must end with the call to
itself, after it has calculated all the values and, upon recursive call, no
operation must be open in the calling instance of the function. This is why
the following case confuses me.
The function, as shown, produces a stack overflow and I get the hint to do
tail recursion. However, if I swap just the expressions (after 'then' and
'else') in lines 13 and 14, the function computes. Now I would have
thought, that it would break, because I recurse within another function,
that is still open ('concat()') and thus must return to the calling
function after recursion, but, interestingly, the swap of the expressions
after the 'then' and 'else' statements on lines 13 and 14 do not error out
with a stack overflow, in spite 'concat()' still being open. Both cases
produce an integer. Also, advice would be welcome, on how I can solve this.
Thank you very much.
1 declare function local:word-wrap(
2 $string as xs:string,
3 $width as xs:integer)
4
5 {
6 let $t := normalize-space($string)
7 return
8 if (string-length($t) < $width or string-length($t) = $width)
9 then $t
10 else
11 let $idx :=
12 if (matches($t, "\s"))
13 then functx:index-of-string-last(substring($t, $width), " ")
14 else $width
15 return
16 concat(
17 string:trim-right(substring($t, 1, $idx)),
18 out:nl(),
19 local:word-wrap(string:trim-left(substring($t, $idx)),$width)
20 )
21 };
22
23 local:word-wrap("This is a very long line of text. It goes well beyond
80 (eighty) characters in width. Therefore we need a word wrap function,
which we test in here. Lorem ipsum dolor sit amet, consectetur adipisicing
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim ve iam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.",80)
--
Minden jót, all the best, Alles Gute,
Andreas Mixich