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: cond(itionals) with optional execution of statements Date: Mon, 13 Sep 2021 12:04:53 +0200 Message-ID: References: <6d4ff570-1b0c-8158-a236-a8c00102f7e4@posteo.de> <230505e1-0fb2-27cf-6578-6805b1c4b6cf@posteo.de> 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="2142"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Zelphir Kaltstahl Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Sep 13 12:13:35 2021 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 1mPixz-0000Mx-6j for guile-user@m.gmane-mx.org; Mon, 13 Sep 2021 12:13:35 +0200 Original-Received: from localhost ([::1]:59110 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mPixy-0000qg-1Q for guile-user@m.gmane-mx.org; Mon, 13 Sep 2021 06:13:34 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47648) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mPipr-0006Oe-Sc for guile-user@gnu.org; Mon, 13 Sep 2021 06:05:12 -0400 Original-Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]:41551) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mPipm-00029b-CH for guile-user@gnu.org; Mon, 13 Sep 2021 06:05:11 -0400 Original-Received: by mail-io1-xd34.google.com with SMTP id j18so11265128ioj.8 for ; Mon, 13 Sep 2021 03:05:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qvcTrvW/JKiGHJWljioa3D8TAN1N4EhMjyT10UDDgcI=; b=DfSXWxAYzkCVNku9PWgx9D1fIJkeX5FE7cC/hcLFMPmAvxYR0riscDs184clBgxO55 nxRqg3anaP8sGWMtZcDArvsxuAfT6J2LSDfayt5qygCgX/9LUGGYrwaq+pyzTw+26n0Q CLDkyahIK/3Tg1pCiGoPYZjoiq+R5B6SRhpY3LPAl1wBVrSJgK0pxURyxH1yzVKb/MIm aBy31mr1IIs3/3/Casb9CgQMhe+EShrukcFkIG1+wAqoGFb+Qw7NoEzsGBv1SFeoHcvY eKL+qeBYyvOLna3cOeN8LDW0WhRNIlE90pxaxIRCnMT5b3QnojcCoXjOO7Uh6XavRQtZ wwEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qvcTrvW/JKiGHJWljioa3D8TAN1N4EhMjyT10UDDgcI=; b=ceqGLda9EWZorf/xPWmBiDsHozkqwiX+DsDJ0fv+XuYTBCS/PLloBuALDVvse4+ky3 PeHy2T79wiaVqXyl8dcQjEcI7oUPoxBcFlCqsH5N6A2qa9qeqMvTpjVUW6oy6WmH5bhG qpL7kX6UVvyRZu+ejlsQe65ZQH25BJydlhWk5RfkNL9whirUOCcWaYhw0/ewY5KWM8EQ NjeDenO86RBv7ddD4onL93UjGucSDK+ARB5/P1mIxuXFlwML33ahjx44o21VXM99ZNPB kiZGbXhsA7kikpfJV2nadR4LBObRpPZkUQOzmA33n1A8urlJ54rHFNJbfeCgPBc/Cr1K Upwg== X-Gm-Message-State: AOAM533Ko1jORdydZIFBBWGYM62BRJb1NJJEJdWz9bULJMk6IsGDSWn1 tslARSvrHm+JqKA5uwo7sYyV6l2ou/yyu1d63RuD9AAT31M= X-Google-Smtp-Source: ABdhPJwHUoqx7eP9xY52lRNdKS7z5glZg133Dyo3bdvL70w0AtNOyOPNxt6uP7t4sHkFdAz68ltMAvpmxVGMLKY54xw= X-Received: by 2002:a05:6638:1642:: with SMTP id a2mr9036937jat.88.1631527504855; Mon, 13 Sep 2021 03:05:04 -0700 (PDT) In-Reply-To: <230505e1-0fb2-27cf-6578-6805b1c4b6cf@posteo.de> Received-SPF: pass client-ip=2607:f8b0:4864:20::d34; envelope-from=damien.mattei@gmail.com; helo=mail-io1-xd34.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, HTML_MESSAGE=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-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.io gmane.lisp.guile.user:17732 Archived-At: hello Zelphir, i will answer in mail, note that some answers are perhaps already in my previous mail in the mailing list On Sun, Sep 12, 2021 at 8:36 PM Zelphir Kaltstahl < zelphirkaltstahl@posteo.de> wrote: > Hi Damien! > > I see! Indeed, some indentation happening there. I get the point now, > thank you. Could be well worth it, but it depends on how much you get out > of it, compared to how much additional mental load you get for introducing > a new control flow concept. > yes i agree. i have not developped a lot with this control flow condx , we can do the same in a python style with a few 'if' and 'return' and 'when' and that give already enought lisibility, but as cond exist in Scheme historically but well used todays i wanted to improve it a bit. 'condx' overpass the cond limitation, cond limitation are the causes that force me to introduce 'return' in 'def'inition of function 'de base' i think we must include 'return' in every function definition and my keyword 'def' (see previous mail) is derived from python ,it is shorter ans save a few char in indentation too... > Sometimes it is worth taking a step back and refactoring part of the logic > out into a separate procedure, which then starts at a lower level of > indentation again, avoiding the problem. I don't know your scenario though, > so this is only a general idea. > > I would probably write the `cond` version a bit different, to avoid > `begin` and break a few lines at other places to avoid the indentation > taking a lot of horizontal space. I am not sure it makes sense in your > context, but here it is: > > ~~~~ > (define (ssigma-proto L t) > > (set! cpt {cpt + 1}) > > (define ls (length L)) > (define dyn (array-ref dyna ls t)) > > ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution > > (cond > [(not (zero? dyn)) (one? dyn)] > [(null? L) (array-set! dyna 2 ls t) #f] ;; return #f > [else > (let [(c (first L))] > (cond > [{c = t} ;; c is the solution > (array-set! dyna 1 ls t) > #t] ;; return #t > [else > (let [(R (rest L))] > (cond > [{c > t} ;; continue searching a solution in the rest > (let [(s (ssigma-proto R t))] > (array-set! dyna (one-two s) ls t) > s)] ;; return s > [else > ;; c < t at this point > ;; c is part of the solution or his approximation > ;; or c is not part of solution > (let [(s {(ssigma-proto R {t - c}) or (ssigma-proto R t)})] > (array-set! dyna (one-two s) ls t) > s)]))]))])) > ~~~~ > > (Removed some empty lines for brevity in this e-mail. Sometimes I like > good empty lines too! Could the `set!` be replaced using a `define`?) > if you look at my previous mail the code i inserted at end ,it goes in the same sense > I guess the expressions in braces are using infix operations like the `or` > or `=` in the condition of `{c = t}`. > yes it is not developped by me but it is in SRFI 105 named 'curly infix': https://srfi.schemers.org/srfi-105/srfi-105.html a great SRFI ! i think, that scheme needs a bit of infix for some mathematical expression or testing ,it allow to write expression as in natural language {x > 3} is easiest to understand than (> x 3),all is explained in the part RATIONALE of the SRFI if you want to use it with guile,as far as i remember you just have to add this in your .guile file: (read-enable 'curly-infix) to have it activated everywhere in code, it is fully compatible with other features of Scheme SRFI 105 also allow to use notation like {dyna[ls t]} for array and notation for functions such as {sin(x)} i'm developping an extension to scheme that will allow more things (see example in previous mail), i just have to package it in a module and write some documentation about it, i hope to release it first for Guile in this month and for other scheme implementation later... Damien Best regards, > Zelphir > On 9/12/21 7:05 PM, Damien Mattei wrote: > > Hello Zelphir, > > condx evaluate all code(s) in the 'exec' block until a conditional is > true, it then evaluate the consequent code of course. > So ,yes your true it saves a lot of nesting parenthesis as in this example: > > here a code with condx and without it: > > > (define (ssigma-proto-condx L t) > > (set! cpt {cpt + 1}) > > (define ls (length L)) > (define dyn (array-ref dyna ls t)) > > ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution > > (condx [(not (zero? dyn)) (one? dyn)] > [(null? L) (array-set! dyna 2 ls t) #f] ;; return #f > > [exec (define c (first L))] > > ;; c is the solution > [{c = t} (array-set! dyna 1 ls t) #t] ;; return #t > > [exec (define R (rest L))] > > ;; continue searching a solution in the rest > [{c > t} (define s (ssigma-proto R t)) > (array-set! dyna > (one-two s) > ls t) > s] ;; return s > > ;; else : > ;; c < t at this point > ;; c is part of the solution or his approximation > ;; or c is not part of solution > [else (define s {(ssigma-proto R {t - c}) or (ssigma-proto R t)}) > (array-set! dyna (one-two s) > ls t) > s])) > > > without condx: > > > > (define (ssigma-proto L t) > > (set! cpt {cpt + 1}) > > (define ls (length L)) > (define dyn (array-ref dyna ls t)) > > ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution > > (cond [(not (zero? dyn)) (one? dyn)] > [(null? L) (array-set! dyna 2 ls t) #f] ;; return #f > > [else (let [(c (first L))] > > (if {c = t} ;; c is the solution > > (begin > (array-set! dyna 1 ls t) > #t) ;; return #t > > ;; else > (let [(R (rest L))] > > (if {c > t} ;; continue searching a solution in the rest > > (let [(s (ssigma-proto R t))] > (array-set! dyna > (one-two s) > ls t) > > s) ;; return s > > ;; else > ;; c < t at this point > ;; c is part of the solution or his approximation > ;; or c is not part of solution > (let [(s {(ssigma-proto R {t - c}) or (ssigma-proto R t)})] > (array-set! dyna (one-two s) > ls t) > s))))) > ] )) > > > there a lot more of indentation and nesting. > > Note also that the use of let () in condx definition allow to use define > in consequent and 'exec' block. > > Damien > > > On Sun, Sep 12, 2021 at 11:41 AM Zelphir Kaltstahl < > zelphirkaltstahl@posteo.de> wrote: > >> Hello Damien! >> >> I am not sure I understand the reasoning behind condx: I think cond is >> already a >> macro, which only evaluates a consequent, if the predicate of its case is >> #t. >> Additionally multiple expressions are possible in each branch. >> >> To clarify, I ask: What is the case, where condx does or does not >> evaluate some >> code, when cond would not or would? Or is it rather about the different >> nesting >> / sequence of expressions, which condx seems to enable? I think the flow >> you >> demonstrate might save a bit of nesting. >> >> Best regards, >> Zelphir >> >> On 9/11/21 11:14 AM, Damien Mattei wrote: >> > hello, >> > >> > i wrote a little macro (file condx.scm) that allow : cond(itionals) >> with >> > optional execution of statements before: >> > >> > (define-syntax condx >> > (syntax-rules (exec) >> > ((_) >> > (error 'condx "No else clause")) >> > ((_ (else e ...)) >> > (let () e ...)) >> > ((_ (exec s ...) d1 ...) >> > (let () s ... (condx d1 ...))) >> > ((_ (t e ...) tail ...) >> > (if t >> > (let () e ...) >> > (condx tail ...))))) >> > >> > use it like that: >> > >> > mattei@macbook-pro-touch-bar library-FunctProg % guile >> > GNU Guile 3.0.7 >> > Copyright (C) 1995-2021 Free Software Foundation, Inc. >> > >> > Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. >> > This program is free software, and you are welcome to redistribute it >> > under certain conditions; type `,show c' for details. >> > >> > Enter `,help' for help. >> > scheme@(guile-user)> (load "condx.scm") >> > ;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0 >> > ;;; or pass the --no-auto-compile argument to disable. >> > ;;; compiling /Users/mattei/Dropbox/git/library-FunctProg/condx.scm >> > ;;; compiled >> > >> /Users/mattei/.cache/guile/ccache/3.0-LE-8-4.5/Users/mattei/Dropbox/git/library-FunctProg/condx.scm.go >> > scheme@(guile-user)> (define x 1) >> > >> > (condx ((= x 7) 'never) >> > (exec >> > (define y 3) >> > (set! x 7)) >> > ((= y 1) 'definitely_not) >> > (exec >> > (set! y 10) >> > (define z 2)) >> > ((= x 7) (+ x y z)) >> > (else 'you_should_not_be_here)) >> > $1 = 19 >> > >> > i share it to have idea about critics or idea to improve it as it will >> be >> > part of a Scheme extension to scheme language that will include other >> > features.... >> > >> > have a good day >> > >> > Damien >> >> -- >> repositories: https://notabug.org/ZelphirKaltstahl >> >> -- > repositories: https://notabug.org/ZelphirKaltstahl > >