[R] %+=% and eval.parent()
Duncan Murdoch
dmurdoch at pair.com
Wed Apr 7 15:16:52 CEST 2004
On 07 Apr 2004 13:51:52 +0200, Peter Dalgaard
<p.dalgaard at biostat.ku.dk> wrote :
>Duncan Murdoch <dmurdoch at pair.com> writes:
>> Wouldn't a clearer syntax to follow be Pascal's? I.e.
>> inc(x[3], 1)
>>
>
>But that's not kosher in functional programming languages where you
>expect function calls not to modify their arguments, is it? (OK, so
>assignment is also a function call as is setq in Lisp, but we have
>syntactic sugar to cover that. That's what was nice about %<-%.)
I think this whole discussion is aiming for ways around functional
programming. I think if you're going to do that, you should be clear
about it.
We can't create an operator %+=% (that's what you meant, isn't it?)
that parses like += in C or <- in R, because of limitations in R
parsing.
We can create assignment functions, but they don't read properly. The
meaning I'd read for
plus(x) <- 3
would be "Set the plus feature of x to 3", and then I'd say "What's
the plus of x???" Your
+x <- 3
is just as obscure.
Personally, I'd generally prefer
x <- x + 3
(because functional programming is good), but I can understand the
desire to avoid repeating a complicated indexing construction in order
to improve the clarity of code. I just don't think that
plus(x[blah1, blah2, blah3]) <- 2
is clearer than
x[blah1, blah2, blah3] <- x[blah1, blah2, blah3] + 2
(because nothing is getting set to 2!), whereas
inc(x[blah1, blah2, blah3], 2)
is reasonably clear.
If this is about efficiency rather than clarity, I think that's a bad
tradeoff.
Duncan Murdoch
More information about the R-help
mailing list