emacs-devel
[Top][All Lists]
Advanced

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

Re: Instead of pcase


From: Dmitry Gutov
Subject: Re: Instead of pcase
Date: Sun, 19 Nov 2023 19:59:42 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0

On 19/11/2023 18:02, Barry Fishman wrote:
On 2023-11-19 13:23:17 +01, Michael Heerdegen wrote:
So, the advantage would be more of a psychological kind: you have normal
Lisp function calls, but structurally you have the same thing, and you
have to remember exactly the same set of details.  The complexity comes
from the task, not from the implementation.

I submit that the opposite is true,  the complexity is in its
implementation, not the task.

In languages like Haskell this is expressed in a very simple way, so the
task is not in itself hard to understand.

Haskell is notable for using the same (or similar) syntax for destructuring as it uses for constructing expressions.

pcase actually follows that practice with backquotes and commas.

And in both matching and destructuring are done in the same expression (the presence of the latter is determined by whether a pattern contains variable bindindgs).

If x is a list with its first element a string "foo" and the second an
integer, remove it from x and add the second element to total:

   (proto-case x
     ((list* (string name) (integer count) (t rest))
      (string-equal name "foo")
      (set! total (+ total count))
      (set! x rest)))

This is just one of many other ways one could setup a matching
expression in elisp, the best is a balance between implementation
efficiency and readability.

And this divorces matching from destructuring. I suggest you try rewriting one of the more complex pcase usages into this syntax and see what people think of the result.



reply via email to

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