"I suspect akshay is (or was? Not sure) unclear about what braces do.
They are not closures... they create an expression that wraps multiple
expressions into one expression... they are a little more like
parentheses than closures. They are not **intrinsically associated**
with creation of environments for holding variables."

Perhaps confusion comes from this behavior described in ?body:
"The bodies of all but the simplest are braced expressions, that is
calls to {: see the ‘Examples’ section for how to create such a call."

and

"body returns the body of the function specified. This is normally a
language object, most often a call to {, but it can also be a symbol
such as pi or a constant (e.g., 3 or "R") to be the return value of
the function."

So braces are needed to demarcate the code/expressions within a
function, but as you said, they do not themselves create closures.
More technically, '{' is primitive, not a closure.

Bert







On Mon, Jan 9, 2023 at 3:16 PM Jeff Newmiller <jdnew...@dcn.davis.ca.us> wrote:
>
> I suspect akshay is (or was? Not sure) unclear about what braces do. They are 
> not closures... they create an expression that wraps multiple expressions 
> into one expression... they are a little more like parentheses than closures. 
> They are not intrinsically associated with creation of environments for 
> holding variables.
>
> Functions are closures... they run in a call-specific environment that 
> inherits from the environment where the function was defined. Blocks (created 
> with braces) simply work with the environment in which they are constructed 
> directly. If you want a block that creates a new local environment without 
> defining a new function then pass your code block to the ?local function.
>
> On January 9, 2023 8:59:12 AM PST, Bert Gunter <bgunter.4...@gmail.com> wrote:
> >Perhaps the following may be of use to you.
> >
> >Consider:
> >
> >> f <- function(){ x <- 3; function(y) x+y}
> >> x <- 5
> >
> >##What does this give?
> >> f()
> >## Why?
> >## How about this?
> >>f()(10)
> >## Why?
> >
> >## If you remove "x <- 3" from the above, what will you get when you repeat
> >the exercise?
> >
> >-- Bert
> >
> >On Mon, Jan 9, 2023 at 8:29 AM Bert Gunter <bgunter.4...@gmail.com> wrote:
> >
> >> Unless you do something special within a function, only the value(s)
> >> returned are available to the caller. That is the essence of
> >> functional-type programming languages.
> >>
> >> You need to read up on (function) environments in R . You can search on
> >> this. ?function and its links also contain useful information, but it may
> >> too terse to be explicable to you. There are of course many available
> >> references on the internet.
> >>
> >> I believe your mental model for how R works is flawed, and you have some
> >> homework to do to correct it. I may be wrong, naturally, but you can judge
> >> by looking at some tutorials.
> >>
> >> -- Bert
> >>
> >> On Mon, Jan 9, 2023 at 6:47 AM akshay kulkarni <akshay...@hotmail.com>
> >> wrote:
> >>
> >>> Dear members,
> >>>                              I have the following code:
> >>>
> >>> > TB <- {x <- 3;y <- 5}
> >>> > TB
> >>> [1] 5
> >>>
> >>> It is consistent with the documentation: For {, the result of the last
> >>> expression evaluated. This has the visibility of the last evaluation.
> >>>
> >>> But both x AND y are created, but the "return value" is y. How can this
> >>> be advantageous for solving practical problems? Specifically, consider the
> >>> following code:
> >>>
> >>> F <- function(X) {  expr; expr2; { expr5; expr7}; expr8;expr10}
> >>>
> >>> Both expr5 and expr7 are created, and are accessible by the code outside
> >>> of the nested braces right? But the "return value" of the nested braces is
> >>> expr7. So doesn't this mean that only expr7 should be accessible? Please
> >>> help me entangle this (of course the return value of F is expr10, and all
> >>> the other objects created by the preceding expressions are deleted. But
> >>> expr5 is not, after the control passes outside of the nested braces!)
> >>>
> >>> Thanking you,
> >>> Yours sincerely,
> >>> AKSHAY M KULKARNI
> >>>
> >>>         [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help@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.
> >>>
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help@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.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
R-help@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.

Reply via email to