Re: [basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
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
Re: [basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
…a nice solution! On Sat, Apr 13, 2019 at 11:55 AM Andy Bunce 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 ') > }; > "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(1)=>string-join("") > > =>local:word-wrap(80) > > /Andy > [1] https://stackoverflow.com/a/3892909/3210344 > > On Sat, 13 Apr 2019 at 06:06, Andreas Mixich 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 >> 10else >> 11 let $idx := >> 12if (matches($t, "\s")) >> 13then functx:index-of-string-last(substring($t, $width), " ") >> 14else $width >> 15 return >> 16concat( >> 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
Re: [basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
> 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?
Re: [basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
Andy Bunce: > 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
Re: [basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
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 ') }; "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(1)=>string-join("") =>local:word-wrap(80) /Andy [1] https://stackoverflow.com/a/3892909/3210344 On Sat, 13 Apr 2019 at 06:06, Andreas Mixich 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 > 10else > 11 let $idx := > 12if (matches($t, "\s")) > 13then functx:index-of-string-last(substring($t, $width), " ") > 14else $width > 15 return > 16concat( > 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 >
[basex-talk] A Tail Call, that runs (not) if different numeric values are being used as parameter
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 10else 11 let $idx := 12if (matches($t, "\s")) 13then functx:index-of-string-last(substring($t, $width), " ") 14else $width 15 return 16concat( 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