Skip to content
Prev 58798 / 398502 Next

Need car() and cdr() for '...'

Paul> Needed to redefine function "sum" for my MATLAB package.

("MATLAB" package, hmm, interesting, let us know more..)

    Paul> There's something similar in Chambers's Green Book (pg 351)
    Paul> so I modified it as such:

    .........

    Paul> So does someone have LISP-derived car/adr
    Paul> functions I can use to split the '...' list such that
    Paul> the generic function could use this instead:

    Paul> sum(c(sum(car(...), na.rm = na.rm),
    Paul>       sum(cdr(...), na.rm = na.rm)))

###---------------

car <- function(...) list(...)[[1]]
cdr <- function(...) list(...) [-1]

## testing:
tst <- function(...) list(car = car(...), cdr = cdr(...))

str(tst(a=1, b=2:4, c=5))

###---------------

gives 

 List of 2
  $ car: num 1
  $ cdr:List of 2
   ..$ b: int [1:3] 2 3 4
   ..$ c: num 5

which I think is what you want.