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 9251153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:37:32+00:00 2026-06-18T10:37:32+00:00

Recently I met a conflict between set! and closures when implementing a scheme interpreter.

  • 0

Recently I met a conflict between set! and closures when implementing a scheme interpreter.

The description of (set! var expr) is the following: (tspl)

set! does not establish a new binding for var but rather alters the value of an existing binding. It first evaluates expr, then assigns var to the value of expr. Any subsequent reference to var within the scope of the altered binding evaluates to the new value.

And the description about the binding of a lambda is: (tspl)

When the procedure is created, the bindings of all variables occurring free within the body, excluding the formal parameters, are retained with the procedure. Subsequently, whenever the procedure is applied to a sequence of actual parameters, the formal parameters are bound to the actual parameters, the retained bindings are restored, and the body is evaluated.

I don’t really understand what does it mean ‘free variable‘, My implementation of a closure is to create a snapshot of all the currently available variables and store the snapshot into a closure. Whenever the closure is executed, it does the following works in order: (code)

  1. create a local scope(context) that refers the parent scope of currently executing environment.
  2. store all variables in the snapshot into the new-created scope.
  3. store all actual arguments with names of corresponding parameters in the new scope.
  4. evaluate the body as a list in the new scope.

My method to implement a set! operator is: (code)

  1. find the scope upwards(along the parent reference), till the one which contains a variable with the given name.
  2. set the value of the variable in that scope to the new one.

This procedure seems work in most times, however when there’s a set! inside that aims to modify an external variable out of the local scope, that turns impossible since set! can’t really find the REAL variable but just the one stored in the snapshot of the closure.

Let me explain this in code:

(define x 3)

((lambda ()      ;; binding(snapshot) == { x: 3 }
   (set! x 4)    ;; changing the value in local scope which derived from the snapshot.
   (display x))) ;; ==> 4

(display x)      ;; ==> 3

How can I solve this conflict?

  • 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-06-18T10:37:34+00:00Added an answer on June 18, 2026 at 10:37 am

    Easy! Don’t use snapshots. Access higher-level scopes directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I met a strange problem, see code snips as below: var sqlCommand: string;
Recently I met the following exception at C# solution: Error 2 Could not load
Recently the PHP manual started showing the following warning on every mysql function page:
Recently we turned a set of complicate C# based scheduling logic into SQL CLR
I met with such question about msgget recently. while(1) { msqid = msgget(IPC_PRIVATE,IPC_CREAT); if(msqid<0)
I've recently started experimenting with DataBinding and implementing DependencyProperties for my custom classes. It
I recently met a problem using blocks with Facebook's app switching. I needed to
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument I recently met a
I am working on an Android project recently and met an issue. I send
I met a very strange problem recently. I want to control the ListBoxItem's render

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.