emacs-devel
[Top][All Lists]
Advanced

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

Re: pcase defuns


From: Andrew Hyatt
Subject: Re: pcase defuns
Date: Tue, 29 Mar 2022 21:28:00 -0400


On Mon, Mar 28, 2022 at 12:15 AM Richard Stallman <rms@gnu.org> wrote:
[[[ 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. ]]] > To give a flavor of what it looks like in practice, here's a > few pattern defuns from the test: > (defun-pattern fibonacci > "Compute the fibonacci sequence." > ((0) 0) ((1) 1) ((n) > (+ (fibonacci (- n 1)) > (fibonacci (- n 2))))) For defining how to compute the value, it is clear and simple. (Do the patterns handle decomposing lists and other compound objects?)

Yes, it does this.

But defining how to compute the value is not the only job a defun needs to do. It also needs to record argument names.

The way I look at it, it does do this, but it's just structured differently. The normal defun as one arglist. Mine has one per matching clause, which means that it can take a variety of different arguments, all matching. And the arglist and the matching clauses are the same thing, so the arglist can be (n), or ('foo n), or (1 2 (3 n)), etc. Yes, it's weird, but I think the differentiation here is useful, see my next point.

Also, it doesn't seem to offer a way to make the function accept other arguments beyond the one that the pattern will be applied to. So I think it should take an argument list, and bind all the argument variables, like this: (defun-pattern fibonacci (n) "Compute the Nth fibonacci number." ((0) 0) ((1) 1) (t (+ (fibonacci (- n 1)) (fibonacci (- n 2))))) (defun-pattern sum (x y) "Compute the sum of natural numbers X and Y." ((0) y) (t (1+ (add (1- x) y))))


Thank you for the feedback and your examples.

There's a tension here: if I make this fit more with existing patterns, it becomes something that seems closer to just a defun with a pcase in it. Maybe a bit cleaner. In particular, with your proposal, we lose the ability to have fairly different arg patterns, with different numbers of args, or different destructuring patterns. However, I admit that my proposal, without a single arglist, is odd, and may be seen as too esoteric by most.

I'm curious to hear more opinions.


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