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: [Elisp][Question] How to modify a list by index while preserving value outside of scope? Date: Sun, 20 Aug 2023 03:46:00 +0000 Message-ID: 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="26898"; 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 05:47:04 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 1qXZP5-0006qM-Nk for geh-help-gnu-emacs@m.gmane-mx.org; Sun, 20 Aug 2023 05:47:03 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qXZOQ-0007w8-Jg; Sat, 19 Aug 2023 23:46:22 -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 1qXZOO-0007vu-Gu for help-gnu-emacs@gnu.org; Sat, 19 Aug 2023 23:46:20 -0400 Original-Received: from mail-qk1-x736.google.com ([2607:f8b0:4864:20::736]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qXZOM-0001Aj-8t for help-gnu-emacs@gnu.org; Sat, 19 Aug 2023 23:46:20 -0400 Original-Received: by mail-qk1-x736.google.com with SMTP id af79cd13be357-76d7b5e374fso148989185a.2 for ; Sat, 19 Aug 2023 20:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692503176; x=1693107976; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=2l9VsRB7HEl9BrHBhwtLxIcQb7IPzss4PI1lWE5zr2w=; b=kDAyWpDCGpxVtUfJStnn4wDLLP1dZKtjK6POByrjtdT91I9W/Ypvroobs2FjAT/pqu k/jEzlgRZEDLaiIVpg2ey9Lu7xE6dGkXmho1mJx85OnThM1OcXg8a34bXkNqanMM1GpG zdcpC+sx09GrInfeS/r8SA0iQXwbX7eBNTowKx2fLndBr/6KIUzmiw7cgWWSGngQ4IPM SKlTEkUMneeEvaahDe/QVT7DQXC+3Qa7EgY5K2qzsgPKlHiMFtHcK9KnuSUUx9tsvAr4 +FRZ26XErIcm7Lvt1Hyc5LKvNnmljTAUt916X0vI81Zx3dXaEOeYzSSCy7RPDAdQ+vQe RGfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692503176; x=1693107976; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=2l9VsRB7HEl9BrHBhwtLxIcQb7IPzss4PI1lWE5zr2w=; b=iZx/r+L/uUQjvb+OKlS3x86dornRGoXrHVBfgslMq6J2g8O5ywhXU7imZx1VnmQ9et hzkFfk1O0A6sr98e7PIAzlxD3Vu673Pqjts+jOfExlO95/kqVQisnou0OMTyHb2w7nQH +bflD3w2RJMygPOo7EoP4ciG2iwW1dAVUp168nCHh7GCcW1s6Wuy+18/y0jrZ1XbbQxe TjHLclC75MPkgOJbzMoaNhrb85sAbuR+AVoWwUACA0QdJBCDlkK5yNb9BgjjhDZzStDD qAImoLeOsFR1hgbuPcjEJu55M5m317vKdw3LXJ4ujzv20sN82BXzx5mseey72vo4euR9 CRvQ== X-Gm-Message-State: AOJu0YyvzD+KPa1M8GGV9zTcDM23GynVtDl/k1znO+2gH4DiKfP3Iy6e hGKA64QR3BVFxzGrs7jFOtrP+p1UJclgHaaVCsUiNlk7wnI= X-Google-Smtp-Source: AGHT+IHBq2yhfx9v/KXAV04PxxNFOvX2hHJym/plgtofoKV16UrDkliGzbGIm5+vGLn2TgMiDSR6i0vqXILxg9FAtB4= X-Received: by 2002:a05:620a:410a:b0:765:5415:9f19 with SMTP id j10-20020a05620a410a00b0076554159f19mr5124495qko.7.1692503176513; Sat, 19 Aug 2023 20:46:16 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::736; envelope-from=moralesrodrigo1100@gmail.com; helo=mail-qk1-x736.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:144884 Archived-At: ---- (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?