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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:12:41+00:00 2026-06-16T22:12:41+00:00

Possible Duplicate: LISP – Global variable keep their old value after reinitialization I’m currently

  • 0

Possible Duplicate:
LISP – Global variable keep their old value after reinitialization

I’m currently doing some coursework with Lisp (using Common Lisp) and have nowhere else to turn with this problem I’m having.

It’s tricky to explain but here goes..

The situation:
I’ve got two global variables, p1 & p2, which are polynomial expressions. My task is to create a polynomial calculator, which is going well so far.

I keep my polynomial elements in a specific format: 3x^2 == ((x 2) 3), and I’ve created two functions which recursively run through both lists of polynomial elements.

If I wanted to add together 3x^2 and 2x^2, the answer would be 5x^2 (or “((x 2) (3+2))”).

I have a third list, the result, which is appended to whenever an element is calculated/can’t be calculated.

The problem:
With two elements I can add together, I create a temporary variable to be added to the result. However, the global variable is changed despite what I do.

I’ve tried let, lambda, various functions, etc. I’ve been stuck for a couple of days now and would greatly appreciate any help you could give me 🙂

Here’s a small example of what I mean:

(setf p1 '((x 2) 2))
    ;2x^2

(setf p2 '((x 2) 3))
    ;3x^2 

(if (equal (first p1) (first p2)) 
    (progn
      (setf temp p1)
      (setf (second temp) (+ (second p1) (second p2)))
      (append-to-result temp)
      (print p1)
      (print temp)))

Output:

((x 2) 5)

((x 2) 5)

  • 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-16T22:12:42+00:00Added an answer on June 16, 2026 at 10:12 pm

    When you do:

    (setf temp p1)
    

    you are not_making a copy of the list structure that p1 refers to. Both variables now refer to the same cons cells. Then when you do:

    (setf (second temp) ...)
    

    you’re modifying that list structure, which both variables still refer to. Change to:

    (setf temp (copy-tree p1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do I see a double variable initialized to some value like
Possible Duplicate: How to get useful error messages in PHP? Im currently learning php
Possible Duplicate: emacs lisp, how to get buffer major mode? Is there a way
Possible Duplicate: What does # mean in LISP I am learning lisp, but one
Possible Duplicate: How to remove nested parentheses in LISP This is my second quick-and-silly
Possible Duplicate: get name of a variable or parameter I have this function. public
Possible Duplicate: In Lisp, how many inputs can the + function actually have? The
Possible Duplicate: What does Asynchronous means in Ajax? jQuery ajax return value Trying to
Possible Duplicate: Variable name as a string in Javascript What I'm trying to do
Possible Duplicate: Why is Lisp used for AI? What makes a language suitable for

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.