emacs-devel
[Top][All Lists]
Advanced

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

Re: cond*


From: Ihor Radchenko
Subject: Re: cond*
Date: Sat, 03 Feb 2024 16:52:54 +0000

Richard Stallman <rms@gnu.org> writes:

>   > (let ((list-of-three '(1 2)))
>   >   (cond* ((match* `(,x ,y ,z) list-of-three))
>   >          ((< 2 (+ x y z))
>   >           'success))) ; => (wrong-type-argument number-or-marker-p nil)
>
> That is not exactly a bug, but it is an unclarity in the design.
> ...

I think that we are drifting away from the original topic I raised in this
discussion branch.

I originally pointed to a missing feature of cond* - inability to match
against multiple values as in

     (defun yant/list-sum (list)
       (apply #'+ list))
     (pcase '(1 2 3)
       ((and `(,x ,y ,z) (app yant/list-sum (and sum (guard (< 2 sum)))))
        (format "%d + %d + %d = %d; 2 is < %d" x y z sum sum)))
     ; => "1 + 2 + 3 = 6; 2 is < 6"   

You suggested how the same can be done with cond*. However, your example
does not work, throwing wrong-type-argument, when the first match fails.
So, I still view the above scenario as a missing feature in cond*.

What I imagine as an addition to cond* is something like:

       (let ((list-of-three '(1 2)))
         (cond* ((and (match* `(,x ,y ,z) list-of-three)
                     (match* (constrain x (< 2 x)) (yant/list-sum 
list-of-three)))
                 'success)))

This way, multiple values can be matched against.

> But it raises the question of what a non-exit match* clause should do
> when it does not match.  What do you think it should do?
>
> The idea that occurs to me is this: bind all those variables,
> initializing by matching those that can match, and initializing the
> rest to nil.

I'd rather make cond* throw an error in such scenario.
Otherwise, it will be impossible to distinguish between "no match" and
something like

(let ((list-of-three '(nil nil nil)))
     (cond* ((match* `(,x ,y ,z) list-of-three)
             ((and (null x) (null y) (null z))
               "We matched '(nil nil nil), or did we?"))))

(let ((list-of-three 'not-a-list))
     (cond* ((match* `(,x ,y ,z) list-of-three)
             ((and (null x) (null y) (null z))
               "We matched '(nil nil nil), or did we?"))))

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



reply via email to

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