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: Fri, 22 Dec 2023 21:53:34 -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. ]]]

  > Or what if TESTFN needs to receive multiple arguments bound from a
  > matched pattern?  What if TESTFN needs 3 arguments, and the middle one
  > is not from the matched pattern?  Or what if the middle one /is/ from
  > the matched pattern?

Thank you.  I did not realize that my description was unclear in this way.
I've done a lot of rewriting; I hope this version is clearer.


Constrained variable: (PRED VARIABLE)  or, more generally,
                      (PRED VARIABLE OTHER-ARGS...)

  This matches any value VALUE that satisfies a specified constraint.
  PRED is a function to test the constraint.  It receives VALUE as an
  argument.  If PRED returns true, that means VALUE matches the
  constraint, so this pattern binds (or sets) VARIABLE to that value.
  For instance,

     (symbolp sym)   ; Match any symbol, bind `sym' to it.

  If you wish, you can specify additional arguments to pass to PRED.
  The OTHER-ARGS are not patterns to match, they are Lisp expressions
  to whose values specify the additional arguments to pass to PRED,
  following the first argument which is VALUE.  In effect, the call
  to PRED looks like this:
      (apply PRED VALUE (mapcar 'eval OTHER-ARGS))

  For example,
 
     (> num-foos 1)  ; Match any number greater than 1, bind `num-foos' to it.
     (> num-foos min-foos)  ; Match any number greater than MIN-FOOS,
                            ;  bind `num-foos' to it.

  When matching to bind variables, the presence of this kind of
  subpattern in the overall pattern to be matched unconditionally
  binds VARIABLE whether the subpattern matches or not.

  Constrained variable constructs can be nested

  For example,

    (< (integerp num-foos) 1)  ; Match any integer number > 1,
                               ;  bind `num-foos' to it.

  Advanced hack for experts: the OTHER-ARGS can refer to variables
  bound earlier in this pattern-matching operation.  For example,

  `(,(integerp smaller) ,(> (integerp bigger) smaller))
     ; Matches a list of two integers in which the second integer
     ; is larger than the first.

  (or `(,(integerp smaller) ,(> (integerp bigger) smaller))
      `(,(integerp bigger) ,(< (integerp smaller) bigger)))
     ; matches a ist of two integers and binds `bigger' to the bigger one
     ; and `smaller' to the smaller one.

  However, it might be cleaner to match the two integers regardless of
  their numberical ordering, then in Lisp code see which one is larger.

General constrained variable: (constrain VAR EXPRESSION)

  This general constrained variable pattern binds VAR to the
  value being matched against, tentatively, then evaluates EXPRESSION.
  If the result is true, the match succeeds and leaves VAR
  bound to that value.

  For instance,

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

  succeeds if the value being matched aainst is in the open interval (0, 100),
  and in that case it binds x to that value.

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