From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Newsgroups: gmane.emacs.help Subject: Re: pcase and minus-sign Date: Wed, 30 Nov 2016 14:39:05 +0100 Message-ID: <20161130133905.GA21731@tuxteam.de> References: <87r35tb0se.fsf@fastmail.fm> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; x-action=pgp-signed Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1480513431 4618 195.159.176.226 (30 Nov 2016 13:43:51 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 30 Nov 2016 13:43:51 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 30 14:43:47 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cC5Ao-0000Tv-GW for geh-help-gnu-emacs@m.gmane.org; Wed, 30 Nov 2016 14:43:46 +0100 Original-Received: from localhost ([::1]:44226 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cC5As-0004V5-BI for geh-help-gnu-emacs@m.gmane.org; Wed, 30 Nov 2016 08:43:50 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:33144) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cC56U-0001DU-GP for help-gnu-emacs@gnu.org; Wed, 30 Nov 2016 08:39:19 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cC56Q-0006qg-J7 for help-gnu-emacs@gnu.org; Wed, 30 Nov 2016 08:39:18 -0500 Original-Received: from mail.tuxteam.de ([5.199.139.25]:54445 helo=tomasium.tuxteam.de) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cC56Q-0006mj-Cm for help-gnu-emacs@gnu.org; Wed, 30 Nov 2016 08:39:14 -0500 Original-Received: from tomas by tomasium.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1cC56H-0005kY-Ug for help-gnu-emacs@gnu.org; Wed, 30 Nov 2016 14:39:06 +0100 In-Reply-To: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 5.199.139.25 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.help:111833 Archived-At: -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wed, Nov 30, 2016 at 02:30:11PM +0100, Andreas Röhler wrote: > > > On 30.11.2016 13:38, Joost Kremers wrote: > > > >On Wed, Nov 30 2016, Andreas Röhler wrote: > >>Hi, > >> > >>see code below. With numerical argument "1", first pattern is > >>matched as expected. > >> > >>However without arg the minus is matched - the second pattern, > >>not the default "_" at last. > >> > >>Any explanation? > >> > >>(defun foo (arg) > >> (interactive "P") > >> (pcase arg > >> (1 (message "%s" "ARG was `1'")) > >> (- (message "%s" "ARG was minus-sign")) > >> (_ (message "%s" "ARG not minus-sign")))) > > > >Probably because - is a symbol and hence a variable. It works if > >you quote it: > > > >(defun foo (arg) > > (interactive "P") > > (pcase arg > > (1 (message "%s" "ARG was `1'")) > > ('- (message "%s" "ARG was minus-sign")) > > (_ (message "%s" "ARG not minus-sign")))) > > > > > > > > Thanks, that helps. Seems it relates to the following in docstring: > > SYMBOL matches anything and binds it to SYMBOL. > > Now if I use some arbitrary char, like "a", > > (defun foo (arg) > (interactive "P") > (pcase arg > (a (message "%s" "ARG was `a'")) > (1 (message "%s" "ARG was `1'")) > ('- (message "%s" "ARG was minus-sign")) > (_ (message "%s" "ARG not minus-sign")))) > > It picks that a-branch at any case - as documented but strange. On the contrary -- that's what makes pcase so enticing. You can match *and* bind parts of the match at the same time As in (pcase arg ... `(,x . ,y) ; match a pair (message "Matched a pair: the car is %S and the cdr is %S" x y)) ... Note how the symbols in the pattern function as placeholders to pick up whatever is in the matched structure. In the case above, if you feed it ('foo . 123) as "arg", x will be bound to 'foo, 123 to bar). regards - -- t -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAlg+1nkACgkQBcgs9XrR2kZUQwCfe0xA9UnxycPgTBUF+rAY7GA0 FucAn1EYDTDoNUbLepRs5it/m6+1xRpn =7X8T -----END PGP SIGNATURE-----