Computing on the Language
27 Oct 2011And now for something a bit more esoteric….
I recently wrote a function to deal with a strange problem. Writing the function ended up being a fun challenge related to computing on the R language itself.
Here’s the problem: Write a function that takes any number of R objects as arguments and returns a list whose names are derived from the names of the R objects.
Perhaps an example provides a better description. Suppose the function is called ‘makeList’. Then
x <- 1
y <- 2
z <- "hello"
makeList(x, y, z)
returns
list(x = 1, y = 2, z = "hello")
It originally seemed straightforward to me, but it turned out to be very much not straightforward.
Note that a function like this is probably most useful during interactive sessions, as opposed to programming.
I challenge you to take a whirl at writing the function, you know, in all that spare time you have. I’ll provide my solution in a future post.