Skip to content
Prev 393630 / 398503 Next

return value of {....}

Richard,

I appreciate your observations. As regularly noted, there are many possible
forks in the road to designing a language and it seems someone is determined
to try every possible fork.

Yes, some languages that are compiled, or like JavaScript, read the entire
function before executing it and promote some parts written further down to
the top so all variable declarations, as one example, are done before
executing any remaining code. If you look at R, if you decide to use a
library, you can ask for it near the top or just before you want to use it.
I think they get executed as needed so if you only load it in one branch of
an IF statement, ...

We can debate good and bad subjective choices in language design BUT for
practical purposes, what matters is that a feature IS a certain way and you
use it consistently with what is documented, not what you wish it was like.
R has an interpreter that arguably may be simple and keep reading small
sections of code and executing them and then getting more. Much of the code
is not even evaluated till much later or never and thus it may not be
trivial to do any kind of look-ahead and adjustment.

Many languages now look a bit silly after some changes are made or things
added that make the earlier way look clumsy. Some features may even be
deprecated and eventually removed, or the language forks again and people
argue that everyone should upgrade to the new version 12.x and so on.

Do note R lets you use rm(x) and also supports multiple environments
including new dynamic ones  so your example might have a region where an x
is used in quite a few ways such as asking some function call to be
evaluated in a specific environment. This flexibility would be harder if you
asked the interpreter to do things like some other languages that may not
support much. However, lots of languages with scoping rules will indeed
allow you to use variables in outer scopes or hidden scopes and so on. To
each their own.

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Richard O'Keefe
Sent: Sunday, January 15, 2023 6:39 PM
To: Valentin Petzel <valentin at petzel.at>
Cc: R help Mailing list <r-help at r-project.org>
Subject: Re: [R] return value of {....}

I wonder if the real confusino is not R's scope rules?
(begin .) is not Lisp, it's Scheme (a major Lisp dialect), and in Scheme,
(begin (define x ...) (define y ...) ...) declares variables x and y that
are local to the (begin ...) form, just like Algol 68.  That's weirdness 1.
Javascript had a similar weirdness, when the ECMAscript process eventually
addressed.  But the real weirdness in R is not just that the existence of
variables is indifferent to the presence of curly braces, it's that it's
*dynamic*.  In f <- function (...) {
   ... use x ...
   x <- ...
   ... use x ...
}
the two occurrences of "use x" refer to DIFFERENT variables.
The first occurrence refers to the x that exists outside the function.  It
has to: the local variable does not exist yet.
The assignment *creates* the variable, so the second occurrence of "use x"
refers to the inner variable.
Here's an actual example.
+     a <- x
+     x <- 42
+     b <- x
+     list(a=a, b=b)
+ }
$a
[1] 137
$b
[1] 42

Many years ago I set out to write a compiler for R, and this was the issue
that finally sank my attempt.  It's not whether the occurrence of "use x" is
*lexically* before the creation of x.
It's when the assignment is *executed* that makes the difference.
Different paths of execution through a function may result in it arriving at
its return point with different sets of local variables.
R is the only language I routinely use that does this.

So rule 1: whether an identifier in an R function refers to an outer
variable or a local variable depends on whether an assignment creating that
local variable has been executed yet.
And rule 2: the scope of a local variable is the whole function.

If the following transcript not only makes sense to you, but is exactly what
you expect, congratulations, you understand local variables in R.
+     n <- 10
+     r <- numeric(n)
+     for (i in 1:n) {
+         if (i == 6) x <- 100
+         r[i] <- x + i
+     }
+     r
+ }
[1]   1   2   3   4   5 106 107 108 109 110
On Fri, 13 Jan 2023 at 23:28, Valentin Petzel <valentin at petzel.at> wrote:

            
accessible?
deleted.
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Thread (32 messages)

akshay kulkarni return value of {....} Jan 9 Valentin Petzel return value of {....} Jan 9 Rui Barradas return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 Andrew Simmons return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 9 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 9 Jeff Newmiller return value of {....} Jan 9 Bert Gunter return value of {....} Jan 9 akshay kulkarni return value of {....} Jan 10 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 10 akshay kulkarni return value of {....} Jan 10 Richard O'Keefe return value of {....} Jan 10 akshay kulkarni return value of {....} Jan 11 Valentin Petzel return value of {....} Jan 12 Heinz Tuechler return value of {....} Jan 13 Bill Dunlap return value of {....} Jan 13 akshay kulkarni return value of {....} Jan 15 akshay kulkarni return value of {....} Jan 15 akshay kulkarni return value of {....} Jan 15 Richard O'Keefe return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15 Bert Gunter return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15 Bert Gunter return value of {....} Jan 15 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 15 @vi@e@gross m@iii@g oii gm@ii@com return value of {....} Jan 15 Sorkin, John return value of {....} Jan 15