[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: |
Wed, 22 Nov 2023 21:58:54 -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've done more work on designing `cond*'; here is what I have now.
Please send me constructive suggestions, including for features you
suggest adding.
(cond*
;; Same as a clause in `cond',
(CONDITION
do-this-if-CONDITION-then-exit...)
;; Variables to bind, as in let
(:bind (x foobar) y z (foo 5) a)
;; Bindings continue in effect.
;; t as condition means run this clause unconditionally
;; then always continue with next clause.
(t do-this-unconditionally-and-dont-stop...)
;; Extracts substructure and binds variables around the rest
;; of the cond*.
(:match (`(expt ,foo ,bar) x)
do-this-if-it-matched-then-exit...)
;; Bindings continue in effect.
;; Like above but always falls thru to next clause.
(:match (`(expt ,foo ,bar) x))
;; Bindings continue in effect.
;; t in the last clause means the same as in previous clauses,
;; but you can think of it as being the same as in `cond'.
(t do-this-as-last-resort...)
)
Here are three macros that would be useful as conditions in `cond*'
but can also be used elsewhere.
(match PATTERN VALUE)
This is a macro.
PATTERN is a backquote form which specifies which parts of
VALUE should be fixed. For instance
(match `(+ (expt nil nil)) (get-complex-list))
would decompose the result of (get-complex-list) and check
for `+' and `expt' in it.
PATTERN will not be evaluated, just used as guidance when the
`match' form is macroexpanded.
If PATTERN includes substitutions, it will virtually perform
those substitutions and compare parts of VALUE against them.
(match `(foo ,foo) x) compares (car x) with `foo' and
compares (cadr x) with the value of foo.
`match' can also handle vectors and maybe some other kinds of
structures.
(match-set PATTERN VALUE)
This is a macro.
PATTERN is a backquote form which specifies which parts of
VALUE to match and parts to extract and store. For instance
(match-set `(+ ,y ,z) (get-complex-list))
would decompose the result of (get-complex-list), verify the car,
and set y and z.
PATTERN will not be evaluated, just used as guidance when the
`match-set' form is macroexpanded, producing code like this:
(if (eq (car value) '+)
(progn
(setq y (car (cadr value)) z (cadr (cadr value)))
t))
`match' can also handle vectors and maybe some other kinds of
structures.
(match-bind PATTERN VALUE BODY...)
Much like `match-set', except that instead of setting the
variables found in PATTERN, it let-binds them and runs BODY
with those bindings.
--
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)
- Re: combining cond and let, to replace pcase., (continued)
- Re: combining cond and let, to replace pcase., Gerd Möllmann, 2023/11/19
- Re: combining cond and let, to replace pcase., Tomas Hlavaty, 2023/11/19
- Re: combining cond and let, to replace pcase., Stefan Monnier, 2023/11/24
- Re: combining cond and let, to replace pcase., Alan Mackenzie, 2023/11/19
- Re: combining cond and let, to replace pcase., Michael Heerdegen, 2023/11/21
- Re: combining cond and let, to replace pcase., Axel Forsman, 2023/11/19
- Re: combining cond and let, to replace pcase., Michael Heerdegen, 2023/11/21
- Re: combining cond and let, to replace pcase.,
Richard Stallman <=
- Re: combining cond and let, to replace pcase., Tomas Hlavaty, 2023/11/23
- Re: combining cond and let, to replace pcase., Richard Stallman, 2023/11/25
- Re: combining cond and let, to replace pcase., Tomas Hlavaty, 2023/11/27
- Re: combining cond and let, to replace pcase., Yuri Khan, 2023/11/27
- Re: combining cond and let, to replace pcase., Tomas Hlavaty, 2023/11/28
- Re: combining cond and let, to replace pcase., Manuel Giraud, 2023/11/23
- Re: combining cond and let, to replace pcase., Richard Stallman, 2023/11/25
- Re: combining cond and let, to replace pcase., Daniel Semyonov, 2023/11/23
- Re: combining cond and let, to replace pcase., Daniel Semyonov, 2023/11/23
- Re: combining cond and let, to replace pcase., Richard Stallman, 2023/11/24