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

  • SEARCH
  • Home
  • 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 8828757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:40:50+00:00 2026-06-14T07:40:50+00:00

The following Scheme code (let ((x 1)) (define (f y) (+ x y)) (set!

  • 0

The following Scheme code

(let ((x 1))
   (define (f y) (+ x y))
   (set! x 2)
   (f 3) )

which evaluates to 5 instead of 4. It is surprising considering Scheme promotes static scoping. Allowing subsequent mutation to affect bindings in the closed environment in a closure seems to revert to kinda dynamic scoping. Any specific reason that it is allowed?

EDIT:

I realized the code above is less obvious to reveal the problem I am concerned. I put another code fragment below:

(define x 1)

(define (f y) (+ x y))

(set! x 2)

(f 3)  ; evaluates to 5 instead of 4
  • 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-14T07:40:51+00:00Added an answer on June 14, 2026 at 7:40 am

    Allowing such mutation is excellent. It allows you to define objects with internal state, accessible only through pre-arranged means:

    (define (adder n)
      (let ((x n))    
        (lambda (y)
          (cond ((pair? y) (set! x (car y)))
                (else (+ x y))))))
    
    (define f (adder 1))
    (f 5)                 ; 6
    (f (list 10))
    (f 5)                 ; 15
    

    There is no way to change that x except through the f function and its established protocols – precisely because of lexical scoping in Scheme.

    The x variable refers to a memory cell in the internal environment frame belonging to that let in which the internal lambda is defined – thus returning the combination of lambda and its defining environment, otherwise known as “closure”.

    And if you do not provide the protocols for mutating this internal variable, nothing can change it, as it is internal and we’ve long left the defining scope:

    (set! x 5) ; WRONG: "x", what "x"? it's inaccessible!
    

    EDIT: your new code, which changes the meaning of your question completely, there’s no problem there as well. It is like we are still inside that defining environment, so naturally the internal variable is still accessible.

    More problematic is the following

    (define x 1)
    (define (f y) (+ x y))
    (define x 4)
    (f 5) ;?? it's 9.
    

    I would expect the second define to not interfere with the first, but R5RS says define is like set! in the top-level.

    Closures package their defining environments with them. Top-level environment is always accessible.

    The variable x that f refers to, lives in the top-level environment, and hence is accessible from any code in the same scope. That is to say, any code.

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

Sidebar

Related Questions

I'm new to scheme. When I run the following code (define lst '(1)) (let
Let's say I have the following bit of code (which I know could be
I tried running the following scheme code: (define affiche-gagnant (lambda (j1 j2 g1 g2)
I tried running the following scheme code: (define affiche-gagnant (lambda (j1 j2 g1 g2)
So I have the following code block in Racket/Scheme: (define (inc x) (local (define
Consider the following code, which is a scheme of storing a callback function as
I have a code to the following scheme: class First s where func1 ::
I have the following code and its evaluation as comment: (require scheme/mpair) (list) ;;
I've the following code, though I set the profile_directory Firefox webdriver still attempts to
Let's say I have the following code: Dim _Brush_Disabled As New SolidBrush(Color.FromKnownColor(KnownColor.GrayText)) Dim _Brush_Enabled

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.