emacs-devel
[Top][All Lists]
Advanced

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

Re: Instead of pcase


From: Richard Stallman
Subject: Re: Instead of pcase
Date: Tue, 12 Dec 2023 23:58:20 -0500

[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

I was compelled to copy pcase's `_' meaning "match anything" because I
could not see a way to do this with anything less arbitrary.
If anyone sees a cleaner way to do this, please tell me.

Here is the current definition of patterns for use in cond*.


**Possible types of patterns for :match and the match-... functions**

CONSTANT: nil, t, a keyword, a quoted constant, fixed parts of a backquote

  This matches any value equal to CONSTANT.
_

  _ means to match any value and not store it anywhere.

VARIABLE: a symbol

  A symbol means to match any value and set VARIABLE to it.

A quoted cons cell

  The car and the cdr are subpattenrs.  The cons cell pattern
  matches any cons cell whose car and cdr match those two subpatterns.
  How nil as a cdr matches is controlled by the match-nil flag.

  When the nil-match-all flag is false, nil as a cdr matches
  only nil itself.

  When the nil-match-all flag is true, nil as a cdr matches
  any object and ignores that object.

  The nil-match-all flag is false by default.  The `cdr' pattern maks
  it false within its its subpattern, and the `cdr-safe' pattern makes
  it true forwithin its its subpattern.

(cdr-safe QUOTED-CONS-CELL)

  This pattern is equivalent to QUOTED-CONS-CELL by itself
  except that it makes the nil-match-all flag true within it.

     (cdr-safe `(a b))  matches (a b), (a b c), (a b . d), etc.

(cdr QUOTED-CONS-CELL)

  This pattern is equivalent to QUOTED-CONS-CELL by itself
  except that it makes the nil-match-all flag false within it.

     (cdr `(a b))  matches only (a b).

Constrained variable: (TESTFN VARIABLE OTHER-ARGS...)

  This matches any value VALUE provided (TESTFN VALUE OTHER-ARGS...)
  evaluates to true.  If so, it sets VARIABLE to VALUE.

     (symbolp sym)    Match any symbol, store it in `sym'.
     (> num-foos 1)   Match any thing greater than 1, store it in `num-foos'.

  When matching to bind variables, this pattern unconditionally binds
  VARIABLE whether it matches or not.

  `quote', `constrain' and `or' cannot be used as TESTFN.

  Constrained variable constructs can be nested.

  For example,

    (< (numberp num-foos) 1)  Match any number > 1,  store it in mum-foos.

General constrained variable: (constrain VAR EXPRESSION)

  This general constrained variable pattern binds VAR to the
  value being matched against, only for evaluating EXPRESSION.
  If the result is non-nil, the match succeeds
  and sets VAR to the value.

  For instance,

    (constrain x (and (> x 0) (< x 100)))

Alternatives: (or SUBPATTERNS...)

  This tries to match each of the SUBPATTERNS in order until one matches.
  If the pattern is being used to bind variables, it binds all the variables
  specified in any of SUBPATTERNS

-- 
Dr Richard Stallman (https://stallman.org)
Chief GNUisance of the GNU Project (https://gnu.org)
Founder, Free Software Foundation (https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)





reply via email to

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