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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:51:03+00:00 2026-06-05T15:51:03+00:00

In this problem, I have three (identically-structured) lists. Two have all numbers and the

  • 0

In this problem, I have three (identically-structured) lists. Two have all numbers and the other is filled with nil. I’m trying to replace the corresponding value in the empty list with the addition of the corresponding values from the two lists. What I have so far utilizes a loop and uses setf to replace the value.

(defun add-two-lists (list1 list2 list3)
   (loop for a in list1
        for b in list2
        for c in list3 do
        (setf c (+ a b))))

The problem is that this function is not being destructive. How do I make this function destructive?


Ok, I am aware I could use an apply to do this, but for future or tangent purposes, is there a way to use a loop to do the same thing?


I’ve decided to resort to my penultimate solution; use the list-length to transverse the lists.

(defun add-two-lists (list1 list2 list3)
       (loop for x from 0 to (- (list-length list1) 1) do
            (setf (nth x list3) (+ (nth x list1) (nth x list2))))
       (values list3))
  • 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-05T15:51:06+00:00Added an answer on June 5, 2026 at 3:51 pm

    Yet another way to do the same thing without using a loop (though it’s conceptually similar)

    (defun add-two-lists (a b c &optional (d c))
      (if a
        (add-two-lists
         (cdr a) (cdr b)
         (cdr (rplaca c (+ (car a) (car b)))) d) d))
    
    (add-two-lists '(1 2 3 4 5) '(1 2 3 4 5) '(nil nil nil nil nil))
    

    EDIT

    (defun add-two-lists (a b c &optional (d c))
      (if a
        (add-two-lists
         (cdr a) (cdr b)
         (cdr (rplaca c (+ (car a) (car b)))) d) d))
    
    (time
     (dotimes (i 1e6)
       (add-two-lists '(1 2 3 4 5)
              '(1 2 3 4 5)
              '(nil nil nil nil nil))))
    
    ;; Evaluation took:
    ;;   0.077 seconds of real time
    ;;   0.076004 seconds of total run time (0.076004 user, 0.000000 system)
    ;;   98.70% CPU
    ;;   214,723,476 processor cycles
    ;;   0 bytes consed
    
    (defun add-two-lists-1 (list1 list2 list3)
      (loop for a in list1
         for b in list2
         for c on list3 do
           (rplaca c (+ a b))))
    
    (time
     (dotimes (i 1e6)
       (add-two-lists-1 '(1 2 3 4 5)
              '(1 2 3 4 5)
              '(nil nil nil nil nil))))
    
    ;; Evaluation took:
    ;;   0.060 seconds of real time
    ;;   0.060004 seconds of total run time (0.060004 user, 0.000000 system)
    ;;   100.00% CPU
    ;;   169,395,444 processor cycles
    ;;   0 bytes consed
    

    EDIT 2

    But notice the optimized version behavior. Possibly, again, YMMV, but this is what I get on 64-bit Debian with SBCL.

    (defun add-two-lists (a b c &optional (d c))
      (declare (optimize (speed 3) (safety 0)))
      (declare (type list a b c d))
      (if a
        (add-two-lists
         (cdr a) (cdr b)
         (cdr (rplaca
           c 
           (the fixnum
             (+ (the fixnum (car a))
            (the fixnum (car b)))))) d) d))
    
    (time
     (dotimes (i 1e6)
       (add-two-lists '(1 2 3 4 5)
              '(1 2 3 4 5)
              '(nil nil nil nil nil))))
    
    ;; Evaluation took:
    ;;   0.041 seconds of real time
    ;;   0.040002 seconds of total run time (0.040002 user, 0.000000 system)
    ;;   97.56% CPU
    ;;   114,176,175 processor cycles
    ;;   0 bytes consed
    
    (defun add-two-lists-1 (list1 list2 list3)
      (declare (optimize (speed 3) (safety 0)))
      (loop for a fixnum in list1
         for b fixnum in list2
         for c cons on list3 do
           (rplaca c (the fixnum (+ a b)))))
    
    (time
     (dotimes (i 1e6)
       (add-two-lists-1 '(1 2 3 4 5)
              '(1 2 3 4 5)
              '(nil nil nil nil nil))))
    
    ;; Evaluation took:
    ;;   0.040 seconds of real time
    ;;   0.040003 seconds of total run time (0.040003 user, 0.000000 system)
    ;;   100.00% CPU
    ;;   112,032,123 processor cycles
    ;;   0 bytes consed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables being used for this problem: songs, blacklist, and whitelist. The
I don't know where to look for about this problem ... I have three
I have two (or more) different databases, identically structured but each containing different data.
Here is the problem that I am trying to solve. I have two folders
I have a cross-tab report which similar to this Problem is when there are
I have this pattern written ^.*\.(?!jpg$|png$).+$ However there is a problem - this pattern
This sounds like a problem many others have posted about, but there's a nuance
I have a problem where I have some html like this <p>There is the
I've seen that a few instances of this problem have been raised already. However,
NOTE: I have solved the majority of this problem but have run into a

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.