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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:24:59+00:00 2026-06-07T03:24:59+00:00

(define self-add (let ((x 0)) (lambda () (set! x (+ x 1)) x))) (self-add)

  • 0
  1. (define self-add
    (let ((x 0))
      (lambda ()
        (set! x (+ x 1))
        x)))
    

(self-add) => 1

(self-add) => 2

(self-add) => 3

(self-add) => 4

    2.
 (define self-add1
    (lambda ()
      (let ((x 0))
        (set! x (+ x 1))
        x)))

(self-add1) => 1

(self-add1) => 1

(self-add1) => 1

Please tell me how to understand the difference between the above two functions?
Thanks a lot in advance!
Best regards.

  • 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-07T03:25:01+00:00Added an answer on June 7, 2026 at 3:25 am

    The first function defines a local variable x with an initial value of 0 and afterwards binds a lambda special form to the name self-add – so x is “enclosed” by the lambda (that’s why we say that the lambda from behaves as a closure) and will be the same for all invocations of self-add (you could say that x is “remembered” by self-add), and each time it gets called the value of x will be incremented by one.

    The second function binds the lambda to the procedure and afterwards defines a local variable x inside the lambda – here x is redefined each time self-add1 gets called, and will be different for all invocations: so x is never “remembered” by self-add1 and is created anew every time the procedure gets called, initialized with 0 and then incremented, always returning the value 1.

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

Sidebar

Related Questions

I'm using the rails-settings gem, and I'm trying to understand how you add functions
I've set up company to load globally like this: (let ((company-path path/to/company)) (add-to-list 'load-path
I'm trying to define a self referencing relationship for a workflow step where that
I can define an object and assign attributes and methods: class object: def __init__(self,a,b):
when subclassing UITableViewCell to define a complete custom cell what style to set? UITableViewCellStyleDefault?
I have two different classes, and I want to define the addition of them
class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: self.value.__add__(100) self.run() def run(self):
TL;DR: Having to define a unique set of getters and setters for each property()'d
in my self-defined module I have this method which will query the db and
I have this self-defined function in my .bashrc : function ord() { printf '%d'

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.