# [R] Nested functions

From: John Wiedenhoeft <wiedenhoeft_at_gmx.net>
Date: Tue 18 Jul 2006 - 06:34:34 EST

I'm having myself a hard time writing an algorithm for finding patterns within a given melody. In a vector I'd like to find ALL sequences that occur at least twice, without having to check all possible patterns via pattern matching.

I finally found a solution in a style that I'm used from C, i.e. calling one function from within another. GNU R doesn't seem to like that, it complains about too deep nesting and infinite recursion (I can't see that...). I've tried options(expressions=500000), but even then the variable a doesn't make it over 2 (my vectors have about 100-300 elements).

I'm not a software engineer, so I guess that algo is rather badly designed. I'd appreciate any help on how to make it suitable for R, or about alternative approaches (I guess something like this must be used in bioinformatics, but I didn't find it implemented in R. Any hints are welcome ;-) )

John

CODECODCODECODECODECODECODECODECODECODECODECODECODECODECODECODE antiphonar <- function(v)
{

```	a <- 1;
b <- 2;
n <- length(v);
alessn(a, b, n, v, x);
```

}

alessn <- function(a, b, n, v, x)
{

```	if(a<n)
{vavb(a, b, n, v, x);}
else{print("That's all, folks ;-)");}
```

}

vavb <- function(a, b, n, v, x)
{

```	if(v[a]==v[b])
{
x <- 1;
while( v[a+x] == v[b+x] && b+x<n)
{x <- x+1;}
m <- 0;
for(k in 0:(x-1))
{m <- v[a+k]*10^(x-1-k)+m;}
p <- c(x, a, b, m);
print(p);
baba(a, b, n, v, x);
}
else baba(a, b, n, v, x);
```

}

baba <- function(a, b, n, v, x)
{

```	b <- b+1;
if(b<=n)
{vavb(a, b, n, v, x);}
else
{
a <- a+1;
b <- a+1;
alessn(a, b, n, v, x);
}
```

}

ENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDENDEND

R-help@stat.math.ethz.ch mailing list