unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Taylan Kammer <taylan.kammer@gmail.com>
To: Damien Mattei <damien.mattei@gmail.com>,
	Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
Cc: Jean-Paul Roy <jean-paul.roy@unice.fr>,
	Guile User <guile-user@gnu.org>,
	Damien Mattei <damien.mattei@univ-cotedazur.fr>
Subject: Re: cond(itionals) with optional execution of statements
Date: Sun, 12 Sep 2021 20:31:48 +0200	[thread overview]
Message-ID: <a03230fd-4630-ad02-2e06-4c7ecba4f36e@gmail.com> (raw)
In-Reply-To: <CADEOadeJY66usnK6Egem=qkSVpvtykXii70WeWuB23bu_aAyCw@mail.gmail.com>

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



  reply	other threads:[~2021-09-12 18:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-11  9:14 cond(itionals) with optional execution of statements Damien Mattei
2021-09-12  9:41 ` Zelphir Kaltstahl
2021-09-12 17:05   ` Damien Mattei
2021-09-12 18:31     ` Taylan Kammer [this message]
2021-09-13  9:08       ` Damien Mattei
2021-09-12 18:36     ` Zelphir Kaltstahl
2021-09-13 10:04       ` Damien Mattei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a03230fd-4630-ad02-2e06-4c7ecba4f36e@gmail.com \
    --to=taylan.kammer@gmail.com \
    --cc=damien.mattei@gmail.com \
    --cc=damien.mattei@univ-cotedazur.fr \
    --cc=guile-user@gnu.org \
    --cc=jean-paul.roy@unice.fr \
    --cc=zelphirkaltstahl@posteo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).