unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* comparator in match pattern
@ 2023-06-28  8:44 Damien Mattei
  2023-06-28  8:55 ` Vivien Kraus
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Mattei @ 2023-06-28  8:44 UTC (permalink / raw)
  To: guile-user

hello

does it exists in Guile something like == in Racket,
documented here:
https://docs.racket-lang.org/reference/match.html#%28form._%28%28lib._racket%2Fmatch..rkt%29._~3d~3d%29%29

that allows some code like that:

(match (list container index1-or-keyword index2-or-keyword)

          ((list c (== /) (== /)) (displayln "T[/ /]"))
          ((list c i1 (== /)) (displayln "T[i1 /]"))
          ((list c (== /) i2) (displayln "T[/ i2]"))
          ((list c i1 i2) (let ((value expr)) ;; to avoid compute it twice

                ;; normal case
                (if (vector? c)
                    (function-array-n-dim-set! c value (reverse (list
i1 i2))) ;;(array-n-dim-set! array value i1 i2)
                    (array-set! c i1 i2 value))

                value)))

Regards,

Damien



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

* Re: comparator in match pattern
  2023-06-28  8:44 comparator in match pattern Damien Mattei
@ 2023-06-28  8:55 ` Vivien Kraus
  2023-06-28 13:38   ` Damien Mattei
  0 siblings, 1 reply; 9+ messages in thread
From: Vivien Kraus @ 2023-06-28  8:55 UTC (permalink / raw)
  To: Damien Mattei, guile-user

Hello!

Le mercredi 28 juin 2023 à 10:44 +0200, Damien Mattei a écrit :
> does it exists in Guile something like == in Racket,
> documented here:
> https://docs.racket-lang.org/reference/match.html#%28form._%28%28lib._racket%2Fmatch..rkt%29._~3d~3d%29%29

From what I understand, (== something) would be translated as (? (cut
equal? <> something)) if you import srfi-26 for cut (or cute, if
something needs to be evaluated once).

Vivien



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

* Re: comparator in match pattern
  2023-06-28  8:55 ` Vivien Kraus
@ 2023-06-28 13:38   ` Damien Mattei
  2023-06-28 13:51     ` Vivien Kraus
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Mattei @ 2023-06-28 13:38 UTC (permalink / raw)
  To: guile-user

hello
i do not understand how i can do that with match:

