I found there's a very good functional set of operations in R, such as apply family, Hadley Wickham's lovely plyr, etc. There's even a Reduce (a.k.a. fold). Now I wonder how can we do pattern-matching? E.g., now I split dimensions like this: m <- dim(V)[1] # R n <- dim(V)[2] # still R While even Matlab allows for [m,n] = size(V) % MATLAB! Ideally I'd be able to say, <<x,y>> <- dim(V) -- where <<.,.>> is some magic needed. Similarly, to break lists, we'd need, in a MLish notation, match L with | head::tail => ... | () => ; What can be done in R now to simulate it, and/or how Rish is it to add something like that? Cheers, Alexy
On 29/10/2008 4:39 PM, Alexy Khrabrov wrote:> I found there's a very good functional set of operations in R, such as > apply family, Hadley Wickham's lovely plyr, etc. There's even a > Reduce (a.k.a. fold). Now I wonder how can we do pattern-matching? > > E.g., now I split dimensions like this: > > m <- dim(V)[1] # R > n <- dim(V)[2] # still R > > While even Matlab allows for > > [m,n] = size(V) % MATLAB! > > Ideally I'd be able to say, > > <<x,y>> <- dim(V) > > -- where <<.,.>> is some magic needed. > > Similarly, to break lists, we'd need, in a MLish notation, > > match L with > | head::tail => ... > | () => ; > > What can be done in R now to simulate it, and/or how Rish is it to add > something like that?You can do this: names <- c("m", "n") for (i in seq_along(names)) assign(names[i], dim(V)[i]) With some trickery, I think you could write a function that did this based on syntax like _(m, n) <- dim(V) I would call this quite non-Rish. It needs tricky evaluation (m and n are only there as names, not as bindings to objects). I don't know ML, so I don't understand your second example. Duncan Murdoch
Using the list function defined here: tolstoy.newcastle.edu.au/R/help/04/06/1430.html list[m, n] <- as.list(dim(iris)) mylist <- as.list(1:5) list[Head, Tail] <- list(mylist[[1]], mylist[-1]) On Wed, Oct 29, 2008 at 4:39 PM, Alexy Khrabrov <deliverable at gmail.com> wrote:> I found there's a very good functional set of operations in R, such as apply > family, Hadley Wickham's lovely plyr, etc. There's even a Reduce (a.k.a. > fold). Now I wonder how can we do pattern-matching? > > E.g., now I split dimensions like this: > > m <- dim(V)[1] # R > n <- dim(V)[2] # still R > > While even Matlab allows for > > [m,n] = size(V) % MATLAB! > > Ideally I'd be able to say, > > <<x,y>> <- dim(V) > > -- where <<.,.>> is some magic needed. > > Similarly, to break lists, we'd need, in a MLish notation, > > match L with > | head::tail => ... > | () => ; > > What can be done in R now to simulate it, and/or how Rish is it to add > something like that? > > Cheers, > Alexy > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >