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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:30:22+00:00 2026-05-28T00:30:22+00:00

I try to figure out something intersting that happen in Scheme: (define last-pair (lambda

  • 0

I try to figure out something intersting that happen in Scheme:

(define last-pair
     (lambda (x)
       (if (null? (cdr x))
           x
           (last-pair (cdr x)))))

When I defined foo in this way:

(define foo
  (lambda ()
    (let ((s (list 'he 'said:)))
      (set-cdr! (last-pair s)
                (list 'ha 'ha))
      s)))

and run foo 3 times, I got:

(he said: ha ha)
(he said: ha ha)
(he said: ha ha)

But when I defined foo in this way:

(define foo
  (lambda ()
    (let ((s '(he said:))) 
      (set-cdr! (last-pair s)
                (list 'ha 'ha))
      s)))

and run foo 3 times, I got:

(he said: ha ha)
(he said: ha ha ha ha)
(he said: ha ha ha ha ha ha)

But why? My first thought was that we build always new list in the first foo, and in the second we don’t. But I didn’t understood how it actully working. Scheme define adress in the second foo, and do what? Is it define as a list also in the second foo? or a symbol?

Thanks.

  • 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-28T00:30:22+00:00Added an answer on May 28, 2026 at 12:30 am

    Literal lists ('(foo bar baz), as opposed to (list 'foo 'bar 'baz)) are not allowed to be mutated. If you do, that “is an error” (i.e., the behaviour is undefined).

    In this case, what you are observing is that the literal '(he said:) is being reused over and over, with the understanding that it’s not going to get mutated. Since you violated that understanding, you get the weird behaviour you saw.

    In contrast, when you use (list 'he 'said:), a new list is returned every time.

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

Sidebar

Related Questions

I try to figure out what -beginUpdates and -endUpdates is actually good for. Adding
I'm try to figure out how to handle the following scenario. In general, i
I'm trying to figure out a way to display a simple countdown that displays
I can't figure out how to dissmis keyboard when Done or something outside textfield
I have 2 very large tables. I try to figure out what they have
In my program I have something close to 50 events that are thrown out.
I am writing an app in javascript to try to figure out item builds
I'm trying to figure out how to write a pattern that can match to
I want to try figure out how to get the <title>A common title</title> <meta
try: spam.foo except AttributeError: do_somthing() (Is it wise to check an attribute like that

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.