all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: <tomas@tuxteam.de>
To: help-gnu-emacs@gnu.org
Subject: Re: pcase and minus-sign
Date: Wed, 30 Nov 2016 14:39:05 +0100	[thread overview]
Message-ID: <20161130133905.GA21731@tuxteam.de> (raw)
In-Reply-To: <ae445475-96bc-fcd8-0a51-0b2236eb896a@easy-emacs.de>

-----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-----



  parent reply	other threads:[~2016-11-30 13:39 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-30 12:12 pcase and minus-sign Andreas Röhler
2016-11-30 12:38 ` Joost Kremers
2016-11-30 13:30   ` Andreas Röhler
2016-11-30 13:31     ` Michael Heerdegen
2016-11-30 13:39     ` tomas [this message]
2016-11-30 13:56     ` Joost Kremers
2016-11-30 15:23       ` Andreas Röhler
2016-11-30 15:34         ` Michael Heerdegen
2016-11-30 15:48         ` Joost Kremers
2016-12-01 13:59           ` Andreas Röhler
2016-12-02  2:30             ` Stefan Monnier
2016-12-03  8:04               ` Andreas Röhler
2016-12-03 15:43                 ` Stefan Monnier
2016-12-05  9:11                   ` Andreas Röhler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161130133905.GA21731@tuxteam.de \
    --to=tomas@tuxteam.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.