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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:45:54+00:00 2026-05-30T03:45:54+00:00

I’m new to lisp and I have a problem when adding an element to

  • 0

I’m new to lisp and I have a problem when adding an element to an existing list.

> (setq l '(1 2))
(1 2)
> (append l 3)
(1 2 . 3)
> l
(1 2)
> (append l '(3))
(1 2 3)
> l
(1 2)
> (list l '(3))
((1 2) (3))
> l
(1 2)
> (cons 3 l)
(3 1 2)
> l
(1 2)
> (push 3 l)
(3 1 2)
> l
(3 1 2)

The example above is what I made on commandline. Here only push works. But even push doesn’t add the element when I execute the code that I write in a file (and load it on commandline). My code example is available in another question’s page.

How can I update the original l so that it adds 3 to itself? I tried several other functions (cons, list) but the result was the same/similar – 3 wasn’t added to the original list.

  • 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-30T03:45:56+00:00Added an answer on May 30, 2026 at 3:45 am

    So you are pushing a value onto a list which is passed as a function parameter? That doesn’t work; push creates a new cons cell, makes it point to the original list, and sets the variable to refer to the new cons cell. Unfortunately, setting a function parameter inside the function doesn’t do anything to the variable which was passed originally (from outside the function).

    When you call a function, the arguments are first evaluated. So if you pass l to a function, what is actually passed is the value of l. Nothing which happens inside the function can change what l refers to (outside the function).

    If you want to make something like a function, but which actually changes the values of the variables used as parameters, you need to write a macro. Or, just make the function return the needed value, and set the original variable outside the function, after you get the return value back. Something like this:

    (setf l (my-function l))

    OR, you could use an extra level of indirection: make l refer to a “dummy” cons cell, which points to the “real” list, and change the cdr of the “dummy” cons from inside your function. This is poor style, however; it’s usually better to use return values, rather than “returning” data in modified arguments.

    Note that if you need to return more than one value, that doesn’t mean you need to modify arguments; Common Lisp can return more than one value from a function, using (values).

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.