[R] strange output of cat function used in recursive function

David Winsemius dwinsemius at comcast.net
Sat Oct 1 18:46:31 CEST 2016


> On Oct 1, 2016, at 9:39 AM, David Winsemius <dwinsemius at comcast.net> wrote:
> 
> 
>> On Oct 1, 2016, at 9:29 AM, Jan Kacaba <jan.kacaba at gmail.com> wrote:
>> 
>> 2016-10-01 18:02 GMT+02:00 David Winsemius <dwinsemius at comcast.net>:
>>> 
>>>> On Oct 1, 2016, at 8:44 AM, Jan Kacaba <jan.kacaba at gmail.com> wrote:
>>>> 
>>>> Hello Dear R-help
>>>> 
>>>> I  tried to understand how recursive programming works in R. Bellow is
>>>> simple recursive function.
>>>> 
>>>> binary1 <- function(n) {
>>>> if(n > 1) {
>>>>  binary(as.integer(n/2))
>>>> }
>>>> cat(n %% 2)
>>>> }
>>> 
>>> Did you mean to type "binary1(as.integer(n)"?
>> 
>> Yes I meant that.
>> 
>>>> When I call binary1(10) I get 1010. I believe that cat function stores
>>>> value to a buffer appending values as recursion proceeds and at the
>>>> end it prints the buffer. Am I right?
>>> 
>>> No. Read the ?cat help page. It returns NULL. The material you see at the console is a side-effect.
>>>> 
>>>> I tried to modify the function to get some understanding:
>>>> 
>>>> binary2 <- function(n) {
>>>> if(n > 1) {
>>>>  binary2(as.integer(n/2))
>>>> }
>>>> cat(n %% 2, sep=",")
>>>> }
>>>> 
>>>> With call binary2(10) I get also 1010. Why the output is not separated
>>>> by commas?
>>> 
>>> I think because there is nothing to separate when it prints (since there was no "buffer".
>> 
>> If I use function:
>> binary3 <- function(n) {
>> if(n > 1) {
>>  binary3(as.integer(n/2))
>> }
>>  cat(n %% 2, ",")
>> }
>> 
>> and call binary3(10) the console output is separated. So there must be
>> some kind of buffer and also it looks like there is some inconsistency
>> in how cat function behaves. Probably there is other explanation.
> 
> The only inconsistency is how you sent arguments to cat. In the first instance you asked cat to display a single character value (and to separate multiple characters _if_present_ with a comma, .... but there were never any instances of a cat call with multiple arguments).
> 
> In the second instance you told it to display single character values followed by a comma and it did that 4 times when the argument to the enclosing function was a decimal 10.
> 
> If by buffer you mean the console stream, then I suppose I misunderstood your use of the term.

On the matter of a buffer, there is output buffering and a flush.console() function. The only way I could get a regursive function to demonstrate a  pause between output with Sys.sleep was to instead use the Recall function:

?Recall
binary4 <- function(n) {
              if(n > 1) {
                   Recall(as.integer(n/2))
                        }
              cat(n %% 2, ","); flush.console(); Sys.sleep(2)
              }

# halting 2 seconds between each appearance of a digit followed by a comma:

> binary4(10)
1 ,0 ,1 ,0 ,


> 
> -- 
> 
> David Winsemius
> Alameda, CA, USA
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list