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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:11:46+00:00 2026-05-11T14:11:46+00:00

I noticed this semi weirdish-behavior in one of my projects using Scheme and lists.

  • 0

I noticed this semi weirdish-behavior in one of my projects using Scheme and lists. I managed to isolate the behavior into a single section. The code is:

(define x (list 1 2 3)) (define y (list 4 5)) (define z (cons (car x) (cdr y))) (define w (append y z)) (define v (cons (cdr x) (cdr y))) (set-car! x 6) (set-car! y 7) (set-cdr! (cdr x) (list 8))  x y z w v 

Gives us the output of:

(6 2 8) (7 5) (1 5) (4 5 1 5) ((2 8) 5) 

Can anyone explain to me:

  1. Why does (set-car! x 6) not update Z? Since according to my understanding car/cdr return pointers or references to the corresponding values. This is really weird and I’m kinda confused.
  2. If car/cdr does not return references/pointers then how is the final set-cdr! manipulating the list v?

Any ideas? It’s a simple fix, but I’m more curious as to why the weirdness with the variables is going on.

  • 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. 2026-05-11T14:11:46+00:00Added an answer on May 11, 2026 at 2:11 pm

    Okay, let’s step through your program line by line. I’m also assigning unique numbers (think of them as object addresses, if you’re used to C-like languages) for each newly-created object so you can see what’s what. 🙂

    (define x (list 1 2 3))             ; => #1 = (1 . #2), #2 = (2 . #3), #3 = (3 . ()) (define y (list 4 5))               ; => #4 = (4 . #5), #5 = (5 . ()) (define z (cons (car x) (cdr y)))   ; => #6 = (1 . #5) (define w (append y z))             ; => #7 = (4 . #8), #8 = (5 . #6) (define v (cons (cdr x) (cdr y)))   ; => #9 = (#2 . #5) (set-car! x 6)                      ; => #1 = (6 . #2) (set-car! y 7)                      ; => #4 = (7 . #5) (set-cdr! (cdr x) (list 8))         ; => #2 = (2 . #10), #10 = (8 . ()) 

    Now, let’s look at your values (for each reference, use the last assigned value):

    x   ; #1 => (6 . #2) => (6 . (2 . #10)) => (6 2 8) y   ; #4 => (7 . #5) => (7 5) z   ; #6 => (1 . #5) => (1 5) w   ; #7 => (4 . #8) => (4 . (5 . #6)) => (4 . (5 . (1 . #5))) => (4 5 1 5) v   ; #9 => (#2 . #5) => ((2 . #10) 5) => ((2 8) 5) 

    Edit: I’m adding a diagram to explain my answer, since you can’t have diagrams in a comment. I don’t have time to make a diagram showing the values above, but this hopefully explains a few things.

    Expression tree

    Each pair has two ‘slots’, car and cdr, represented as the left and right boxes in the diagram above. Each of these slots, as you see, has three possible things:

    1. An atom (a number in your examples, or symbols in the diagram, such as let, s5, and sqrt)
    2. A reference (represented as an arrow in the diagram)
    3. Null (represented as a black box in the diagram)

    You can put any of these in any of the slots. So, in my explanation above, each of the # items is an arrow, each of the non-# numbers is an atom, and each of the () is a black box. So, in the line

    (define v (cons (cdr x) (cdr y))) 

    you are creating a pair, where the left-hand slot has the same contents as the right-hand slot of x (i.e., an arrow going to pair 2), and the right-hand slot has the same contents as the right-hand slot of y (an arrow going to pair 5). In other words, both boxes in v contain arrows, each going off a different pair.

    Hope this makes more sense. 🙂

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

Sidebar

Related Questions

Background I've been using the C preprocessor to manage and compile semi-large javascript projects
I noticed this accidentally one day, and now decided to test it extensively. So,
I noticed this while using Qt, going through the code examples. When they try
Has anyone noticed this behavior before? This really threw me off... I would have
Have noticed this a fewtimes when I'm piping something into sed which does not
I noticed this piece of code in using CodeIgniter database migrations: $this->migrations->verbose AND print
You probably have noticed this behavior on thefreedictionary.com : on double-clicking a word, the
I've noticed this behavior when writing my JavaScript, and I haven't been able to
I have noticed this weird behavior in firefox, it seems like font have some
I've never noticed this behavior before, but I'm surprised at the output naming conventions

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.