[R] Need car() and cdr() for '...'

Martin Maechler maechler at stat.math.ethz.ch
Wed Nov 10 10:14:16 CET 2004


>>>>> "Paul" == Paul Roebuck <roebuck at odin.mdacc.tmc.edu>
>>>>>     on Tue, 9 Nov 2004 14:28:42 -0600 (CST) writes:

    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.




More information about the R-help mailing list