'Combining recursion with map - is reduce the solution?

I am trying to avoid using a for loop at all costs in this example. Consider this simple case:

I have a vector z and an initial condition b1:

z <- 1:5
b1 <- 0

Consider also this simple function like adding:

f <- function(y, b){
 return(y + b)
}

I'd like to write a function that generates a sequence S as follows: the first element, call it S[1] is f's output with the y argument as z[1] and the b argument as the initial condition b1. The second element S[2] is such that S[2] = f(y = z[2], b = S[1]). How can I?

Keep in mind that I don't want to use stuff like cumsum since my actual function f is more complicated The desired output on this case would be the vector:

c(0 + 1,
1 + 2,
3 + 3,
6 + 4,
10 + 5)

Or c(1, 3, 6, 10, 15)

I thought about using reduce but I guess it only accepts deals with the recursive argument b and not the mapping part y



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source