emacs-devel
[Top][All Lists]
Advanced

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

Re: combining cond and let, to replace pcase.


From: Richard Stallman
Subject: Re: combining cond and let, to replace pcase.
Date: Tue, 28 Nov 2023 22:41:41 -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. ]]]

  >     (pcase 42
  >       (`(,x . ,y) (format "%s:%s" x y))
  >       (_ "not a cons cell"))
  >     → not a cons cell

  > If I understand Richard correctly,

Alas I think we are miscommunicating.

                                       the corresponding :match will
  > unconditionally attempt to bind x to (car 42) and y to (cdr 42), which
  > results in a run-time error.

pcase has one kind of matching clause.
cond* has two kinds.

It is possible that this distinction is not clear enough in the text I
posted.

In this version, each clause exits if it contains more than one
element (after the possible keyword), and falls through
unconditionally if it has only one element (after the possible
keyword).

For the second kind, I think this addresses your concern.

  (cond* 
       ;; Same as a clause in `cond',
       (CONDITION
        DO-THIS-IF-TRUE-THEN-EXIT...)

       ;; Execute FORM and ignore its value.
       (FORM)

       ;; Variables to bind, as in let
       (:bind ((x foobar) y z (foo 5) a))
       ;; Bindings continue in effect.

       ;; Extracts substructure and binds variables around the rest
       ;; of the cond*.
       (:match (`(expt ,foo ,bar) x)
        DO-THIS-IF-IT-MATCHED-THEN-EXIT...)
       ;; Bindings no longer in effect.

       ;; Like above but always falls thru to next clause.
       (:match (`(expt ,foo ,bar) x))
       ;; Bindings continue in effect.
       ;; All the variables mentioned in the pattern
       ;; are bound whether match succeeds or not.
       ;; If a value can be determined from an incomplete match,
       ;; the variable gets 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]