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 11:08:43 +0200 Message-ID: References: <6d4ff570-1b0c-8158-a236-a8c00102f7e4@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="4480"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Jean-Paul Roy , Guile User , Damien Mattei To: Taylan Kammer Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Sep 13 11:09:16 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 1mPhxj-0000zP-JV for guile-user@m.gmane-mx.org; Mon, 13 Sep 2021 11:09:15 +0200 Original-Received: from localhost ([::1]:49414 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mPhxi-0004Ct-JW for guile-user@m.gmane-mx.org; Mon, 13 Sep 2021 05:09:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36426) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mPhxT-0004Cl-7V for guile-user@gnu.org; Mon, 13 Sep 2021 05:08:59 -0400 Original-Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]:37727) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mPhxQ-0005DG-Fj for guile-user@gnu.org; Mon, 13 Sep 2021 05:08:58 -0400 Original-Received: by mail-il1-x131.google.com with SMTP id i13so9300077ilm.4 for ; Mon, 13 Sep 2021 02:08:55 -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=RSjAjSIU1ibSS3eq/P7A4aQDN4vv7wbAD70QN8HehCE=; b=OMd1gyP448kgJqJLoQ52KHe+1bDE9AhFpYW9hD8jTxY870+o+gXbhcBf23OdqYJJLC WblbUSYVRPq5NXxUfxus/pwoo2FUnomp8lX4lVrubmyjxVWe9O69b7M797HzsNC2Q2+B yil9AhRFs1ztjD7oP1MymyzX2s9PrESKFTT2CCXSRk91wX0KOwyrK+XwexSCvV+u8NqM 7PRBrMLVr3TnnnUsutyFPCsvsUG22iS2KaK8LS5ZTrgjbYXnNT5aQEXelyGqqEA2wBkd fTKlJDhiWgTuVgT9kVCU1fIpS818x3iHZgaVOqDBPBVz3nTBZjJeKAp/YUzXOBrquafj 8gwg== 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=RSjAjSIU1ibSS3eq/P7A4aQDN4vv7wbAD70QN8HehCE=; b=aU7UNdknJ7BYm0Nu0EgUgszHrdEmGzIHt6nRY8Rr6+W6SyvnbgXoO19z7f6aL21R0y TwGgBXeiufrl7y06CeLwFzjq0FcLRfYrfmesnOUFQGx+pdRMbmErR0aw7mlEz2BjNXjz P+r9fB3FSW5DC/u4kEBgCT8aGgbF2MXgF7f6WiCXBRw6t77IaaLaQQMlcyqv6D9CsseH HO77mGScuCTXHBrbooydvYFEvHwQQ9X/Wg8qlD3bwT95yPltvb5Wis/IeK2jjQttTKM9 KNa1TsJoJilEI47aIUNrO7jVfqk8F1FpmF+BL4Ab5KzVn6ElGr7qTPzBiqIH+RXR30/V 2DVA== X-Gm-Message-State: AOAM532HLNz75CsyvUuYl3sdU9Hsfe6m55UAd5/JkLrDvf8xN+pemGfp OVOoVEShUxbIgadhFcRokbAlYD9e9JjrKEmBFVE= X-Google-Smtp-Source: ABdhPJwrTHYQA4ypQpgFXvB0zBQnLbWlUercsG+Ogx2Ka/DmZyb5ZEQSeZSIiQuG9d3U6h19uiiSFaBMg/gbrYhgh3g= X-Received: by 2002:a05:6e02:48d:: with SMTP id b13mr2948110ils.171.1631524135074; Mon, 13 Sep 2021 02:08:55 -0700 (PDT) In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::131; envelope-from=damien.mattei@gmail.com; helo=mail-il1-x131.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_FONT_LOW_CONTRAST=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:17731 Archived-At: Hi Taylan, thanks for your ideas ,i had the same thoughts for some. I will try to answer below: On Sun, Sep 12, 2021 at 8:31 PM Taylan Kammer wrote: > On 12.09.2021 19:05, 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: > > > > Interesting macro. I think I've occasionally felt the need for such a > thing. > > Two comments: > > 1. Using 'begin' instead of 'exec' might feel more familiar to Schemers, > and > since the 'literals' of syntax-rules are matched hygienically starting > from Guile 2.2 (I think), it might be a good idea to use a common core > identifier instead of an unbound one like 'exec'. (Or you could bind > it.) > i had this idea of using begin ,i choose the name condx ,x for execute , begin sound less well ;-) ok i admit it is not a serious argument but i have to keep 'cond' prefix for naming historically and i do not want to have a long name as it add some indentation too much. about 'begin' if found it transparent but it can be confusing with the 'begin' of Scheme,and 'begin' of scheme is dependant of context: " begin is special - begin in a body context allows definitions, but begin in an expression contexts forbids definitions. " (source : https://stackoverflow.com/questions/16221336/error-with-define-in-racket/16221820#16221820 ) doc with guile : https://www.gnu.org/software/guile/manual/html_node/begin.html so begin is used for sequencing and splicing depending of context, so using the keyword begin here can cause the user to ask itself about the context which is not clearly defined in condx , i assume is it a context where definitions are allowed, but we can use begin as key words , i had this idea too... searching about a previous of exec in scheme give no result, it is not used in guile, racket ,only in others languages : bash, C ? (not sure) > > A little elaboration, in case you don't know what I'm talking about: the > "literals" of a syntax-rules macro are not matched by "name" (the string > representing the symbol seen in code) but by "variable binding." > i understand a bit what you mean but i'm not a easy with that, i've been facing this problem defining a return keywords too, see later below > > For example: > > (let ((else #f)) > (cond > ((= 1 0) 'foo) > (else 'bar) > (#t 'qux))) > ; => qux > > The code returns qux, because the 'cond' macro doesn't recognize the 'else' > as the 'else' that it knows of, since it's been rebound via let. > i hope no serious programmer will rename 'else' even if i do it in Lisp cond which have not else clause, so renaming else to #t ( t in lisp) make lisp compatible with scheme but it is not the case here we are in Scheme,even if one day hope to port that to Lisp > > An example that would work in a conforming R7RS-small implementation; don't > know if it works with Guile, it's just to explain the principle: > > ;; Import core bindings, but renaming 'else' to 'otherwise' > (import (rename (scheme base) (else otherwise))) > > (cond > ((= 1 0) 'foo) > ((= 2 3) 'bar) > (otherwise 'qux)) > just a comment, otherwise is a long keyword,i'm afraid of identation in can cause, i assume it was just an example you wrote > ; => qux > > The (scheme base) library defined by R7RS-small exports the identifier > 'else' > which is used by 'cond' for matching. This allows the programmer to rename > the 'else' used by 'cond' while importing the base library. (The 'else' is > not bound to anything useful, it's just bound at all so it can be renamed.) > > Likewise you might want to bind 'exec' to anything and export it along with > the 'condx' identifier, so if some Schemer uses the identifier 'exec' for > something different in their code, they can still use your macro by > renaming > your 'exec' to something else. Otherwise there's no way to make it work. > If 'exec' was unbound during the definition of 'condx' then it must remain > unbound for 'condx' to recognized it again, meaning it can't be renamed. > > Or (IMO better) you could reuse the 'begin' binding (in Guile's case, from > boot-9, in R7RS-small, from (scheme base)), because it's very unlikely that > someone will use 'begin' for something else in their code, and it would > force > them to rename the core 'begin' to something else and then that would work > with your code automatically. E.g. if someone renames 'begin' to 'start' > it > will automatically work in your macro if you had defined it with 'begin' in > the literals list of syntax-rules. > > 2. You might be interested in let/ec, which lets you bind a variable to an > "escape continuation" i.e. a way to "return" from a block of code. > Here's > your code using condx rewritten to use let/ec instead: > yes very good idea , i already done that but it is in another function and will be packaged with it of course, i have a simple def macro that allow return in it definition: ;; scheme@(guile-user)> (def (foo) (when #t (return "hello") "bye"));; scheme@(guile-user)> (foo);; "hello" ;; (def x) ;; TODO: study def of a recursive function (define-syntax def (lambda (stx) (syntax-case stx () ;; multiple definitions without values assigned ;; (def (x y z)) ((_ (var1 ...)) #`(begin (define var1 '()) ...)) ;; (def (foo) (when #t (return "hello") "bye")) ((_ ( ...) * ...) (let ((ret-id (datum->syntax stx 'return))) #`(define ( ...) (call/cc (lambda (#,ret-id) * ...))))) ;; single definition without a value assigned ;; (def x) ((_ var) #`(define var '())) ;; (def x 7) ((_ var expr) #`(define var expr)) ((_ err ...) #`(syntax-error "Bad def form")) ))) and it allow (with some other extensions: declare , assignment operaotor <-, square bracket [ operator for arrays ) coding like that: ;; (subset-sum-dyna L-init t-init);; #t ;; there exist a solution (def (subset-sum-dyna L t) (declare ls dyn) ;; declare multiple variables {ls <- (length L)} {dyn <- dyna[ls t]} ;; dyna[ls][t] means : 0: unknown solution, 1: solution found, 2: no solution (if {dyn <> 0} ;; IF or WHEN : it is the same thing here (only one statement) (return (one? dyn))) (when (null? L) {dyna[ls t] <- 2} (return #f)) {c <+ (first L)} (when {c = t} ;; c is the solution {dyna[ls t] <- 1} (return #t)) {R <+ (rest L)} ;; continue searching a solution in the rest (declare s) (if {c > t} ;; c is to big to be a solution {s <- (subset-sum-dyna R t)} ;; c is part of the solution or c is not part of solution {s <- {(subset-sum-dyna R {t - c}) or (subset-sum-dyna R t)}}) {dyna[ls t] <- (one-two s)} s) ;; return value i will release all soon... Damien > (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 > > (let/ec return > > (when (not (zero? dyn)) > (return (one? dyn))) > > (when (null? L) > (array-set! dyna 2 ls t) > (return #f)) > > (define c (first L)) > > (when {c = t} > (array-set! dyna 1 ls t) > (return #t)) > > (define R (rest L)) > > ;; continue searching a solution in the rest > (when {c > t} > (define s (ssigma-proto R t)) > (array-set! dyna (one-two s) ls t) > (return s)) > > ;; else : > ;; c < t at this point > ;; c is part of the solution or his approximation > ;; or c is not part of solution > (define s {(ssigma-proto R {t - c}) or (ssigma-proto R t)}) > (array-set! dyna (one-two s) ls t) > (return s))) > > I've turned all 'condx' branches that weren't exec to use 'when' because it > felt more natural with the imperative style. Of course you could still use > the regular 'cond' here and there or the regular 'if'. 'When' is just > short > for an 'if' without an else part. > > The last '(return s)' could just be 's' but I find it more consistent and > readable to use 'return' there as well. > > -- > Taylan >