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: Re: difference with 'match' at REPL and in code Date: Wed, 11 Oct 2023 08:51:55 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="23633"; 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:52:51 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 1qqT5O-0005wM-T4 for guile-user@m.gmane-mx.org; Wed, 11 Oct 2023 08:52:51 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qqT4r-0004mG-Jy; Wed, 11 Oct 2023 02:52:17 -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 1qqT4o-0004k5-1A for guile-user@gnu.org; Wed, 11 Oct 2023 02:52:15 -0400 Original-Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qqT4i-000390-Fz for guile-user@gnu.org; Wed, 11 Oct 2023 02:52:13 -0400 Original-Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-53de8fc1ad8so171205a12.0 for ; Tue, 10 Oct 2023 23:52:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697007126; x=1697611926; darn=gnu.org; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MdH3KM+jLcy83yuYfCwZB2ZQT5QoJcwvmItYffpRqpw=; b=RYH841frfSQrlQYFWyrf4y370yokErDOIAi6Kx3cD7XUV30ycHIv9EnXZtuSDDLecT Grz/I/dxEj0b0k1mpslet5vdkQXBhBO0ucrikxcpwo3OHqmGmzcC16demvMolcl6apHe mcGElb8zhDm+fImvO3lnF44pE51LyLKjtsRsRQjP2T1lCnpyJ1XjKodedaVLr7NFxhHh N//KPd14BES+2HPnfToLHFrAJ0yonm2RuvYAGP5cfBm5UeIqdmUNGe3cZmSX57DtFcRb hScTu3upkiJtNP7391ZTBeP0XjrXlGBjvDAZS0vGc7agCIwYPBap3RRaIcnXIOEcm0vs 37YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697007126; x=1697611926; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MdH3KM+jLcy83yuYfCwZB2ZQT5QoJcwvmItYffpRqpw=; b=O7O4+aCX49H8IXzh1Yqv4L85wOe8g3MK91Ykcox4jvzFoAa+n7JKEtQuixkGYewqM8 kE2WGeH1dTghmWiphCk6pnHfA/BR9ZYL8RjbRaZW2h3whXv6b8EZKf065XidT3OiiT9i uKL+WEwQa90N+ECeMeD/ggYIPTHSW3GvlQWeYeH0qQiIIosHbSQ6/str94NtPT1ZTIdg Cj3wYWAStX0PEW5FRcPJbSfOwgexdBpY3P7/ldbaJBLdfaNS39oknzxWzCMHaoeFFz4e YU1Y8s5TuoknK5c6u9Zs6hirmhxFQ/74+H7ilrjIb5e2T2kye46V96E8mJZMtdLKZDrL Nydw== X-Gm-Message-State: AOJu0Yz8Xf3K8jeFf9V+5lfUJf50w1l6xS3gkpYVvCo0gMHHR1g5U+zs jS/wezM1mjWoJn7cqbJO4dwcsCx/vAfs6re/LkO67eKg X-Google-Smtp-Source: AGHT+IGaBKkz1Hl4lSym0McMyTh2umdWYNcb20/Lwul2x7acxAbJErMBlUSC3q+7RRP+uOS//S8SbVFCvdgwvye17vk= X-Received: by 2002:a05:6402:12c3:b0:533:310b:a8aa with SMTP id k3-20020a05640212c300b00533310ba8aamr17954093edx.13.1697007126176; Tue, 10 Oct 2023 23:52:06 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2a00:1450:4864:20::533; envelope-from=damien.mattei@gmail.com; helo=mail-ed1-x533.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, T_SPF_TEMPERROR=0.01 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:19252 Archived-At: i just forgot to say that in my code <+ is 'define' On Wed, Oct 11, 2023 at 8:50=E2=80=AFAM Damien Mattei 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-us= ed) > > (display "we are in match !") (newline) > {step <+ index4-or-step-eval} > > (when {step =3D 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 >=3D 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-us= ed) > > {step <+ index4-or-step-eval} > > ;; > {s <+ (string-append "abcdefgh")} > ;; "abcdefgh" > ;; {s[3 $ $ 2] <- "0000"} > ;; > s > ;; "abc0e0g0" > > (when (=3D 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)) (>=3D 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 =3D #(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 =3D #(-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.