Question: For example [1,2,3] and i want to add 5 to the 2nd element so that I get [1,7,3]. Would this actually be possible?

Answer: 
One problem is that if `len==1`, then `argms[2:len]` returns a list of length 2; in particular,

```
> identical(argms[2:1], list(NULL, argms[[1]]))
[1] TRUE

```

To fix that you could just drop the first element of the list using `argms[-1]`. 

You also need to make use of your `of` function because as probably you noted `sin(cos)` returns an error rather than a function. Putting this together we get:

```
comp <- function(...) {
  argms <- c(...)
  len <- length(argms)
  if(len==1) { return(of(argms[[1]], id)) }
  else {
    of(argms[[1]], comp(argms[-1]))
  }
}

> comp(sin, cos, tan)(1)
[1] 0.0133878
> compose(sin, cos, tan)(1)
[1] 0.0133878

```
Here is a solution that builds the function from calls, it gives a readable output similar to Benjamin's :

```
compose_explicit <- function(...){
  funs <- as.character(match.call()[-1])
  body <- Reduce(function(x,y) call(y,x), rev(funs), init = quote(x))
  eval.parent(call("function",as.pairlist(alist(x=)),body))
}
compose_explicit(sin, cos, tan)
# function (x) 
# sin(cos(tan(x)))

compose_explicit(sin, cos, tan)(pi/4)
# [1] 0.5143953

```

It seems quite robust:

```
compose_explicit()
# function (x) 
# x

compose_explicit(sin)
# function (x) 
# sin(x)

```

---

And unrelated but useful, here is the code of `purrr:compose` :

```
#' Compose multiple functions
#'
#' @param ... n functions to apply in order from right to left.
#' @return A function
#' @export
#' @examples
#' not_null <- compose(`!`, is.null)
#' not_null(4)
#' not_null(NULL)
#'
#' add1 <- function(x) x + 1
#' compose(add1, add1)(8)
compose <- function(...) {
  fs <- lapply(list(...), match.fun)
  n <- length(fs)

  last <- fs[[n]]
  rest <- fs[-n]

  function(...) {
    out <- last(...)
    for (f in rev(rest)) {
      out <- f(out)
    }
    out
  }
}

```