help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: pcase and minus-sign


From: tomas
Subject: Re: pcase and minus-sign
Date: Wed, 30 Nov 2016 14:39:05 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]