unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* pcase and minus-sign
@ 2016-11-30 12:12 Andreas Röhler
  2016-11-30 12:38 ` Joost Kremers
  0 siblings, 1 reply; 14+ messages in thread
From: Andreas Röhler @ 2016-11-30 12:12 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

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?

Thanks,

Andreas


(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"))))

;; GNU Emacs 25.1.90.1 (i686-pc-linux-gnu, GTK+ Version 3.14.5) of 
2016-11-29





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  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
  0 siblings, 1 reply; 14+ messages in thread
From: Joost Kremers @ 2016-11-30 12:38 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Help Gnu Emacs mailing list


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



-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 12:38 ` Joost Kremers
@ 2016-11-30 13:30   ` Andreas Röhler
  2016-11-30 13:31     ` Michael Heerdegen
                       ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Andreas Röhler @ 2016-11-30 13:30 UTC (permalink / raw)
  To: Joost Kremers; +Cc: Help Gnu Emacs mailing list



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.







^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 13:30   ` Andreas Röhler
@ 2016-11-30 13:31     ` Michael Heerdegen
  2016-11-30 13:39     ` tomas
  2016-11-30 13:56     ` Joost Kremers
  2 siblings, 0 replies; 14+ messages in thread
From: Michael Heerdegen @ 2016-11-30 13:31 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Joost Kremers, Help Gnu Emacs mailing list

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Thanks, that helps. Seems it relates to the following in docstring:
>
> SYMBOL    matches anything and binds it to SYMBOL.

Exactly.


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

It's not so strange anymore if you correct the message string ;-)


Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 13:30   ` Andreas Röhler
  2016-11-30 13:31     ` Michael Heerdegen
@ 2016-11-30 13:39     ` tomas
  2016-11-30 13:56     ` Joost Kremers
  2 siblings, 0 replies; 14+ messages in thread
From: tomas @ 2016-11-30 13:39 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 13:30   ` Andreas Röhler
  2016-11-30 13:31     ` Michael Heerdegen
  2016-11-30 13:39     ` tomas
@ 2016-11-30 13:56     ` Joost Kremers
  2016-11-30 15:23       ` Andreas Röhler
  2 siblings, 1 reply; 14+ messages in thread
From: Joost Kremers @ 2016-11-30 13:56 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Help Gnu Emacs mailing list


On Wed, Nov 30 2016, Andreas Röhler wrote:
> Thanks, that helps. Seems it relates to the following in 
> docstring:
>
> SYMBOL    matches anything and binds it to SYMBOL.

Yup.

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

I don't think it's strange, TBH. `a' is essentially a let-bound 
variable. Having the ability to bind values is extremely useful in 
pcase, I'd say.

It may be a bit surprising that - functions the same way, but it's 
not strange once you realise that - is not a special character in 
Lisp (unlike most other programming languages). For example, the 
following code works fine:

(let ((- "hi"))
  (message "%s" -))

Note BTW that the same if true for the underscore: in your 
example, _ is also just a symbol used to let-bind some value. The 
only special thing about _ is that the byte-compiler doesn't 
complain about an unused variable if its name starts with an 
underscore. But again, the following code works just fine:

(let ((_ "hi"))
  (message "%s" _))



-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  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
  0 siblings, 2 replies; 14+ messages in thread
From: Andreas Röhler @ 2016-11-30 15:23 UTC (permalink / raw)
  To: Joost Kremers; +Cc: Michael Heerdegen, Help Gnu Emacs mailing list



On 30.11.2016 14:56, Joost Kremers wrote:
>
> On Wed, Nov 30 2016, Andreas Röhler wrote:
>> Thanks, that helps. Seems it relates to the following in docstring:
>>
>> SYMBOL    matches anything and binds it to SYMBOL.
>
> Yup.
>
>> 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.
>
> I don't think it's strange, TBH. `a' is essentially a let-bound 
> variable. Having the ability to bind values is extremely useful in 
> pcase, I'd say.
>
> It may be a bit surprising that - functions the same way, but it's not 
> strange once you realise that - is not a special character in Lisp 
> (unlike most other programming languages). For example, the following 
> code works fine:
>
> (let ((- "hi"))
>  (message "%s" -))
>
> Note BTW that the same if true for the underscore: in your example, _ 
> is also just a symbol used to let-bind some value. The only special 
> thing about _ is that the byte-compiler doesn't complain about an 
> unused variable if its name starts with an underscore. But again, the 
> following code works just fine:
>
> (let ((_ "hi"))
>  (message "%s" _))
>
>
>

Hmm, thanks all.

But what make the char `a' so special WRT char `1'?:

