unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Using let-bound var inside pcase conditions
@ 2017-04-17 18:07 Kaushal Modi
  2017-04-17 20:33 ` Michael Heerdegen
  0 siblings, 1 reply; 3+ messages in thread
From: Kaushal Modi @ 2017-04-17 18:07 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi,

I am trying to set the conditions in a pcase to variables, but cannot get
it working (See below dummy code).

Can you help fix the second message form below?

;; -*- lexical-binding: t; -*-

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

    (message "(good) Value is %s." (pcase var
                                     (`nil "nil")
                                     (1 "one")
                                     (2 "two")
                                     (_ "none of the expected")))
    (message "(bad)  Value is %s." (pcase var
                                     (`nil "nil")
                                     (`,one "one")
                                     (`,two "two")
                                     (_ "none of the expected")))))
(pcase-test nil)
(pcase-test 1)
(pcase-test 2)
(pcase-test 3)

-- 

Kaushal Modi


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using let-bound var inside pcase conditions
  2017-04-17 18:07 Using let-bound var inside pcase conditions Kaushal Modi
@ 2017-04-17 20:33 ` Michael Heerdegen
  2017-04-17 20:40   ` Kaushal Modi
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Heerdegen @ 2017-04-17 20:33 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Help Gnu Emacs mailing list

Kaushal Modi <kaushal.modi@gmail.com> writes:

>     (message "(bad)  Value is %s." (pcase var
>                                      (`nil "nil")
>                                      (`,one "one")
>                                      (`,two "two")
>                                      (_ "none of the expected")))))

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.

To your question: I guess the shortest correct pattern is

  (pred (eq one))


Regards,

Michael.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Using let-bound var inside pcase conditions
  2017-04-17 20:33 ` Michael Heerdegen
@ 2017-04-17 20:40   ` Kaushal Modi
  0 siblings, 0 replies; 3+ messages in thread
From: Kaushal Modi @ 2017-04-17 20:40 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: Help Gnu Emacs mailing list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-04-17 20:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-17 18:07 Using let-bound var inside pcase conditions Kaushal Modi
2017-04-17 20:33 ` Michael Heerdegen
2017-04-17 20:40   ` Kaushal Modi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).