[Top][All Lists]

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

Re: The poor state of documentation of pcase like things.

From: Michael Heerdegen
Subject: Re: The poor state of documentation of pcase like things.
Date: Sat, 19 Dec 2015 16:55:12 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

> But the example doesn't use `,foo`, it uses just ,foo.  I guess `..`
> here is some markdown-style "quoting", unrelated to the Lisp
> backticks?


> "Local symbol" here meaning what? an un-interned symbol or a local
> variable by that name?

It binds to the symbol specified.  The type of binding is local.

> >     (pcase value
> >       (`(1 2 ,foo 3)
> >        (message "Matched 1, 2, something now bound to foo, and 3"))
> >       (foo
> >        (message "Match anything at all, and bind it to foo!"))
> >       (`(,the-car . ,the-cdr))
> >        (message "Match any cons cell, binding the car and cdr locally"))
> So to bind something to 'foo' you just use "foo", but to bind
> something to 'the-car' and 'the-cdr' you need to use ",the-car" and
> ",the-cdr"?  Why the inconsistency?

The examples only differ because some symbols occur inside backquote and
others don't.

> > The reason for doing this is two-fold: Either to refer to a previous match
> > later in the pattern (where it is compared using `eq'), or to make use of a
> > matched value within the related code block:
> > 
> >     (pcase value
> >       (`(1 2 ,foo ,foo 3)
> >        (message "Matched (1 2 %s %s 3)" foo)))
> ??? Is "foo" here bound to 2 different values?  And how come the
> format has 2 %s, but only one variable, foo, to provide values?

The second occurrence is turned into an equivalence test (the pcase
docstring does already say that).  The message call is invalid.

> > We can express boolean logic within a pattern match using the `or` and `and`
> > Patterns:
> > 
> >     (pcase value
> >       (`(1 2 ,(or 3 4)
> >          ,(and (pred stringp)
> >                (pred (string> "aaa"))
> >                (pred (lambda (x) (> (length x) 10)))))
> >        (message "Matched 1, 2, 3 or 4, and a long string "
> >                 "that is lexically greater than 'aaa'")))
> Why did you use 'lambda' for the 3rd predicate, but not for the 2nd?
> Is it just a way to show off use of 'lambda', or is there some
> significant difference between these 2 use cases that requires a
> 'lambda' in the latter case?  More generally, when is 'lambda'
> required in a predicate like these ones?

That's explained in the pcase docstring.

> Isn't it true that pcase-let is just a short-hand for a pcase that
> assigns values according to patterns, and has nil as the default
> value?  If that's true, I think it explains better what pcase-let
> does, especially when backed up by an example of a pcase and the
> equivalent pcase-let.

I think there are more differences, AFAICT pcase-let can't "fail", and
it can bind multiple value--pattern pairs.


reply via email to

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