From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: How the backquote and the comma really work? Date: Fri, 26 Jun 2015 09:31:35 +0200 Message-ID: <20150626073135.GA6552@tuxteam.de> References: <87vbebg1fs.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1435303925 3947 80.91.229.3 (26 Jun 2015 07:32:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 26 Jun 2015 07:32:05 +0000 (UTC) Cc: Help Gnu Emacs mailing list To: Marcin Borkowski Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jun 26 09:32:01 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z8O7E-0000kR-Fe for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Jun 2015 09:32:00 +0200 Original-Received: from localhost ([::1]:58656 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z8O7D-00037m-Qw for geh-help-gnu-emacs@m.gmane.org; Fri, 26 Jun 2015 03:31:59 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58653) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z8O71-00037S-Q6 for help-gnu-emacs@gnu.org; Fri, 26 Jun 2015 03:31:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z8O6w-0005xg-UD for help-gnu-emacs@gnu.org; Fri, 26 Jun 2015 03:31:47 -0400 Original-Received: from mail.tuxteam.de ([5.199.139.25]:41696 helo=tomasium.tuxteam.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z8O6w-0005uu-OC for help-gnu-emacs@gnu.org; Fri, 26 Jun 2015 03:31:42 -0400 Original-Received: from tomas by tomasium.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1Z8O6q-0001yy-0P; Fri, 26 Jun 2015 09:31:36 +0200 In-Reply-To: <87vbebg1fs.fsf@mbork.pl> User-Agent: Mutt/1.5.21 (2010-09-15) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 5.199.139.25 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105190 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, Jun 25, 2015 at 07:09:11PM +0200, Marcin Borkowski wrote: > Hi all, > > I decided that the time has come that I finally approach the scary > backquote-comma duo. (While I understand it superficially, I’d like to > get it right and thoroughly this time.) So my question is whether my > mental model (see below) is correct. > > So, I assume that when Emacs Lisp interpreter encounters a backquote, it > looks at the expression after it. If it is anything but a list, it just > works like the usual quote, and the backquoted expression evaluates to > what was backquoted. > > If it is a list, its element are read and scanned. If any part of the > list (probably a nested one) begins with a comma, the whole thing after > the comma (be it a symbol, a list or whatever) is evaluated as usual, > and the result is put into the resulting list. It's *always* read as an S-expression (i.e. either a symbol, a string, a couple of more things, or a pair (thus, a list too). Thus something like `(bla bli would be an unfinished expression. To put a slightly different slant than the other very good answers on it, backquote is Lisp's take on the shell's, Perl's, Pythons "variable interpolation". On those languages it operates on strings, in Lisp it operates on S-expressions. Where in Perl you might say: my $amount=200; my $currency="dollars"; print("You owe me $amount $currency\n"); => You owe me 200 dollars in Lisp you think in S-expressions. Somewhat equivalent would be (setq amount 200) (setq currency 'dollars) ; use a symbol, just for kicks (print `(You owe me ,amount ,currency)) => (You owe me 200 dollars) Of course, print shows the surrounding parentheses because the result is a list in this case (an S-expression in general). The whole magic of ` and , comes because you can "unquote" whole sub-expressions: think `(you owe me ,(* amount 1.1) ,currency) and because you can nest the whole thing (unquote within quote within unquote ...). It is just a minimalistic, but complete template language, in classical Lisp tradition. (and if you have a canonical transformation of e.g. S-expressions to HTML, it's much more fun to write HTML templates in than the usual template languages). Now how this can be used to transform source code (i.e. "write macros") is left as an exercise to the reader ;-) Regards - -- t -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAlWM/9cACgkQBcgs9XrR2kYOFACeMkeLRxMLJHBrJ/n71ErVkZy1 QFIAmwcUEneXbHY+zcbmCDsTGRl97TM8 =AVmD -----END PGP SIGNATURE-----