From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.help Subject: Re: macros and macroexpand Date: Mon, 7 Aug 2023 18:46:35 +0700 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="28011"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Heime via Users list for the GNU Emacs text editor To: Heime Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Mon Aug 07 13:47:33 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 1qSyhw-00074A-V4 for geh-help-gnu-emacs@m.gmane-mx.org; Mon, 07 Aug 2023 13:47:32 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qSyhH-0007Di-EP; Mon, 07 Aug 2023 07:46:51 -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 1qSyhF-0007DK-K2 for help-gnu-emacs@gnu.org; Mon, 07 Aug 2023 07:46:49 -0400 Original-Received: from mail-vs1-xe29.google.com ([2607:f8b0:4864:20::e29]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qSyhE-0006cA-3f for help-gnu-emacs@gnu.org; Mon, 07 Aug 2023 07:46:49 -0400 Original-Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-44757af136cso1604946137.3 for ; Mon, 07 Aug 2023 04:46:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691408807; x=1692013607; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=4eymgFDj9gYZRtkjtk0cO4RlchK5dsqvMaWyn/5vOu0=; b=qgwpqfnS4paZViX5LAtV4qtDNwr9EuhcTlSwNJV5GmlzJYmYKAB5STTBvAme5xPKwx +2CcIAvQ3GWni6Lzil0SWYImXwc4Ye2gQik+tjCPF5NS96DNOtAyqqh9dYAjC+qvhFhz jgvrZH7ggNzH3hHkH0gn8IRjGEFU/9OA6jnsxueoyhEnjbbZ1sHhr+vD0PMUCxW4HiyU Fei34cjDJ01IX6c31B+oDHicHPDzWlkxZCUYjXmueEhIRiFZRs4rCri5ojdQUw05y0bi WJXQ3lpMreoKZe+xYLLCHTCZznNpvGzEDcHtaHAu2RcWylj8KUmr+XFbQNhuocM/hgFZ QrAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691408807; x=1692013607; h=content-transfer-encoding:cc: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=4eymgFDj9gYZRtkjtk0cO4RlchK5dsqvMaWyn/5vOu0=; b=JGhDWaow0BbKlRJ/NNUxNNX2Jr54kghECu0EXBPwcA3XRUeoPSqEjZXQPX0sg//Saz BKlAenE/zN3uYOOvgVKfKyGPHb5lkFSQOb795vrzk0EPRhJXOlPbWV9vUIPqVyk+3UZs rLYfVFyLIIogq5JW3En4YP1BiYN2jxPYDxJFwArW0POoMIHKku5EL3WZgGwI5L6Q+WYA bxEX6NF+e2M426ld96Osq1ZKxZPi6PtdVsO3camfKDeTnSuHUP1hljxon/qq5VCMeU7p lpMVTeHEy5htfSOg8Hs8i9tB4joiPUqjPGfC6FQ2U0uRqACpLb2iwHtWKIBM9M66Hk9N vorg== X-Gm-Message-State: AOJu0Yw/jLuGelC1NVGJpz/kp8Gcy/VZOh9U1opDAOBmsbAA0GcrMenn 7uYLzsLf8fH7Ctl2HpOQx6Uz4gTxVWN5YRBcFJ8= X-Google-Smtp-Source: AGHT+IFpaaTTVBXDGQO12RUOyTDOZ3HdhqBOJnL5T+DAQEMsm+1KexuDgI/FZF60Z8slEfWBBR/3iUmx6UaquJunbDE= X-Received: by 2002:a67:fbcc:0:b0:443:90ff:c691 with SMTP id o12-20020a67fbcc000000b0044390ffc691mr3193769vsr.13.1691408806757; Mon, 07 Aug 2023 04:46:46 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::e29; envelope-from=yurivkhan@gmail.com; helo=mail-vs1-xe29.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_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:144662 Archived-At: On Mon, 7 Aug 2023 at 18:04, Heime wrote: > I have made a macro and know that they are supposed to return > expanded code for use. Still I cannot understand the need to > call "macroexpand". Should't the macro already perform the > expansion ? You should be posting small examples of code that you=E2=80=99re trying, otherwise, there is high chance people will either misunderstand you or just disregard your questions as ill-posed. ---- When you define a macro, you indeed write the definition similarly to a function that returns expanded code. (defmacro foo (&rest body) `(bar ,@body)) When you evaluate a form that references a macro, Elisp will (1) expand the macro, and (2) evaluate the result of the expansion: (foo 'quux) =E2=87=92 Debugger entered--Lisp error: (void-function bar) On the other hand, calling =E2=80=98macroexpand=E2=80=99 on a data represen= tation of that form will just return the expansion result: (macroexpand '(foo 'quux)) =E2=87=92 (bar 'quux) In this example, I did not bother to define =E2=80=98bar=E2=80=99, so Elisp= assumes it would be a function and complains at evaluation time. But I could further define =E2=80=98bar=E2=80=99 as a macro: (defmacro bar (&rest body) `(baz ,@body)) In this case, evaluating the original form shows that Elisp expanded both macros =E2=80=98foo=E2=80=99 and =E2=80=98bar=E2=80=99, and then tried= to call the undefined function =E2=80=98baz=E2=80=99: (foo 'quux) =E2=87=92 Debugger entered--Lisp error: (void-function baz) Meanwhile, =E2=80=98macroexpand=E2=80=99 still just expands a single level = of macros: (macroexpand '(foo 'quux)) =E2=87=92 (bar 'quux) and you can invoke it repeatedly until you get to the fixed point: (macroexpand (macroexpand '(foo 'quux))) =E2=87=92 (baz 'quux) (macroexpand (macroexpand (macroexpand '(foo 'quux)))) =E2=87=92 (baz 'quux)