A Tail Call, that runs (not) if different numeric values are being used as parameter

Saturday, 13 April 2019 · 6 messages · last reply 2 May 2019
AM
Andreas Mixich13 Apr 2019 07:05 #
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
AB
Andy Bunce13 Apr 2019 11:55 #
Hi Andreas, Ignoring for a moment the mysteries of tail recursion, I wonder if a simple regex can give you the wrapping you are looking for. See [1] declare function local:word-wrap($string,$width){ $string=>normalize-space()=>concat(" ")=>replace(``[(.{0,`{ $width }`}) ]``,'$1&#xA;') }; "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." =>util:replicate(10000)=>string-join("") =>local:word-wrap(80) /Andy [1] https://stackoverflow.com/a/3892909/3210344
Quoted text · Andreas Mixich · 56 lines
On Sat, 13 Apr 2019 at 06:06, Andreas Mixich <mixich.andreas at gmail.com> wrote: > 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 >
AM
Andreas Mixich14 Apr 2019 03:01 #
Andy Bunce:
Quoted text · Andy Bunce · 1 line
> I wonder if a simple regex
Absolutely! That did it. Thank you. I am going to use your function. Though, I still wonder, why the exchange of the two expressions did what it did.
-- Goody Bye, Minden jót, Mit freundlichen Grüßen, Andreas Mixich
CG
Christian Grün2 May 2019 12:16 #
Quoted text · Andreas Mixich · 2 lines
> Though, I still wonder, why the exchange of the two expressions did what > it did.
If you simply swap the two branches, I guess there will be too few recursive calls? What happens if you swap the condition, too?
CG
Christian Grünin reply to Andy Bunce, 13 Apr 2019, 11:552 May 2019 12:23 #
…a nice solution!
Quoted text · Andy Bunce · 52 lines
On Sat, Apr 13, 2019 at 11:55 AM Andy Bunce <bunce.andy at gmail.com> wrote: > > Hi Andreas, > > Ignoring for a moment the mysteries of tail recursion, I wonder if a simple regex can give you the wrapping you are looking for. See [1] > > declare function local:word-wrap($string,$width){ > $string=>normalize-space()=>concat(" ")=>replace(``[(.{0,`{ $width }`}) ]``,'$1&#xA;') > }; > "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." > =>util:replicate(10000)=>string-join("") > > =>local:word-wrap(80) > > /Andy > [1] https://stackoverflow.com/a/3892909/3210344 > > On Sat, 13 Apr 2019 at 06:06, Andreas Mixich <mixich.andreas at gmail.com> wrote: >> >> 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
MS
Michael Seiferlein reply to Christian Grün, 2 May 2019, 12:162 May 2019 13:19 #
Hi Andreas, maybe you might want to share your string-Module with us? I don’t think the swapped version is end-recursive either (due to the concat) but it would be interesting to see what happens either way! Best from Konstanz Michael

⚡Generated with XQuery