unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* cond(itionals) with optional execution of statements
@ 2021-09-11  9:14 Damien Mattei
  2021-09-12  9:41 ` Zelphir Kaltstahl
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Mattei @ 2021-09-11  9:14 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1603 bytes --]

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

[-- Attachment #2: condx.scm --]
[-- Type: application/octet-stream, Size: 652 bytes --]

;; condx: cond(itionals) with optional execution of statements before
;
; example:
;(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))
;
; 19

(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 ...)))))

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  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
  0 siblings, 1 reply; 7+ messages in thread
From: Zelphir Kaltstahl @ 2021-09-12  9:41 UTC (permalink / raw)
  To: Damien Mattei; +Cc: Guile User

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  2021-09-12  9:41 ` Zelphir Kaltstahl
@ 2021-09-12 17:05   ` Damien Mattei
  2021-09-12 18:31     ` Taylan Kammer
  2021-09-12 18:36     ` Zelphir Kaltstahl
  0 siblings, 2 replies; 7+ messages in thread
From: Damien Mattei @ 2021-09-12 17:05 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Jean-Paul Roy, Guile User, Damien Mattei

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  2021-09-12 17:05   ` Damien Mattei
@ 2021-09-12 18:31     ` Taylan Kammer
  2021-09-13  9:08       ` Damien Mattei
  2021-09-12 18:36     ` Zelphir Kaltstahl
  1 sibling, 1 reply; 7+ messages in thread
From: Taylan Kammer @ 2021-09-12 18:31 UTC (permalink / raw)
  To: Damien Mattei, Zelphir Kaltstahl; +Cc: Jean-Paul Roy, Guile User, Damien Mattei

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  2021-09-12 17:05   ` Damien Mattei
  2021-09-12 18:31     ` Taylan Kammer
@ 2021-09-12 18:36     ` Zelphir Kaltstahl
  2021-09-13 10:04       ` Damien Mattei
  1 sibling, 1 reply; 7+ messages in thread
From: Zelphir Kaltstahl @ 2021-09-12 18:36 UTC (permalink / raw)
  To: Damien Mattei; +Cc: Guile User

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. 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`?)

I guess the expressions in braces are using infix operations like the `or` or
`=` in the condition of `{c = t}`.

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
> <mailto: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
>     <https://notabug.org/ZelphirKaltstahl>
>
-- 
repositories: https://notabug.org/ZelphirKaltstahl



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  2021-09-12 18:31     ` Taylan Kammer
@ 2021-09-13  9:08       ` Damien Mattei
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Mattei @ 2021-09-13  9:08 UTC (permalink / raw)
  To: Taylan Kammer; +Cc: Jean-Paul Roy, Guile User, Damien Mattei

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 <taylan.kammer@gmail.com>
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"))
        ((_ (<name> <arg> ...) <body> <body>* ...)
         (let ((ret-id (datum->syntax stx 'return)))
           #`(define (<name> <arg> ...)
               (call/cc (lambda (#,ret-id) <body> <body>* ...)))))

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: cond(itionals) with optional execution of statements
  2021-09-12 18:36     ` Zelphir Kaltstahl
@ 2021-09-13 10:04       ` Damien Mattei
  0 siblings, 0 replies; 7+ messages in thread
From: Damien Mattei @ 2021-09-13 10:04 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Guile User

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


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2021-09-13 10:04 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2021-09-13  9:08       ` Damien Mattei
2021-09-12 18:36     ` Zelphir Kaltstahl
2021-09-13 10:04       ` Damien Mattei

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