From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: difference with 'match' at REPL and in code Date: Wed, 11 Oct 2023 08:50:45 +0200 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16036"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Wed Oct 11 08:51:43 2023 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qqT4J-00044G-EA for guile-user@m.gmane-mx.org; Wed, 11 Oct 2023 08:51:43 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qqT3e-0004L2-Nv; Wed, 11 Oct 2023 02:51:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qqT3d-0004Ki-PK for guile-user@gnu.org; Wed, 11 Oct 2023 02:51:01 -0400 Original-Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qqT3b-0002yy-Rs for guile-user@gnu.org; Wed, 11 Oct 2023 02:51:01 -0400 Original-Received: by mail-ed1-x534.google.com with SMTP id 4fb4d7f45d1cf-53db3811d8fso1265645a12.1 for ; Tue, 10 Oct 2023 23:50:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697007057; x=1697611857; darn=gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=WhvrnAILyjZxsc1CcV9m5FQEE/aDHToI9856bNbF+xk=; b=T1Gl0sKN2J6B68rV+vERH60uvF25GhS7FTUJFJGZjoGdIHLjzsZehYuHAG4ki3hdRR nPNXxOil6IJCWQH1yAF5F+oNohESbOVsv9iDUA43HkzCbyomFf7uUpPTqaY8VznquROJ tIOcoro3FQzKrm2Um6xjerCsrKV8sW6aBykTMO6VaMHXTz/yjGUZ6jEVtQgkSLcDSBct 1fl0qqhQMQtZWKypjBQoxfwHex9uHcI01xXMXevUNWYQfvZmaCf6OgY7qZSTSv/TORUi z4yhG+nmWk/5HVcnsOvc/4NExAdTNqSod9TGWdLDZahUu2jKHLPi5iUxYwAMWwQVKqPm vxqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697007057; x=1697611857; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=WhvrnAILyjZxsc1CcV9m5FQEE/aDHToI9856bNbF+xk=; b=flQS8JufjTURfWb4+fN2meDZrtbRjBMBMt4EjvxeXyGr2knHIKD8SUan3UdMcKuqBn DsR7vS9ESgQuIl7T2KxH6I/uv76IFpbwTKCjpZXhkEupfJoW1gmXoeSPzAJBhwbXiSex 8zOwNjPv3p8bFCEMkAo0UO1ZCOqILnwm+sh24bAAL3LjoewYAbzHPgcP+Ffml26/DHrN f+cT//WBz3xCqZsEW5Vt4EhZgus7NwUANdtcUxIKCmu2cFXbIs+K18vWFrkXGogFkPBC 8pk/7DTwqTf+VpPmcMYON5bLab+Sw9QEaEDA5z1C1nCyaekjBu6qHG4wE1tt21DUlA2l zI8g== X-Gm-Message-State: AOJu0Yxl8nDp+MwPr1dnrxoXj/d435s4Eewy0XKHEDB9KTT1RICsFp2E nroN79YT6bdGY1qoXUQk8NjM/4IR6WT0OpzSA4GP1QUo7GY= X-Google-Smtp-Source: AGHT+IHJJdRK+7fUWyzpf2pY323oWjOITkLJeIHDh7R0XlZ75lMgL2tzxklIJcuXeaoCFlNkF7LAcBMCDv/HA9xmhC4= X-Received: by 2002:aa7:c792:0:b0:52f:a763:aab4 with SMTP id n18-20020aa7c792000000b0052fa763aab4mr15945638eds.5.1697007057058; Tue, 10 Oct 2023 23:50:57 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::534; envelope-from=damien.mattei@gmail.com; helo=mail-ed1-x534.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19251 Archived-At: 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.