unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: 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 19:05:18 +0200	[thread overview]
Message-ID: <CADEOadeJY66usnK6Egem=qkSVpvtykXii70WeWuB23bu_aAyCw@mail.gmail.com> (raw)
In-Reply-To: <6d4ff570-1b0c-8158-a236-a8c00102f7e4@posteo.de>

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
>
>


  reply	other threads:[~2021-09-12 17:05 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 [this message]
2021-09-12 18:31     ` Taylan Kammer
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='CADEOadeJY66usnK6Egem=qkSVpvtykXii70WeWuB23bu_aAyCw@mail.gmail.com' \
    --to=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).