Let's see what happens:
fac <- function(n) {
ifelse(n == 1, 1, {message(paste(n-1, collapse = ","));
stopifnot(n > 0); n * fac(n-1)})
}
fac(4:5)
As you can see, the condition is never TRUEfor all elements n, and therefore recursion never stops.
If all of the elements nare equal, it works:
fac(c(5,5))
n:
fac <- function(n) {
ifelse(n <= 1, 1, n * fac(n-1))
}
fac(1:5)