unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* difference with 'match' at REPL and in code
@ 2023-10-11  6:50 Damien Mattei
  2023-10-11  6:51 ` Damien Mattei
  0 siblings, 1 reply; 3+ messages in thread
From: Damien Mattei @ 2023-10-11  6:50 UTC (permalink / raw)
  To: guile-user

hi,

when i test at toplevel:
scheme@(guile-user)> (match 1 (1 (define x3) (+ x 1)))
While compiling expression:
Syntax error:
unknown location: definition in expression context, where definitions
are not allowed, in form (define x3)

but i have this code that works well and have definition in match:

(define (assignment-argument-4 container-eval index1-or-keyword-eval
index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
expr-eval)

  (when (not {(vector? container-eval) or (string? container-eval) or
          (array? container-eval) or (growable-vector? container-eval)})
    (error "assignment : container type not compatible : " container-eval))

  {index1-or-keyword-eval-pos <+ index1-or-keyword-eval}
  {index2-or-keyword-eval-pos <+ index2-or-keyword-eval}
  {index3-or-keyword-eval-pos <+ index3-or-keyword-eval}
  {index4-or-step-eval-pos <+ index4-or-step-eval}

  (declare container-length container-copy!)

  (if (vector? container-eval)
      ($>
       {container-length <- vector-length}
       {container-copy! <- vector-copy!})
      ($>  ;; a string
       {container-length <- string-length}
       {container-copy! <- string-copy!}))

  ;; transform the negative indexes in positive ones when not slices
  (when {(not (equal? index1-or-keyword-eval-pos slice)) and
{index1-or-keyword-eval-pos < 0}}
    {index1-or-keyword-eval-pos <- (container-length container-eval) +
index1-or-keyword-eval-pos})

  (when {(not (equal? index2-or-keyword-eval-pos slice)) and
{index2-or-keyword-eval-pos < 0}}
    {index2-or-keyword-eval-pos <- (container-length container-eval) +
index2-or-keyword-eval-pos})

  (when {(not (equal? index3-or-keyword-eval-pos slice)) and
{index3-or-keyword-eval-pos < 0}}
    {index3-or-keyword-eval-pos <- (container-length container-eval) +
index3-or-keyword-eval-pos})


  (when {(not (equal? index4-or-step-eval-pos slice)) and
{index4-or-step-eval-pos < 0}}
    {index4-or-step-eval-pos <- (container-length container-eval) +
index4-or-step-eval-pos})


  (match (list index1-or-keyword-eval-pos index2-or-keyword-eval-pos
index3-or-keyword-eval-pos index4-or-step-eval-pos)

     ;; T[i1 $ i2 $]
     ((i1 (? (cut equal? <> slice)) i2 (? (cut equal? <> slice)))
{container-eval[i1 slice i2] <- expr-eval})

     ;; T[$ i2 $ s3]
     ;; > {v <+ (vector 1 2 3 4 5 6 7 8 9)}
     ;; '#(1 2 3 4 5 6 7 8 9)
     ;; > {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
     ;; > v
     ;; '#(-1 2 -2 4 -3 6 7 8 9)
     (((? (cut equal? <> slice)) i2 (? (cut equal? <> slice)) step-not-used)

      (display "we are in match !") (newline)
      {step <+ index4-or-step-eval}

      (when {step = 0}
        (error "assignment : slice step cannot be zero"))

      {i <+ 0}

      (if {step < 0} ;; with negative index we start at end of vector
(like in Python)
          (for ({k <+ i2} {k >= 0} {k <- k + step})
           {container-eval[k] <- expr-eval[i]}
           {i <- i + 1})

          (for ({k <+ 0} {k < i2} {k <- k + step})
           {container-eval[k] <- expr-eval[i]}
           {i <- i + 1}))

      container-eval
      )


     ;; T[i1 $ $ s3]
     ((i1 (? (cut equal? <> slice)) (? (cut equal? <> slice)) step-not-used)

      {step <+ index4-or-step-eval}

      ;; > {s <+ (string-append "abcdefgh")}
      ;; "abcdefgh"
      ;; {s[3 $ $ 2] <- "0000"}
      ;; > s
      ;; "abc0e0g0"

      (when (= 0 step)
        (error "assignment : slice step cannot be zero"))

      (let* ((size-input (vector-length container-eval))
         (i 0))

        (if (< step 0) ;; with negative index we start at end of
vector (like in Python)
        (for ((define k (- size-input 1)) (>= k i1) (set! k (+ k step)))
             (vector-set! container-eval
                  k
                  (vector-ref expr-eval i))
             (set! i (+ 1 i)))

        (for ({k <+ i1} {k < size-input} {k <- k + step})
             (vector-set! container-eval
                  k
                  (vector-ref expr-eval i))
             {i <- 1 + i})))


      container-eval
      )



     ;; T[i1 i2 i3 i4]
     ((i1 i2 i3 i4)

      ;; normal case
      (if (vector? container-eval)
          (function-array-n-dim-set! container-eval expr-eval (reverse
(list i1 i2 i3 i4))) ;;(array-n-dim-set! array value i1 i2)
          (srfi25-array-set! container-eval index1-or-keyword-eval
index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
expr-eval))

      expr-eval  ;; returning a value allow the chaining : {T[3 5 6 2]
<- A[4 2 3] <- T[2 5]}
      )

     ) ;; end match

  )

scheme@(guile-user)> {v <+ (vector 1 2 3 4 5 6 7 8 9)}
$1 = #(1 2 3 4 5 6 7 8 9)
scheme@(guile-user)> {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
we are in match !
$2 = #(-1 2 -2 4 -3 6 7 8 9)

seems that match from toplevel is different than in code.
note: there is no difference in Racket or Kawa , match allow definitions inside.



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

* Re: difference with 'match' at REPL and in code
  2023-10-11  6:50 difference with 'match' at REPL and in code Damien Mattei
@ 2023-10-11  6:51 ` Damien Mattei
  2023-10-11  7:00   ` Damien Mattei
  0 siblings, 1 reply; 3+ messages in thread
From: Damien Mattei @ 2023-10-11  6:51 UTC (permalink / raw)
  To: guile-user

i just forgot to say that in my code <+ is 'define'

On Wed, Oct 11, 2023 at 8:50 AM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> hi,
>
> when i test at toplevel:
> scheme@(guile-user)> (match 1 (1 (define x3) (+ x 1)))
> While compiling expression:
> Syntax error:
> unknown location: definition in expression context, where definitions
> are not allowed, in form (define x3)
>
> but i have this code that works well and have definition in match:
>
> (define (assignment-argument-4 container-eval index1-or-keyword-eval
> index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
> expr-eval)
>
>   (when (not {(vector? container-eval) or (string? container-eval) or
>           (array? container-eval) or (growable-vector? container-eval)})
>     (error "assignment : container type not compatible : " container-eval))
>
>   {index1-or-keyword-eval-pos <+ index1-or-keyword-eval}
>   {index2-or-keyword-eval-pos <+ index2-or-keyword-eval}
>   {index3-or-keyword-eval-pos <+ index3-or-keyword-eval}
>   {index4-or-step-eval-pos <+ index4-or-step-eval}
>
>   (declare container-length container-copy!)
>
>   (if (vector? container-eval)
>       ($>
>        {container-length <- vector-length}
>        {container-copy! <- vector-copy!})
>       ($>  ;; a string
>        {container-length <- string-length}
>        {container-copy! <- string-copy!}))
>
>   ;; transform the negative indexes in positive ones when not slices
>   (when {(not (equal? index1-or-keyword-eval-pos slice)) and
> {index1-or-keyword-eval-pos < 0}}
>     {index1-or-keyword-eval-pos <- (container-length container-eval) +
> index1-or-keyword-eval-pos})
>
>   (when {(not (equal? index2-or-keyword-eval-pos slice)) and
> {index2-or-keyword-eval-pos < 0}}
>     {index2-or-keyword-eval-pos <- (container-length container-eval) +
> index2-or-keyword-eval-pos})
>
>   (when {(not (equal? index3-or-keyword-eval-pos slice)) and
> {index3-or-keyword-eval-pos < 0}}
>     {index3-or-keyword-eval-pos <- (container-length container-eval) +
> index3-or-keyword-eval-pos})
>
>
>   (when {(not (equal? index4-or-step-eval-pos slice)) and
> {index4-or-step-eval-pos < 0}}
>     {index4-or-step-eval-pos <- (container-length container-eval) +
> index4-or-step-eval-pos})
>
>
>   (match (list index1-or-keyword-eval-pos index2-or-keyword-eval-pos
> index3-or-keyword-eval-pos index4-or-step-eval-pos)
>
>      ;; T[i1 $ i2 $]
>      ((i1 (? (cut equal? <> slice)) i2 (? (cut equal? <> slice)))
> {container-eval[i1 slice i2] <- expr-eval})
>
>      ;; T[$ i2 $ s3]
>      ;; > {v <+ (vector 1 2 3 4 5 6 7 8 9)}
>      ;; '#(1 2 3 4 5 6 7 8 9)
>      ;; > {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
>      ;; > v
>      ;; '#(-1 2 -2 4 -3 6 7 8 9)
>      (((? (cut equal? <> slice)) i2 (? (cut equal? <> slice)) step-not-used)
>
>       (display "we are in match !") (newline)
>       {step <+ index4-or-step-eval}
>
>       (when {step = 0}
>         (error "assignment : slice step cannot be zero"))
>
>       {i <+ 0}
>
>       (if {step < 0} ;; with negative index we start at end of vector
> (like in Python)
>           (for ({k <+ i2} {k >= 0} {k <- k + step})
>            {container-eval[k] <- expr-eval[i]}
>            {i <- i + 1})
>
>           (for ({k <+ 0} {k < i2} {k <- k + step})
>            {container-eval[k] <- expr-eval[i]}
>            {i <- i + 1}))
>
>       container-eval
>       )
>
>
>      ;; T[i1 $ $ s3]
>      ((i1 (? (cut equal? <> slice)) (? (cut equal? <> slice)) step-not-used)
>
>       {step <+ index4-or-step-eval}
>
>       ;; > {s <+ (string-append "abcdefgh")}
>       ;; "abcdefgh"
>       ;; {s[3 $ $ 2] <- "0000"}
>       ;; > s
>       ;; "abc0e0g0"
>
>       (when (= 0 step)
>         (error "assignment : slice step cannot be zero"))
>
>       (let* ((size-input (vector-length container-eval))
>          (i 0))
>
>         (if (< step 0) ;; with negative index we start at end of
> vector (like in Python)
>         (for ((define k (- size-input 1)) (>= k i1) (set! k (+ k step)))
>              (vector-set! container-eval
>                   k
>                   (vector-ref expr-eval i))
>              (set! i (+ 1 i)))
>
>         (for ({k <+ i1} {k < size-input} {k <- k + step})
>              (vector-set! container-eval
>                   k
>                   (vector-ref expr-eval i))
>              {i <- 1 + i})))
>
>
>       container-eval
>       )
>
>
>
>      ;; T[i1 i2 i3 i4]
>      ((i1 i2 i3 i4)
>
>       ;; normal case
>       (if (vector? container-eval)
>           (function-array-n-dim-set! container-eval expr-eval (reverse
> (list i1 i2 i3 i4))) ;;(array-n-dim-set! array value i1 i2)
>           (srfi25-array-set! container-eval index1-or-keyword-eval
> index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
> expr-eval))
>
>       expr-eval  ;; returning a value allow the chaining : {T[3 5 6 2]
> <- A[4 2 3] <- T[2 5]}
>       )
>
>      ) ;; end match
>
>   )
>
> scheme@(guile-user)> {v <+ (vector 1 2 3 4 5 6 7 8 9)}
> $1 = #(1 2 3 4 5 6 7 8 9)
> scheme@(guile-user)> {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
> we are in match !
> $2 = #(-1 2 -2 4 -3 6 7 8 9)
>
> seems that match from toplevel is different than in code.
> note: there is no difference in Racket or Kawa , match allow definitions inside.



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

* Re: difference with 'match' at REPL and in code
  2023-10-11  6:51 ` Damien Mattei
