From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Taylan Kammer Newsgroups: gmane.lisp.guile.user Subject: Re: cond(itionals) with optional execution of statements Date: Sun, 12 Sep 2021 20:31:48 +0200 Message-ID: References: <6d4ff570-1b0c-8158-a236-a8c00102f7e4@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32073"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 Cc: Jean-Paul Roy , Guile User , Damien Mattei To: Damien Mattei , Zelphir Kaltstahl Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 12 20:32:22 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 1mPUH7-0008Dw-Uz for guile-user@m.gmane-mx.org; Sun, 12 Sep 2021 20:32:22 +0200 Original-Received: from localhost ([::1]:46178 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mPUH5-0002Mz-6Q for guile-user@m.gmane-mx.org; Sun, 12 Sep 2021 14:32:20 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58974) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mPUGg-0002Ln-Kd for guile-user@gnu.org; Sun, 12 Sep 2021 14:31:55 -0400 Original-Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]:37670) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mPUGe-0004IN-VR for guile-user@gnu.org; Sun, 12 Sep 2021 14:31:54 -0400 Original-Received: by mail-wr1-x436.google.com with SMTP id t8so6046440wrq.4 for ; Sun, 12 Sep 2021 11:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=sVpWdP1nAN9MgBqYZC+u4bwGtAIZbqM9CVwZrbR4dcw=; b=XYs1qnc4oQHd6hP4y/0FwL1/+sd7dm6EPDhOYGdmQkl4+2y05QXy65DSF3+Rr7wajF 0oJoFh0uMwkAvol+pJhWMO7W8nMB6xEBpWEvEhjBR+onsf+BTZjkmbvgYtJS+hL3y2rd yMzfJBjv/ymkLpjMnBtRBYTVHEa9rZCFVvY7BEUnzAbowSugUkzAwRhnEqGCdSm+57vw fLJCziK+uGARMCoLC/CumCTwNDnRLhnVjqxZXmouWo2KuxvOcYK7HJ7s7z3dmnpLmkI1 MOnzSXZCqVwPYsiRQPlcnFafOn+Grn5Ca7St/CCRE4ELNVNcQflH1q1b57A/tER9FjAH kreg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=sVpWdP1nAN9MgBqYZC+u4bwGtAIZbqM9CVwZrbR4dcw=; b=QanUc7FxAM+J9fNZmTPdoukcD5VBvf8n4I56+5mTJr38pdIg6t9w6dn4OFgJzvIymX g2aUm5Gw08PummhytLKcFY3/T+hRfT+VYg6BgG65Flx5OAM089HwARiGswBhGxPjccV6 jG7+UylaEL9l0OMnVrAfivQoYw9Oy63GaXgAfWmiM5LCCCTQONkWRnkfnBsaOLvaM12c ZMdkP0iK/3reeqOIGdV4zf+KZo7kQ9HbpXz38dISH3VPdRNTrOMttugasN3ofvyE8MZF /MtjJ5wxl88xC4SejM7e9OtBH/M01GIKOjbm8i+IogZV/rJDXjD9FeeFHiQa/PQS0DIH Su/A== X-Gm-Message-State: AOAM531usRdJlZxINv1gyg+0G+xnm3kBBP+fdkumFtcaht8xot/HYGYh SreJTsF12JB+kIMUWObBS7U= X-Google-Smtp-Source: ABdhPJwUq8HcDA0378MC2orEvB/nxh9ETgv/ebEh6TH0BOVEROJqXzpmaRQhGlF7MJ6lanz1LebF0g== X-Received: by 2002:adf:ce03:: with SMTP id p3mr8693948wrn.261.1631471510251; Sun, 12 Sep 2021 11:31:50 -0700 (PDT) Original-Received: from [192.168.178.20] (b2b-109-90-125-150.unitymedia.biz. [109.90.125.150]) by smtp.gmail.com with ESMTPSA id d5sm5403979wra.38.2021.09.12.11.31.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 12 Sep 2021 11:31:49 -0700 (PDT) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=taylan.kammer@gmail.com; helo=mail-wr1-x436.google.com X-Spam_score_int: -56 X-Spam_score: -5.7 X-Spam_bar: ----- X-Spam_report: (-5.7 / 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, NICE_REPLY_A=-3.584, 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.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:17729 Archived-At: 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.) 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." 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. 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)) ; => 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: (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