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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:27:24+00:00 2026-06-01T23:27:24+00:00

EDIT: The solution is to replace the ‘(1) with (list 1) in the first

  • 0

EDIT: The solution is to replace the ‘(1) with (list 1) in the first (let…) form. This is because I was trying to modify literal data. Thanks for the help! (I would give upvotes, but apparently you need 15 reputation…)

This is my first post on this site.

I was solving some Project Euler problems today and I came across some unexpected list sorting behavior (well, at least to me) in Common Lisp:

I have a function that finds all of the proper divisors of a number x:

(defun divisors (x)
    "Finds all of the proper divisors of x."
    (let ((sq (sqrt x)) (divs '(1)))
        (when (integerp sq) (push sq divs))
        (loop for i from 2 to (1- (floor sq)) do
        (let ((div (/ x i)))
            (when (integerp div)
                (push i divs)
                (push div divs))))
    divs))

This function works great. For example:

(divisors 100)
==> (20 5 25 4 50 2 10 1)

The problem arises whenever I try to sort the resulting list:

(sort (divisors 100) #'<)
==> (1 2 4 5 10 20 25 50)

Well, that worked fine. But what happens when I call divisors again?

(divisors 100)
==> (20 5 25 4 50 2 10 1 2 4 5 10 20 25 50)

What? Maybe if I try a different number…

(divisors 33)
==> (11 3 1 2 4 5 10 20 25 50)

The divisors from previous queries are persistent after I sort the resulting list. If I recompile the function, I get no errors until I sort the resulting list again. I am assuming I have messed up somewhere in the function definition, but I am pretty new to Lisp and I cannot find the error. Could it be a problem with the nested (let…) forms?

Thanks in advance!

  • 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-01T23:27:26+00:00Added an answer on June 1, 2026 at 11:27 pm

    Frequently asked.

    You’ve modified literal data. You need to cons up new data. Use the function LIST or the function to make a copy.

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

Sidebar

Related Questions

Intro: EDIT: See solution at the bottom of this question (c++) I have a
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
My current solution will suck sometimes EDIT For those who don't understand,see this example:
I'm having trouble figuring out the solution to this SQL query. Schema Edit :
I'm trying to single source a form page which can be in edit mode
Basically I am trying to have jQuery replace the button with the form and
First of all, I know this is a bad solution and I shouldn't be
EDIT : proper solution: void add(Student s) { if(root == null) root = new
Solution Edit: Turns out you can't use the PHP SDK to return the correct
[edit] Found the solution. Reinstall EVERYTHING - xcode, mono, monodevelop and monotouch. Now it

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.