From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Rodrigo Morales Newsgroups: gmane.emacs.help Subject: Re: [Elisp][Question] How to modify a list by index while preserving value outside of scope? Date: Sun, 20 Aug 2023 04:08:29 +0000 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11485"; mail-complaints-to="usenet@ciao.gmane.io" To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Sun Aug 20 06:09:51 2023 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qXZl8-0002nA-7c for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 20 Aug 2023 06:09:50 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXZkA-0003mX-Js; Sun, 20 Aug 2023 00:08:50 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qXZk9-0003mO-6N for help-gnu-emacs@gnu.org; Sun, 20 Aug 2023 00:08:49 -0400 Original-Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXZk6-0005CU-Uo for help-gnu-emacs@gnu.org; Sun, 20 Aug 2023 00:08:48 -0400 Original-Received: by mail-ua1-x936.google.com with SMTP id a1e0cc1a2514c-7870821d9a1so1424283241.1 for ; Sat, 19 Aug 2023 21:08:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692504525; x=1693109325; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=bBGv0qAfumVu/0B0UX3UhVQiZrWiCcBzFsJYPfb7hNM=; b=OD6DYvIvUjHqGsESaNuHSTczkeGiGHm1tTLT1VRWtzyUir71tSwhGBz3/xBQluMACr u7VUhSuHDB0QonRNB9sF09lLFWXiTw58ijocllZyFauY9BhBNYDA1jXfOi3rC+/NGKaj kcfYHkTc1lUnUIGR0DzqzAcvtIcTDDfFb5l3ym+qq0s9ghWaPv1BDsucbrL8zUD0qz/V kHz0mqvDjZzfp4woTGxx93muoJCtrsS2e7NH7RNFrslQixQ7Vro1U7mXF7nWRsmOLSum GyLXeiVevyidUwdxGzvaX0PpCbu+atB0uqIyS5OwGIA43ZnROMLqKvWwjmCXfEpY70V3 eNeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692504525; x=1693109325; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bBGv0qAfumVu/0B0UX3UhVQiZrWiCcBzFsJYPfb7hNM=; b=hPdBufLGhscrFuKB3p4Fztpa7LHvynyUli94bBYYECGEs6bIRjCkPuyI7aEkypxCVq QAisH1ro95XvBY+gFP5sFZ0zddGBzh3SuSPjZ0ri/VDgc7MT1j8UZtoj1+sXTUTly/6j 5BrLNzJ9Hi993sQVWQHiZvorFihs+YbMO+QWClMDExM3qcXneVNxtJbSlwJ0pwg53EJI lGSHRilBiLvbXS05UzF+XYZHXYHTk9q6mxw3Il2tJbGP5yONAVsH5gs3MKWHuji4po2V KK5QdrSTN4+S8WyP5zVNaIddc3DEQsQ/GbjEEGTQ8TRZhFnKBL/RvAwFwTu4Zpz+mE3L ZrUQ== X-Gm-Message-State: AOJu0YxjTot329/HaM07U3cSY2us3A5AkV/Qf5OPaEjwUh7Ut8Y6KHjb TMnsAUYIDq+JNnVHwWBANNk79SbAlzmuJimhZJpRq8rdUv8= X-Google-Smtp-Source: AGHT+IHq0r2TbN0dfJK/VsTVSfljPc/DJmDtNedUc4PGnnB9ij5LTs45BUbLsuXPBQsOTRVCNZRnT+mVmrs6Z6+U1mI= X-Received: by 2002:a67:ba07:0:b0:443:61f7:3ce with SMTP id l7-20020a67ba07000000b0044361f703cemr2676731vsn.15.1692504525516; Sat, 19 Aug 2023 21:08:45 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::936; envelope-from=moralesrodrigo1100@gmail.com; helo=mail-ua1-x936.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.help:144885 Archived-At: Here's a simpler example. --- (defun my/f2 (foo) (setf (nth 0 foo) 12)) (defun my/f1 (foo) (princ (format "%s\n" foo)) (my/f2 foo) (princ (format "%s\n" foo))) (let ((items (list 1 2 3))) (my/f1 items)) --- --- (1 2 3) (12 2 3) --- Today I learned that `setf' can be used inside functions to change the value of lists and see those changes outside of the function. I wrongly believed that whenever function parameters were passed by copy and there was no way to see the changes reflected outside of the function (i.e. the only way would be to return a value). Relevant information on this topic is appreciated. On Sun, 20 Aug 2023 at 03:46, Rodrigo Morales wrote: > > ---- > (defun my/f (foo bar) > (princ (format "(foo: %s) (bar: %s)\n" foo bar)) > (cond > (foo (setf (nth 0 bar) "100")) > (t (my/f "apples" bar) > (my/f "bananas" bar)))) > > (my/f nil (list 123)) > ---- > > --- > (foo: nil) (bar: (123)) > (foo: apples) (bar: (123)) > (foo: bananas) (bar: (100)) > --- > > I have some questions: > > + The second time my/f function is called (i.e. when "apples" is > passed), `bar' equals `123'. The third time `my/f' is called > (i.e. when "bananas" is passed), `bar' has a different value. We can > conclude that the modification to `bar' in the second call affected > the third call. How is this possible? `bar' is an argument of `my/f', > as far as I'm concerned, any modification to a variable that is a > function parameter only affects the scope of the function. > + This is a minimal working example, in reality, the code I'm writing is > more complex. In the code that I'm writing, `bar' is a list and I want > to modify some of their elements by index. The only way I know is by > using `(setf (nth index my-list) new-value)'. However, using this > method seems to changes the value of the variable outside of the > function call. Are there any other ways to modify a list by index > without affecting its value outside of the function call?