scheme@(guile-user)> (use-modules (ice-9 match))
scheme@(guile-user)> (match (list 1 /) ((list c (cut equal? <> /))
'cool) ((list c t) 'not_cool))
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Throw to key `match-error' with args `("match" "no matching pattern"
(1 #<procedure / (#:optional _ _ . _)>))'.

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,q
scheme@(guile-user)> (match (list 1 /) ((? (cut equal? <> /)) 'cool)
((list c t) 'not_cool))
;;; <stdin>:23:0: warning: wrong number of arguments to `#<tree-il
(lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
(toplevel <>) (toplevel /)))))>'
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong number of arguments to (1 #<procedure / (#:optional _ _ . _)>)

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.


On Wed, Jun 28, 2023 at 10:52 AM Vivien Kraus <vivien@planete-kraus.eu> wrote:
>
> Hello!
>
> Le mercredi 28 juin 2023 à 10:44 +0200, Damien Mattei a écrit :
> > does it exists in Guile something like == in Racket,
> > documented here:
> > https://docs.racket-lang.org/reference/match.html#%28form._%28%28lib._racket%2Fmatch..rkt%29._~3d~3d%29%29
>
> From what I understand, (== something) would be translated as (? (cut
> equal? <> something)) if you import srfi-26 for cut (or cute, if
> something needs to be evaluated once).
>
> Vivien



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

* Re: comparator in match pattern
  2023-06-28 13:38   ` Damien Mattei
@ 2023-06-28 13:51     ` Vivien Kraus
  2023-06-28 13:55       ` Vivien Kraus
  0 siblings, 1 reply; 9+ messages in thread
From: Vivien Kraus @ 2023-06-28 13:51 UTC (permalink / raw)
  To: Damien Mattei, guile-user

Le mercredi 28 juin 2023 à 15:38 +0200, Damien Mattei a écrit :
> scheme@(guile-user)> (use-modules (ice-9 match))
> scheme@(guile-user)> (match (list 1 /) ((list c (cut equal? <> /))

So, you want to match against the / symbol, not the value of a variable
named /?

The simplest case would be to use quasiquote/unquote syntax in the
pattern:

(match (list 1 '/) 
  (`(,c /) 
   c))

should give: 1, the value of c

If you don’t understand quasiquote/unquote, you can use the slightly
uglier:

(match (list 1 '/) 
  ((c '/) 
   c))

If / is the name of a variable (why not), you can do:

(use-modules (srfi srfi-26))
(let ((/ 42)) 
  (match (list 1 42) 
    ((c (? (cut equal? <> /))) 
     c)))

In any case, no need to write "list" in the pattern.

Vivien



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

* Re: comparator in match pattern
  2023-06-28 13:51     ` Vivien Kraus
@ 2023-06-28 13:55       ` Vivien Kraus
  2023-06-28 15:10         ` Damien Mattei
  0 siblings, 1 reply; 9+ messages in thread
From: Vivien Kraus @ 2023-06-28 13:55 UTC (permalink / raw)
  To: Damien Mattei, guile-user

Le mercredi 28 juin 2023 à 15:51 +0200, Vivien Kraus a écrit :
> So, you want to match against the / symbol, not the value of a
> variable
> named /?

Sorry, I misread. You actually have a variable named /.

So this code is the only relevant one:

(use-modules (ice-9 match) (srfi srfi-26))
(let ((/ 42)) 
  (match (list 1 42) 
    ((c (? (cut equal? <> /))) 
     c)))

Vivien



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

* Re: comparator in match pattern
  2023-06-28 13:55       ` Vivien Kraus
@ 2023-06-28 15:10         ` Damien Mattei
  2023-06-28 15:31           ` Vivien Kraus
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Mattei @ 2023-06-28 15:10 UTC (permalink / raw)
  To: guile-user

Vivien , your solution is not working:

(let ((/ 42))
  (match (list 1 42)
    ((c (? (cut equal? <> /)))
     c)))
;;; <stdin>:30:2: warning: wrong number of arguments to `#<tree-il
(lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
(toplevel <>) (lexical / /-1dff1b83541ce327-407)))))>'
ice-9/boot-9.scm:1685:16: In procedure raise-exception:
Wrong number of arguments to 42

and / is  not a variable , it is simply a separator , the / procedure
too,and it is not quoted

in Racket this works:

(match (list container index1-or-keyword index2-or-keyword)

          ((list c (== /) (== /)) (displayln "T[/ /]"))

i can not find an equivalent in Guile:

(match (list 1 /) ((list c (? (cut equal? <> /))) 'cool))
;;; <stdin>:38:0: warning: wrong number of arguments to `#<tree-il
(lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
(toplevel <>) (toplevel /)))))>'

i do not understand the syntax.

i can not use the ther solutions with backquote or quote because the
infix parser give / not '/ to my procedures. I have solutions without
match, using cond but they are more long to code and less readable.

On Wed, Jun 28, 2023 at 3:51 PM Vivien Kraus <vivien@planete-kraus.eu> wrote:
>
> Le mercredi 28 juin 2023 à 15:51 +0200, Vivien Kraus a écrit :
> > So, you want to match against the / symbol, not the value of a
> > variable
> > named /?
>
> Sorry, I misread. You actually have a variable named /.
>
> So this code is the only relevant one:
>
> (use-modules (ice-9 match) (srfi srfi-26))
> (let ((/ 42))
>   (match (list 1 42)
>     ((c (? (cut equal? <> /)))
>      c)))
>
> Vivien



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

* Re: comparator in match pattern
  2023-06-28 15:10         ` Damien Mattei
@ 2023-06-28 15:31           ` Vivien Kraus
  2023-06-28 19:47             ` Damien Mattei
  0 siblings, 1 reply; 9+ messages in thread
From: Vivien Kraus @ 2023-06-28 15:31 UTC (permalink / raw)
  To: Damien Mattei, guile-user

[-- Attachment #1: Type: text/plain, Size: 1654 bytes --]

Le mercredi 28 juin 2023 à 17:10 +0200, Damien Mattei a écrit :
> Vivien , your solution is not working:
> 
> (let ((/ 42))
>   (match (list 1 42)
>     ((c (? (cut equal? <> /)))
>      c)))
> ;;; <stdin>:30:2: warning: wrong number of arguments to `#<tree-il
> (lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
> (toplevel <>) (lexical / /-1dff1b83541ce327-407)))))>'
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> Wrong number of arguments to 42

It works on my side (see screenshot). You did not quote the module
import line in your reply, did you forget to paste it?

(use-modules (ice-9 match) (srfi srfi-26))

Otherwise it is likely a problem with your guile installation, and I
can’t really help.

> and / is  not a variable , it is simply a separator , the / procedure
> too,and it is not quoted

I do not understand. Do you want to match against the '/ symbol or the
variable/procedure bound to "/"?

If you want to match against something that is bound to "/", then the
cut-equal solution is relevant. If you want to match against the /
symbol, then you can do either one of these:

(use-modules (ice-9 match))
(match (list 1 '/) 
  (`(,c /) 
   c))

or

(use-modules (ice-9 match))
(match (list 1 '/) 
  ((c '/) 
   c))

> in Racket this works:
> 
> (match (list container index1-or-keyword index2-or-keyword)
> 
>           ((list c (== /) (== /)) (displayln "T[/ /]"))

This is not a self-contained example, I don’t know what is index*-or-
keyword. Is it the division function or whatever is bound to / at that
time? The / symbol?

Vivien

[-- Attachment #2: Capture d’écran du 2023-06-28 17-18-46.png --]
[-- Type: image/png, Size: 50448 bytes --]

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

* Re: comparator in match pattern
  2023-06-28 15:31           ` Vivien Kraus
@ 2023-06-28 19:47             ` Damien Mattei
  2023-06-28 20:52               ` Damien Mattei
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Mattei @ 2023-06-28 19:47 UTC (permalink / raw)
  To: guile-user

thank you, ok , i understand ,my Scheme+ redefined <> to not = , this
was the problem, i apologize

On Wed, Jun 28, 2023 at 5:27 PM Vivien Kraus <vivien@planete-kraus.eu> wrote:
>
> Le mercredi 28 juin 2023 à 17:10 +0200, Damien Mattei a écrit :
> > Vivien , your solution is not working:
> >
> > (let ((/ 42))
> >   (match (list 1 42)
> >     ((c (? (cut equal? <> /)))
> >      c)))
> > ;;; <stdin>:30:2: warning: wrong number of arguments to `#<tree-il
> > (lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
> > (toplevel <>) (lexical / /-1dff1b83541ce327-407)))))>'
> > ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> > Wrong number of arguments to 42
>
> It works on my side (see screenshot). You did not quote the module
> import line in your reply, did you forget to paste it?
>
> (use-modules (ice-9 match) (srfi srfi-26))
>
> Otherwise it is likely a problem with your guile installation, and I
> can’t really help.
>
> > and / is  not a variable , it is simply a separator , the / procedure
> > too,and it is not quoted
>
> I do not understand. Do you want to match against the '/ symbol or the
> variable/procedure bound to "/"?
>
> If you want to match against something that is bound to "/", then the
> cut-equal solution is relevant. If you want to match against the /
> symbol, then you can do either one of these:
>
> (use-modules (ice-9 match))
> (match (list 1 '/)
>   (`(,c /)
>    c))
>
> or
>
> (use-modules (ice-9 match))
> (match (list 1 '/)
>   ((c '/)
>    c))
>
> > in Racket this works:
> >
> > (match (list container index1-or-keyword index2-or-keyword)
> >
> >           ((list c (== /) (== /)) (displayln "T[/ /]"))
>
> This is not a self-contained example, I don’t know what is index*-or-
> keyword. Is it the division function or whatever is bound to / at that
> time? The / symbol?
>
> Vivien



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

* Re: comparator in match pattern
  2023-06-28 19:47             ` Damien Mattei
@ 2023-06-28 20:52               ` Damien Mattei
  0 siblings, 0 replies; 9+ messages in thread
From: Damien Mattei @ 2023-06-28 20:52 UTC (permalink / raw)
  To: guile-user

seems to works now:

scheme@(guile-user)> (match (list 1 /) ((c (? (cut equal? <> /))) 'cool))
$2 = cool

On Wed, Jun 28, 2023 at 9:47 PM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> thank you, ok , i understand ,my Scheme+ redefined <> to not = , this
> was the problem, i apologize
>
> On Wed, Jun 28, 2023 at 5:27 PM Vivien Kraus <vivien@planete-kraus.eu> wrote:
> >
> > Le mercredi 28 juin 2023 à 17:10 +0200, Damien Mattei a écrit :
> > > Vivien , your solution is not working:
> > >
> > > (let ((/ 42))
> > >   (match (list 1 42)
> > >     ((c (? (cut equal? <> /)))
> > >      c)))
> > > ;;; <stdin>:30:2: warning: wrong number of arguments to `#<tree-il
> > > (lambda () (lambda-case ((() #f #f #f () ()) (call (toplevel equal?)
> > > (toplevel <>) (lexical / /-1dff1b83541ce327-407)))))>'
> > > ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> > > Wrong number of arguments to 42
> >
> > It works on my side (see screenshot). You did not quote the module
> > import line in your reply, did you forget to paste it?
> >
> > (use-modules (ice-9 match) (srfi srfi-26))
> >
> > Otherwise it is likely a problem with your guile installation, and I
> > can’t really help.
> >
> > > and / is  not a variable , it is simply a separator , the / procedure
> > > too,and it is not quoted
> >
> > I do not understand. Do you want to match against the '/ symbol or the
> > variable/procedure bound to "/"?
> >
> > If you want to match against something that is bound to "/", then the
> > cut-equal solution is relevant. If you want to match against the /
> > symbol, then you can do either one of these:
> >
> > (use-modules (ice-9 match))
> > (match (list 1 '/)
> >   (`(,c /)
> >    c))
> >
> > or
> >
> > (use-modules (ice-9 match))
> > (match (list 1 '/)
> >   ((c '/)
> >    c))
> >
> > > in Racket this works:
> > >
> > > (match (list container index1-or-keyword index2-or-keyword)
> > >
> > >           ((list c (== /) (== /)) (displayln "T[/ /]"))
> >
> > This is not a self-contained example, I don’t know what is index*-or-
> > keyword. Is it the division function or whatever is bound to / at that
> > time? The / symbol?
> >
> > Vivien



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

end of thread, other threads:[~2023-06-28 20:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-28  8:44 comparator in match pattern Damien Mattei
2023-06-28  8:55 ` Vivien Kraus
2023-06-28 13:38   ` Damien Mattei
2023-06-28 13:51     ` Vivien Kraus
2023-06-28 13:55       ` Vivien Kraus
2023-06-28 15:10         ` Damien Mattei
2023-06-28 15:31           ` Vivien Kraus
2023-06-28 19:47             ` Damien Mattei
2023-06-28 20:52               ` Damien Mattei

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).