@ 2023-10-11  7:00   ` Damien Mattei
  0 siblings, 0 replies; 3+ messages in thread
From: Damien Mattei @ 2023-10-11  7:00 UTC (permalink / raw)
  To: guile-user

sorry there was something misformed in my toplevel test example,
all is ok even at toplevel:

(let ((l '(hello (world))))
  (match l           ;; <- the input object
    (('hello (who))  ;; <- the pattern
(define x 3) (+ 1 x))))


On Wed, Oct 11, 2023 at 8:51 AM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> i just forgot to say that in my code <+ is 'define'
>
> On Wed, Oct 11, 2023 at 8:50 AM Damien Mattei <damien.mattei@gmail.com> wrote:
> >
> > hi,
> >
> > when i test at toplevel:
> > scheme@(guile-user)> (match 1 (1 (define x3) (+ x 1)))
> > While compiling expression:
> > Syntax error:
> > unknown location: definition in expression context, where definitions
> > are not allowed, in form (define x3)
> >
> > but i have this code that works well and have definition in match:
> >
> > (define (assignment-argument-4 container-eval index1-or-keyword-eval
> > index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
> > expr-eval)
> >
> >   (when (not {(vector? container-eval) or (string? container-eval) or
> >           (array? container-eval) or (growable-vector? container-eval)})
> >     (error "assignment : container type not compatible : " container-eval))
> >
> >   {index1-or-keyword-eval-pos <+ index1-or-keyword-eval}
> >   {index2-or-keyword-eval-pos <+ index2-or-keyword-eval}
> >   {index3-or-keyword-eval-pos <+ index3-or-keyword-eval}
> >   {index4-or-step-eval-pos <+ index4-or-step-eval}
> >
> >   (declare container-length container-copy!)
> >
> >   (if (vector? container-eval)
> >       ($>
> >        {container-length <- vector-length}
> >        {container-copy! <- vector-copy!})
> >       ($>  ;; a string
> >        {container-length <- string-length}
> >        {container-copy! <- string-copy!}))
> >
> >   ;; transform the negative indexes in positive ones when not slices
> >   (when {(not (equal? index1-or-keyword-eval-pos slice)) and
> > {index1-or-keyword-eval-pos < 0}}
> >     {index1-or-keyword-eval-pos <- (container-length container-eval) +
> > index1-or-keyword-eval-pos})
> >
> >   (when {(not (equal? index2-or-keyword-eval-pos slice)) and
> > {index2-or-keyword-eval-pos < 0}}
> >     {index2-or-keyword-eval-pos <- (container-length container-eval) +
> > index2-or-keyword-eval-pos})
> >
> >   (when {(not (equal? index3-or-keyword-eval-pos slice)) and
> > {index3-or-keyword-eval-pos < 0}}
> >     {index3-or-keyword-eval-pos <- (container-length container-eval) +
> > index3-or-keyword-eval-pos})
> >
> >
> >   (when {(not (equal? index4-or-step-eval-pos slice)) and
> > {index4-or-step-eval-pos < 0}}
> >     {index4-or-step-eval-pos <- (container-length container-eval) +
> > index4-or-step-eval-pos})
> >
> >
> >   (match (list index1-or-keyword-eval-pos index2-or-keyword-eval-pos
> > index3-or-keyword-eval-pos index4-or-step-eval-pos)
> >
> >      ;; T[i1 $ i2 $]
> >      ((i1 (? (cut equal? <> slice)) i2 (? (cut equal? <> slice)))
> > {container-eval[i1 slice i2] <- expr-eval})
> >
> >      ;; T[$ i2 $ s3]
> >      ;; > {v <+ (vector 1 2 3 4 5 6 7 8 9)}
> >      ;; '#(1 2 3 4 5 6 7 8 9)
> >      ;; > {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
> >      ;; > v
> >      ;; '#(-1 2 -2 4 -3 6 7 8 9)
> >      (((? (cut equal? <> slice)) i2 (? (cut equal? <> slice)) step-not-used)
> >
> >       (display "we are in match !") (newline)
> >       {step <+ index4-or-step-eval}
> >
> >       (when {step = 0}
> >         (error "assignment : slice step cannot be zero"))
> >
> >       {i <+ 0}
> >
> >       (if {step < 0} ;; with negative index we start at end of vector
> > (like in Python)
> >           (for ({k <+ i2} {k >= 0} {k <- k + step})
> >            {container-eval[k] <- expr-eval[i]}
> >            {i <- i + 1})
> >
> >           (for ({k <+ 0} {k < i2} {k <- k + step})
> >            {container-eval[k] <- expr-eval[i]}
> >            {i <- i + 1}))
> >
> >       container-eval
> >       )
> >
> >
> >      ;; T[i1 $ $ s3]
> >      ((i1 (? (cut equal? <> slice)) (? (cut equal? <> slice)) step-not-used)
> >
> >       {step <+ index4-or-step-eval}
> >
> >       ;; > {s <+ (string-append "abcdefgh")}
> >       ;; "abcdefgh"
> >       ;; {s[3 $ $ 2] <- "0000"}
> >       ;; > s
> >       ;; "abc0e0g0"
> >
> >       (when (= 0 step)
> >         (error "assignment : slice step cannot be zero"))
> >
> >       (let* ((size-input (vector-length container-eval))
> >          (i 0))
> >
> >         (if (< step 0) ;; with negative index we start at end of
> > vector (like in Python)
> >         (for ((define k (- size-input 1)) (>= k i1) (set! k (+ k step)))
> >              (vector-set! container-eval
> >                   k
> >                   (vector-ref expr-eval i))
> >              (set! i (+ 1 i)))
> >
> >         (for ({k <+ i1} {k < size-input} {k <- k + step})
> >              (vector-set! container-eval
> >                   k
> >                   (vector-ref expr-eval i))
> >              {i <- 1 + i})))
> >
> >
> >       container-eval
> >       )
> >
> >
> >
> >      ;; T[i1 i2 i3 i4]
> >      ((i1 i2 i3 i4)
> >
> >       ;; normal case
> >       (if (vector? container-eval)
> >           (function-array-n-dim-set! container-eval expr-eval (reverse
> > (list i1 i2 i3 i4))) ;;(array-n-dim-set! array value i1 i2)
> >           (srfi25-array-set! container-eval index1-or-keyword-eval
> > index2-or-keyword-eval index3-or-keyword-eval index4-or-step-eval
> > expr-eval))
> >
> >       expr-eval  ;; returning a value allow the chaining : {T[3 5 6 2]
> > <- A[4 2 3] <- T[2 5]}
> >       )
> >
> >      ) ;; end match
> >
> >   )
> >
> > scheme@(guile-user)> {v <+ (vector 1 2 3 4 5 6 7 8 9)}
> > $1 = #(1 2 3 4 5 6 7 8 9)
> > scheme@(guile-user)> {v[$ 6 $ 2] <- (vector -1 -2 -3 -4 -5)}
> > we are in match !
> > $2 = #(-1 2 -2 4 -3 6 7 8 9)
> >
> > seems that match from toplevel is different than in code.
> > note: there is no difference in Racket or Kawa , match allow definitions inside.



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

end of thread, other threads:[~2023-10-11  7:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-11  6:50 difference with 'match' at REPL and in code Damien Mattei
2023-10-11  6:51 ` Damien Mattei
2023-10-11  7:00   ` 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).