Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6725061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:45:45+00:00 2026-05-26T09:45:45+00:00

I want to better understand how environments , closures , and frames are related.

  • 0

I want to better understand how environments, closures, and frames are related. I understand function closures contain an environment, environments contain a frame and an enclosure, and frames contain variables, but I’m a bit fuzzy on how they interact with one another.

Perhaps an example of what’s going on during a function call would help? Or maybe a diagram?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:45:46+00:00Added an answer on May 26, 2026 at 9:45 am

    UPDATE R-lang defines an environment as having a frame. I tend to think about frames as stack frames, not as mapping from name to value – but then there is of course the data.frame which maps column names to vectors (and then some…). I think most of the confusion comes from the fact that the original S-language (and still S-Plus) did not have environment objects, so all “frames” were essentially what environment objects are now, except that they could only exists as part of the call stack.

    For instance, in S-Plus the doc for sys.nframe says “sys.nframe returns the numerical index of the current frame in the list of all frames.” …that sounds an awful lot like stack frames to me… You can read more about stack frames here: http://en.wikipedia.org/wiki/Call_stack#Structure

    I expanded some of the explanations below and use the term “stack frame” consistently (I hope).

    END UPDATE

    I’d explain them like this:

    1. An environment is an object that maps variable names to values. Each mapping is called a binding. The value can be either a real value or a promise. An environment has a parent environment (except for the empty environment). When you look up a symbol in an environment and it isn’t found, the parent environments are also searched.

    2. A promise is an unevaluated expression and an environment in which to evaluate the expression. When the promise is evaluated it is replaced with the generated value.

    3. A closure is a function and the environment that the function was defined in. A function like lm would have the stats namespace environment and a user defined function would have the global environment – but a function f defined within another function g would have the local environment for g as its environment.

    4. A stack frame (or activation record) is what represents the entries on the call stack. Each stack frame has the local environment that the function is executed in, and the function call’s expression (so that sys.call works).

    5. When a function call is executed, a local environment is created with it’s parent set to the closure’s environment, the arguments are matched against the function’s formal arguments and those bindings are added to the local environment (as promises). The unmatched formal arguments are assigned the default values (promises) of the function (if any) and marked as missing. A stack frame is then created with this local environment and the call expression. The stack frame is pushed on the call stack and then the body of the function is evaluated in this local environment.

    …so all symbols in the body will be looked up in the local environment (formal arguments and local variables), and if not found in the parent environment (which is the closure enviroment) and the parent’s parent environment and so on until found.

    Note that the parent stack frame’s environment is NOT searched in this case. The parent.frame, sys.frame functions gets the environments on the call stack – that is, the caller’s environment and the caller’s caller’s environment etc…

    # Here match.fun needs to look in the caller's caller's environment to find what "x" is...
    f <- function(FUN) match.fun(FUN)(1:10)
    g <- function() { x=sin; y="x"; f(y) }
    g() # same as sin(1:10)
    
    # Here we see that the stack frames must also contain the actual call expression
    f <- function(...) sys.call()
    g <- function(...) f(..., x=42)
    g(a=2) # f(..., x = 42)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably an easy question, but I want to understand better how Apache
I want to better understand the capabilities and limitations of what can be done
I have a .exe app which I want to understand better - I can
I'm reading Paul Graham's A Plan for Spam and want to understand it better
I want to better understand what is required in order to implement a geo-spatial
I've been searching for tutorials to better understand this, but I'm having no luck.
I'm trying to better understand exception handling and logging in a j2ee environment to
I want to understand better the difference between using 'new' to allocate memory for
I want to better understand how to structure an Android app where an activity
what is the best free/open source WinForms progress bar control? (i.e. want a better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.