unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user <guile-user@gnu.org>
Subject: Re: difference with 'match' at REPL and in code
Date: Wed, 11 Oct 2023 09:00:25 +0200	[thread overview]
Message-ID: <CADEOaddH+D1RngLq2W_mOCeewtF=4UBHTiAgCpxhPfgrCdDe1w@mail.gmail.com> (raw)
In-Reply-To: <CADEOadfwwZ51Qvj34JwPx8Z=YHBcdEguAcVpcU2maORE3r8myA@mail.gmail.com>

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.



      reply	other threads:[~2023-10-11  7:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADEOaddH+D1RngLq2W_mOCeewtF=4UBHTiAgCpxhPfgrCdDe1w@mail.gmail.com' \
    --to=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).