help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Using let-bound var inside pcase conditions


From: Kaushal Modi
Subject: Re: Using let-bound var inside pcase conditions
Date: Mon, 17 Apr 2017 20:40:19 +0000

On Mon, Apr 17, 2017 at 4:33 PM Michael Heerdegen <michael_heerdegen@web.de>
wrote:

> I also try this from time to time just to remember that it's wrong: the
> first appearance of a symbol in a pcase pattern is never transformed
> into an `eq'uality test.  Sometimes that's surprising (especially in
> backquote patterns), but that's how it's defined.
>
> BTW, `nil is equivalent to 'nil, and `,symbol is equivalent to just
> symbol as a pattern.
>

Thanks! I did not know that.


> To your question: I guess the shortest correct pattern is
>
>   (pred (eq one))
>

Works! Thank you.

Here the code again for completeness (or my future-self ending up on this
thread while searching the Intrawebs):

;; -*- lexical-binding: t; -*-
;; http://lists.gnu.org/archive/html/help-gnu-emacs/2017-04/msg00123.html

(defun pcase-test (var)
  (let ((one 1)
        (two 2))

    (message "(hard-coded)  Value is %s." (pcase var
                                            (`nil "nil")
                                            (1 "one")
                                            (2 "two")
                                            (_ "none of the expected")))
    (message "(vars - bad)  Value is %s." (pcase var
                                            (`nil "nil")
                                            (`,one "one")
                                            (`,two "two")
                                            (_ "none of the expected")))
    (message "(vars - good) Value is %s." (pcase var
                                            (`nil "nil")
                                            ((pred (eq one)) "one")
                                            ((pred (eq two)) "two")
                                            (_ "none of the expected")))))

(pcase-test nil)
(pcase-test 1)
(pcase-test 2)
(pcase-test 3)

-- 

Kaushal Modi


reply via email to

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