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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:44:06+00:00 2026-06-08T23:44:06+00:00

In lisp, I can pass an argument to a function and have it altered

  • 0

In lisp, I can pass an argument to a function and have it altered within the function. (AKA destructive functions). However, in Clojure, I’ve read somewhere that it is not permissible to alter the given arguments within that same function. For example:

(defn add-two-lists [list1 list2]
  (for [n (range (count list1))]
    (+ (nth list1 n) (nth list2 n))))

This is a normal function and its output is the addition of the two identical lists. However, I want something like this:

(defn add-two-lists [list1 list2 added_list]
  (set! added_list 
       (for [n (range (count list1))]  
          (+ (nth list1 n) (nth list2 n)))))

Perhaps my use of set! is wrong or misused, and I still get errors. Is there a elegant way to destructively modify arguments in Clojure?

  • 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-08T23:44:07+00:00Added an answer on June 8, 2026 at 11:44 pm

    The with-local-vars macro lets you create thread-locally bound vars that you can modify with var-set. You also have to access the var’s value with var-get, which can be shortened to just @.

    (defn add-two-lists [list1 list2 added-list]
      (var-set added-list 
               (for [n (range (count list1))]  
                 (+ (nth list1 n) (nth list2 n)))))
    
    (with-local-vars [my-list nil]
      (add-two-lists '(1 2 3) '(3 4 5) my-list)
      @my-list)
    

    EDIT:

    On a stylistic note, you could use map to add the two lists without using the nth function to random-access each index in each list:

    (defn add-two-lists [list1 list2 added-list]
      (var-set added-list (map + list1 list2)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a lot that LISP can redefine syntax on the fly, presumably
can I use common lisp and Clojure from within emacs at the same time?
Possible Duplicate: In Lisp, how many inputs can the + function actually have? The
With Clojure (and other Lisp dialects) you can modify running code. So, when a
How can I write an Emacs Lisp function to find all hrefs in an
Using emacs with Paredit enabled. How can I search through a LISP (Clojure) S-expression
I have a problem with common lisp. I want to pass a string to
In Common LISP I can do: (setf a1 'a) (setf 1a 'b) In clojure
I am trying to write a Lisp function that can take optional and keyword
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in

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.