guile-user
[Top][All Lists]
Advanced

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

difference with 'match' at REPL and in code


From: Damien Mattei
Subject: difference with 'match' at REPL and in code
Date: Wed, 11 Oct 2023 08:50:45 +0200

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 via email to

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