(defun foo (arg)
   (interactive "P")
   (pcase arg
     (1 (message "%s" "ARG was `1'"))
     (a (message "%s" "ARG was `a'"))
     ('- (message "%s" "ARG was minus-sign"))
     (_ (message "%s" "ARG not minus-sign"))))



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 15:23       ` Andreas Röhler
@ 2016-11-30 15:34         ` Michael Heerdegen
  2016-11-30 15:48         ` Joost Kremers
  1 sibling, 0 replies; 14+ messages in thread
From: Michael Heerdegen @ 2016-11-30 15:34 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Joost Kremers, Help Gnu Emacs mailing list

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> But what make the char `a' so special WRT char `1'?:
>
> (defun foo (arg)
>   (interactive "P")
>   (pcase arg
>     (1 (message "%s" "ARG was `1'"))
>     (a (message "%s" "ARG was `a'"))
>     ('- (message "%s" "ARG was minus-sign"))
>     (_ (message "%s" "ARG not minus-sign"))))

The Lisp reader maps "a" to a symbol, but "1" to an integer.  As pcase
patterns, symbols and integers have a different meaning.

If pcase could only match against atoms, this would be strange.  But
pcase is not `case', and as already has been mentioned, being able to
use variables in patterns (for binding or equivalence tests) is very
useful.

If you want to test whether your ARG is `equal' to some VALUE, you can
always use 'VALUE as pattern.  This is very consistent and easy to
remember.  Anything not quoted has a different meaning as a pattern.
There is one exception: if VALUE is actually a keyword, an integer, or a
string, you can omit the quote (as you do above for "1") - but you don't
need to omit it.  Symbols that are not keywords are not included in this
list, because they already have a different meaning.

Michael.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  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
  1 sibling, 1 reply; 14+ messages in thread
From: Joost Kremers @ 2016-11-30 15:48 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: Michael Heerdegen, Help Gnu Emacs mailing list


On Wed, Nov 30 2016, Andreas Röhler wrote:
> But what make the char `a' so special WRT char `1'?:
>
> (defun foo (arg)
>    (interactive "P")
>    (pcase arg
>      (1 (message "%s" "ARG was `1'"))
>      (a (message "%s" "ARG was `a'"))
>      ('- (message "%s" "ARG was minus-sign"))
>      (_ (message "%s" "ARG not minus-sign"))))

The same thing that makes it different in "normal" Lisp code. 1 is 
read as an integer, not as a symbol.

-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-11-30 15:48         ` Joost Kremers
@ 2016-12-01 13:59           ` Andreas Röhler
  2016-12-02  2:30             ` Stefan Monnier
  0 siblings, 1 reply; 14+ messages in thread
From: Andreas Röhler @ 2016-12-01 13:59 UTC (permalink / raw)
  To: Joost Kremers; +Cc: Michael Heerdegen, Help Gnu Emacs mailing list



On 30.11.2016 16:48, Joost Kremers wrote:
>
> On Wed, Nov 30 2016, Andreas Röhler wrote:
>> But what make the char `a' so special WRT char `1'?:
>>
>> (defun foo (arg)
>>    (interactive "P")
>>    (pcase arg
>>      (1 (message "%s" "ARG was `1'"))
>>      (a (message "%s" "ARG was `a'"))
>>      ('- (message "%s" "ARG was minus-sign"))
>>      (_ (message "%s" "ARG not minus-sign"))))
>
> The same thing that makes it different in "normal" Lisp code. 1 is 
> read as an integer, not as a symbol.
>

Thanks all again!




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-12-01 13:59           ` Andreas Röhler
@ 2016-12-02  2:30             ` Stefan Monnier
  2016-12-03  8:04               ` Andreas Röhler
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2016-12-02  2:30 UTC (permalink / raw)
  To: help-gnu-emacs

>>> But what make the char `a' so special WRT char `1'?:
>>> 
>>> (defun foo (arg)
>>> (interactive "P")
>>> (pcase arg
>>> (1 (message "%s" "ARG was `1'"))
>>> (a (message "%s" "ARG was `a'"))
>>> ('- (message "%s" "ARG was minus-sign"))
>>> (_ (message "%s" "ARG not minus-sign"))))
>> 
>> The same thing that makes it different in "normal" Lisp code. 1 is read as
>> an integer, not as a symbol.

> Thanks all again!

BTW, the driving idea behind the shape of pcase patterns is more or less
that a pattern PAT matches the value V if an expression PAT could
evaluate to the value V.

So for the pattern 1, it will match any value that is equal to the
evaluation of the expression 1, i.e. it only matches the value 1.

For pattern 'a, it will match any value that is equal to the evaluation
of the expression 'a, which means it the value has to be the symbol `a`.

Whereas for pattern a, it will match any value that can be equal to the
evaluation of the expression a, which means any value (under the
assumption that the variable `a` has the appropriate value).


        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-12-02  2:30             ` Stefan Monnier
@ 2016-12-03  8:04               ` Andreas Röhler
  2016-12-03 15:43                 ` Stefan Monnier
  0 siblings, 1 reply; 14+ messages in thread
From: Andreas Röhler @ 2016-12-03  8:04 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: Stefan Monnier



On 02.12.2016 03:30, Stefan Monnier wrote:
>>>> But what make the char `a' so special WRT char `1'?:
>>>>
>>>> (defun foo (arg)
>>>> (interactive "P")
>>>> (pcase arg
>>>> (1 (message "%s" "ARG was `1'"))
>>>> (a (message "%s" "ARG was `a'"))
>>>> ('- (message "%s" "ARG was minus-sign"))
>>>> (_ (message "%s" "ARG not minus-sign"))))
>>> The same thing that makes it different in "normal" Lisp code. 1 is read as
>>> an integer, not as a symbol.
>> Thanks all again!
> BTW, the driving idea behind the shape of pcase patterns is more or less
> that a pattern PAT matches the value V if an expression PAT could
> evaluate to the value V.

Must confess some hardship of understanding. If an expression may 
evaluate to a certain value or not, that depends resp. may depend on the 
state at run-time. Can't see how that makes sense here.

>




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-12-03  8:04               ` Andreas Röhler
@ 2016-12-03 15:43                 ` Stefan Monnier
  2016-12-05  9:11                   ` Andreas Röhler
  0 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2016-12-03 15:43 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: help-gnu-emacs

> Must confess some hardship of understanding. If an expression may evaluate
> to a certain value or not, that depends resp. may depend on the state at
> run-time.

No: whether it *will* evaluate to that value depends on the state at
run-time.  But whether it *can* evaluate to it doesn't.


        Stefan



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: pcase and minus-sign
  2016-12-03 15:43                 ` Stefan Monnier
@ 2016-12-05  9:11                   ` Andreas Röhler
  0 siblings, 0 replies; 14+ messages in thread
From: Andreas Röhler @ 2016-12-05  9:11 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs



On 03.12.2016 16:43, Stefan Monnier wrote:
>> Must confess some hardship of understanding. If an expression may evaluate
>> to a certain value or not, that depends resp. may depend on the state at
>> run-time.
> No: whether it *will* evaluate to that value depends on the state at
> run-time.  But whether it *can* evaluate to it doesn't.
>
>
>          Stefan

At any case permits a much nicer implementation of complement-char than 
done via plain cond, thanks.

(defun general-close--return-complement-char-maybe (erg)
   "For example return \"}\" for \"{\" but keep \"\\\"\". "
   (pcase erg
     (34 ?\")
     (?' ?')
     (?\( ?\))
     (?\) ?\()
     (?\] ?\[)
     (?\[ ?\])
     (?} ?{)
     (?{ ?})))




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2016-12-05  9:11 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).