[R] Break during the recursion?

Duncan Murdoch murdoch at stats.uwo.ca
Sun Jul 15 17:17:45 CEST 2007


On 15/07/2007 10:33 AM, Atte Tenkanen wrote:
>> On 15/07/2007 10:06 AM, Atte Tenkanen wrote:
>>> Hi,
>>>
>>> Is it possible to break using if-condition during the recursive 
>> function?
>> You can do
>>
>>  if (condition) return(value)
>>
>>> Here is a function which almost works. It is for inorder-tree-
>> walk. 
>>> iotw<-function(v,i,Stack,Indexes) # input: a vector and the first 
>> index (1), Stack=c(), Indexes=c().
>>> {
>>> 	print(Indexes)
>>> 	# if (sum(i)==0) break # Doesn't work...
>> 	if (sum(i)==0) return(NULL)
>>
>> should work.
>>
>> Duncan Murdoch
> 
> Hmm - - - I'd like to save the Indexes-vector (in the example c(8,4,9,2,10,5,11,1,3)) and stop, when it is ready. 

This seems more like a problem with the design of your function than a 
question about R.  I can't really help you with that, because your 
description of the problem doesn't make sense to me.  What does it mean 
to do an inorder tree walk on something that isn't a tree?

Duncan Murdoch


> 
> The results are enclosed to the end.
> 
> Atte
> 
>>> 	
>>> 	if (is.na(v[i])==FALSE & is.null(unlist(v[i]))==FALSE)
>>>         	{Stack=c(i,Stack); i=2*i; iotw(v,i,Stack,Indexes)}
>>> 	Indexes=c(Indexes,Stack[1])
>>> 	Stack=pop.stack(Stack)$vector
>>> 	Indexes=c(Indexes,Stack[1])
>>> 	i=2*Stack[1]+1
>>> 	Stack=pop.stack(Stack)$vector
>>> 	iotw(v,i,Stack,Indexes)
>>> }
>>>
>>>
>>>> v=c(`-`,`+`,1,`^`,`^`,NA,NA,"X",3,"X",2)
>>>> Stack=c()
>>>> Indexes=c()
>>>> iotw(v,1,Stack,Indexes)
>>> NULL
>>> NULL
>>> NULL
>>> NULL
>>> NULL
>>> [1] 8 4
>>> [1] 8 4
>>> [1] 8 4 9 2
>>> [1] 8 4 9 2
>>> [1] 8 4 9 2
>>> [1]  8  4  9  2 10  5
>>> [1]  8  4  9  2 10  5
>>> [1]  8  4  9  2 10  5 11  1
>>> [1]  8  4  9  2 10  5 11  1
>>> [1]  8  4  9  2 10  5 11  1  3
>>> Error in if (is.na(v[i]) == FALSE & is.null(unlist(v[i])) == 
>> FALSE) { : 
>>> 	argument is of length zero
>>>
>>> Regards,
>>>
>>> Atte Tenkanen
>>> University of Turku, Finland
>>>
> 
> 
>> iotw(v,1,Stack,Indexes)
> NULL
> NULL
> NULL
> NULL
> NULL
> [1] 8 4
> [1] 8 4
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2 5 1 3
> [1] 8 4
> [1] 8 4
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1] 8 4 9 2
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1
> [1]  8  4  9  2 10  5 11  1  3
> [1]  8  4  9  2 10  5 11  1  3
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1
> [1] 8 4 9 2 5 1 3
> [1] 8 4 9 2 5 1 3
> [1] 4 2
> [1] 4 2
> [1] 4 2
> [1]  4  2 10  5
> [1]  4  2 10  5
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5
> [1]  4  2 10  5
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1
> [1]  4  2 10  5 11  1  3
> [1]  4  2 10  5 11  1  3
> [1] 4 2 5 1
> [1] 4 2 5 1
> [1] 4 2 5 1 3
> [1] 4 2 5 1 3
> [1] 2 1
> [1] 2 1
> [1] 2 1 3
> [1] 2 1 3
> [1] 1
> [1] 1



More information about the R-help mailing list