unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* define anywhere
@ 2021-06-04 22:27 Damien Mattei
  2021-06-05  2:10 ` Taylan Kammer
  2021-06-05 15:23 ` Linus Björnstam
  0 siblings, 2 replies; 14+ messages in thread
From: Damien Mattei @ 2021-06-04 22:27 UTC (permalink / raw)
  To: guile-user

hello,
i'm was considering that i want to be able to define a variable anywhere in
code, the way Python do. Few scheme can do that (Bigloo i know)
( the list here is not exact:
https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
)
 is it possible in guile to do it? i do not know, so could it be added to
the language specification for future release?

regards,
Damien


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

* Re: define anywhere
  2021-06-04 22:27 define anywhere Damien Mattei
@ 2021-06-05  2:10 ` Taylan Kammer
  2021-06-05  8:50   ` Dr. Arne Babenhauserheide
  2021-06-05 15:23 ` Linus Björnstam
  1 sibling, 1 reply; 14+ messages in thread
From: Taylan Kammer @ 2021-06-05  2:10 UTC (permalink / raw)
  To: Damien Mattei, guile-user

On 05.06.2021 00:27, Damien Mattei wrote:
> hello,
> i'm was considering that i want to be able to define a variable anywhere in
> code, the way Python do. Few scheme can do that (Bigloo i know)
> ( the list here is not exact:
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> )
>  is it possible in guile to do it? i do not know, so could it be added to
> the language specification for future release?
> 
> regards,
> Damien
> 

It's possible since 3.0, see here:

https://www.gnu.org/software/guile/manual/html_node/Internal-Definitions.html#Internal-Definitions

-- 
Taylan



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

* Re: define anywhere
  2021-06-05  2:10 ` Taylan Kammer
@ 2021-06-05  8:50   ` Dr. Arne Babenhauserheide
  0 siblings, 0 replies; 14+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-06-05  8:50 UTC (permalink / raw)
  To: Taylan Kammer; +Cc: guile-user

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


Taylan Kammer <taylan.kammer@gmail.com> writes:

> On 05.06.2021 00:27, Damien Mattei wrote:
>> hello,
>> i'm was considering that i want to be able to define a variable anywhere in
>> code, the way Python do. Few scheme can do that (Bigloo i know)
>> ( the list here is not exact:
>> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
>> )
>>  is it possible in guile to do it? i do not know, so could it be added to
>> the language specification for future release?
>> 
>> regards,
>> Damien
>> 
>
> It's possible since 3.0, see here:
>
> https://www.gnu.org/software/guile/manual/html_node/Internal-Definitions.html#Internal-Definitions

It is currently possible almost anywhere:

Works:

(define (a) (when #f #t) (define a 'b) a)

Does not work:

(define (a) (when #t (define a 'b) a))

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

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

* Re: define anywhere
  2021-06-04 22:27 define anywhere Damien Mattei
  2021-06-05  2:10 ` Taylan Kammer
@ 2021-06-05 15:23 ` Linus Björnstam
  2021-06-06  8:09   ` Linus Björnstam
                     ` (3 more replies)
  1 sibling, 4 replies; 14+ messages in thread
From: Linus Björnstam @ 2021-06-05 15:23 UTC (permalink / raw)
  To: Damien Mattei, guile-user

I implemented this hack before guile 3 got defines in function bodies: https://hg.sr.ht/~bjoli/guile-define

Even I guile 3 it allows a more liberal placement of define, but it won't work for things like bodies of imported macros (like match)
-- 
  Linus Björnstam

On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> hello,
> i'm was considering that i want to be able to define a variable 
> anywhere in
> code, the way Python do. Few scheme can do that (Bigloo i know)
> ( the list here is not exact:
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> )
>  is it possible in guile to do it? i do not know, so could it be added 
> to
> the language specification for future release?
> 
> regards,
> Damien
> 



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

* Re: define anywhere
  2021-06-05 15:23 ` Linus Björnstam
@ 2021-06-06  8:09   ` Linus Björnstam
  2021-06-06 12:02     ` Dr. Arne Babenhauserheide
       [not found]   ` <CADEOadcwk1D9of6C1owQQSqFNRvUmA=wJKSngySbGsfdDUzoMw@mail.gmail.com>
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 14+ messages in thread
From: Linus Björnstam @ 2021-06-06  8:09 UTC (permalink / raw)
  To: Damien Mattei, guile-user

Note however, that I seem to have forgotten when and unless. also: cond has no support for the extended "test guard => lambda" form. Neither do I believe that the any "special" case form is supported. If there is any interest whatsoever, I can implement it when I have time.

The code produced is NOT the same as guile3 does for internal defines, however. 

(define a 2)
(define b 3)
(display "hej")
(define c 3)
(+ a b c)

becomes ONE letrec under gulie3, whereas my library turns it into
(letrec ((a 2) (b 3))
  (display "hej")
  (letrec ((c 3))
    (+ a b c)))

That should be an easy fix, again if there is any interest.
-- 
  Linus Björnstam

On Sat, 5 Jun 2021, at 17:23, Linus Björnstam wrote:
> I implemented this hack before guile 3 got defines in function bodies: 
> https://hg.sr.ht/~bjoli/guile-define
> 
> Even I guile 3 it allows a more liberal placement of define, but it 
> won't work for things like bodies of imported macros (like match)
> -- 
>   Linus Björnstam
> 
> On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > hello,
> > i'm was considering that i want to be able to define a variable 
> > anywhere in
> > code, the way Python do. Few scheme can do that (Bigloo i know)
> > ( the list here is not exact:
> > https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > )
> >  is it possible in guile to do it? i do not know, so could it be added 
> > to
> > the language specification for future release?
> > 
> > regards,
> > Damien
> > 
> 
> 



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

* Re: define anywhere
  2021-06-06  8:09   ` Linus Björnstam
@ 2021-06-06 12:02     ` Dr. Arne Babenhauserheide
  2021-06-06 13:36       ` Linus Björnstam
  0 siblings, 1 reply; 14+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-06-06 12:02 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-user

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


Linus Björnstam <linus.internet@fastmail.se> writes:

> becomes ONE letrec under gulie3, whereas my library turns it into
> (letrec ((a 2) (b 3))
>   (display "hej")
>   (letrec ((c 3))
>     (+ a b c)))
>
> That should be an easy fix, again if there is any interest.

I’m not sure which approach I prefer. Your approach is more precise, but
I slightly lean towards the Guile3-version, because it does not change
behaviour when I put a pretty-print between two defines.

Though I would want a precise approach inside other forms (like when:
separating inside when and outside when).

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

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

* Re: define anywhere
  2021-06-06 12:02     ` Dr. Arne Babenhauserheide
@ 2021-06-06 13:36       ` Linus Björnstam
  2021-06-06 16:45         ` Dr. Arne Babenhauserheide
  2021-06-11  8:08         ` Damien Mattei
  0 siblings, 2 replies; 14+ messages in thread
From: Linus Björnstam @ 2021-06-06 13:36 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide; +Cc: guile-user

Oh, my version is an error. I implemented the macro before definitions in (some) expression contexts were a thing.

Andy did a marvellous thing implementinf the new letrec: if it is possible, letrec will have no overhead. It also automatically handle dependant clauses: even though it might seem counterintuitive, the guile-proper version will do the right thing always. The syntactic differentiation mine does is just there for our feeble human minds, at least with regards to what guile-proper is doing.

So TL/DR: guiles way is the correct way. 

-- 
  Linus Björnstam

On Sun, 6 Jun 2021, at 14:02, Dr. Arne Babenhauserheide wrote:
> 
> Linus Björnstam <linus.internet@fastmail.se> writes:
> 
> > becomes ONE letrec under gulie3, whereas my library turns it into
> > (letrec ((a 2) (b 3))
> >   (display "hej")
> >   (letrec ((c 3))
> >     (+ a b c)))
> >
> > That should be an easy fix, again if there is any interest.
> 
> I’m not sure which approach I prefer. Your approach is more precise, but
> I slightly lean towards the Guile3-version, because it does not change
> behaviour when I put a pretty-print between two defines.
> 
> Though I would want a precise approach inside other forms (like when:
> separating inside when and outside when).
> 
> Best wishes,
> Arne
> -- 
> Unpolitisch sein
> heißt politisch sein
> ohne es zu merken
> 
> Attachments:
> * signature.asc



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

* Re: define anywhere
  2021-06-06 13:36       ` Linus Björnstam
@ 2021-06-06 16:45         ` Dr. Arne Babenhauserheide
  2021-06-11  8:08         ` Damien Mattei
  1 sibling, 0 replies; 14+ messages in thread
From: Dr. Arne Babenhauserheide @ 2021-06-06 16:45 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-user

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


Linus Björnstam <linus.internet@fastmail.se> writes:

> Andy did a marvellous thing
> So TL/DR: guiles way is the correct way. 

Thank you for the clarification!

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

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

* Re: define anywhere
       [not found]     ` <28842df5-d82b-4819-b7ad-2148265f7a83@www.fastmail.com>
@ 2021-06-09  9:29       ` Damien Mattei
  2021-06-09 10:11         ` Linus Björnstam
  0 siblings, 1 reply; 14+ messages in thread
From: Damien Mattei @ 2021-06-09  9:29 UTC (permalink / raw)
  To: Linus Björnstam, guile-user; +Cc: Jean-Paul Roy

hello, i'm just answering now  because my ten years old Mac book pro
definitely died sunday evening RIP

i was trying to make macro that set! variable if they exist or create it
before if the variable is not defined (tested by the Guile procedure
'defined?' which is not R*RS in any version)

but it seems that Guile 3.0 'define' can not create variable in local
blocks, as in Python :
(if test
    (begin
         (define x 7)
         (foo x))
     (else-block))
will not work.

This behavior force the definition of all variable at the upper level of
the function, the only advantage of guile 3.0 is perhaps (i have not tested
it fully) that you can put define not only at beginning of function.

This would be interesting to be able to define local variables with
'define',defining variable only in the block they are usefull.

With those limitation in mind i defined a few more macros:
(define-syntax <+
  (syntax-rules ()
    ((_ var expr) (define var expr))))

{ x <+ 7 } is equivalent to : (<- x 7) or (define x 7)

:=  is the same as <+
(define-syntax :=
  (syntax-rules ()
    ((_ var expr) (define var expr))))

{ x := 7 } is equivalent to : (:= x 7) or (define x 7)

;; definition without a value assigned
;;  (def x)
(define-syntax def
  (syntax-rules ()
    ((_ var) (define var '()))))

i added all that to my previous macro system in my Scheme+ (unfortunately
my github account is on my dead mac,so no commit for now)

i do not think i can do more for now, define anywhere in code could be a
good solution to let letrec let* that in my opinion should no longer exist
in Scheme.

Here is an example on the famous sub set sum problem algorithm  in two
versions one with define 'anywhere' and the other with my previous macro of
Scheme+:
scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889 1040
1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027))
scheme@(guile-user)> (start-ssigma-sol-approx-pack-define-anywhere L-init
19836)
$1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 5027)
scheme@(guile-user)> (apply + '(1 3 4 16 17 256 275 723 1040 1041 1284 1344
1520 3000 4285 5027))
$2 = 19836

code:

*Preview:*

(define (start-ssigma-sol-approx-pack-define-anywhere L t) ;; Sub Set
Sum problem (find solution or approximation)
  ;; { } are for infix notation as defined in SRFI 105
  ;; <+ and := are equivalent to (define var value)
  { best-sol <+ (lambda (L1 L2)
		
		  {s1 <+ (apply + L1)}
		  {s2 <+ (apply + L2)}
		
		  (if {(abs {t - s1}) <= (abs {t - s2})}
		      L1
		      L2)) }

  ;; := is the same macro as <+
  { best-sol3 := (lambda (L1 L2 L3)
		
		   {L22 <+ (best-sol L2 L3)}
		   (best-sol L1 L22)) }


  { ssigma-sol-approx <+ (lambda (L)
			   ;; def is a macro for declared but unasigned variable, it is
same as (define var '())
			   (def c)
			   (def R)
			
			   (if (null? L)
			       L
			       (begin {c <- (first L)}
				      {R <- (rest L)}
				
				      (cond [ {c = t} (list c) ] ;; c is the solution
					    [ {c > t} (best-sol (list c) (ssigma-sol-approx R)) ] ;; c is
to big to be a solution but could be an approximation
					    ;; c < t at this point, 3 possibilities :
					    ;; c is the best solution
					    ;; c is part of the solution or his approximation
					    ;; or c is not part of solution or his approximation
					    [ else (best-sol3 (list c) ;; c is the best solution
							
							      ;; c part of solution or is approximation
							      (cons c (start-ssigma-sol-approx-pack-define-anywhere R
{t - c})) ;; we have to find a solution or an approximation for t-c
now
									
							      ;; c is not part of solution or his approximation
							      (ssigma-sol-approx R))])))) }
		
	
  ;; start the function
  (ssigma-sol-approx L))


with other macros of Scheme+ code looked like this:


*Preview:*

;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723
889 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027));;
scheme@(guile-user)> (start-ssigma-sol-approx-pack L-init 19836);; $1
= (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 5027)
(define (start-ssigma-sol-approx-pack L t) ;; Sub Set Sum problem
(find solution or approximation)


  (letrec-arrow* [ best-sol ← (lambda (L1 L2)
			
				(let-arrow*  [ s1 ← (apply + L1)
					       s2 ← (apply + L2) ]
				
					     (if {(abs {t - s1}) <= (abs {t - s2})}
						 L1
						 L2)))

		   best-sol3 ← (lambda (L1 L2 L3)
			
				 (let [(L22 (best-sol L2 L3))]
				   (best-sol L1 L22)))
	
	           ssigma-sol-approx ← (lambda (L)
				     				
				      (if (null? L)
					
					  L
					
					  (let-arrow* [ c ← (first L)
						        R ← (rest L)  ]
					
					    (cond [ {c = t} (list c) ] ;; c is the solution
						  [ {c > t} (best-sol (list c) (ssigma-sol-approx R)) ] ;; c is
to big to be a solution but could be an approximation
						  ;; c < t at this point, 3 possibilities :
						  ;; c is the best solution
						  ;; c is part of the solution or his approximation
						  ;; or c is not part of solution or his approximation
						  [ else (best-sol3 (list c) ;; c is the best solution
								
								    ;;(begin
								      ;; (display "append c=") (display c) (newline)
								    ;; c part of solution or is approximation
								    (cons c (start-ssigma-sol-approx-pack R {t - c}));;) ;; we
have to find a solution or an approximation for t-c now
								
								    ;; c is not part of solution or his approximation
								    (ssigma-sol-approx R))]))))

	
	       ]

	     ;; start the function
	     (ssigma-sol-approx L)))



the use of define anywhere will make code more readable, infixe notation
and macros too...

A last example in Scheme+ with arrays on the dynamic solution of subset sum
problem (just answer if a solution exist or not):

*Preview:*

(include "../library-FunctProg/first-and-rest.scm")(include
"../library-FunctProg/guile/array.scm")(include
"../library-FunctProg/pair.scm")(include
"../library-FunctProg/number.scm")(include
"../library-FunctProg/list.scm")(include
"../library-FunctProg/let.scm")

(define L-init '(1 3 4 16 17 24 45 64 197 256 275 323 540 723 889 915
1040 1041 1093 1099 1111 1284 1344 1520 2027 2500 2734 3000 3267 3610
4285 5027))
(define t-init 35267)
(define ls (length L-init))
(define dyna (make-array 0 {ls + 1} {t-init + 1}))
(define (one-two b)
  (if b 1 2))
(define cpt 0)
;; scheme@(guile-user)> (ssigma-dyna-define-anywhere L-init t-init);;
$1 = #t;; scheme@(guile-user)> cpt;; $2 = 147801(define
(ssigma-dyna-define-anywhere L t)

  {cpt <- {cpt + 1}} ;; cpt is defined at toplevel

  ;;(display L) (display " ") (display t) (newline)

  {ls <+ (length L)}
  {dyn <+ {dyna[ls t]}}

  (def c)
  (def R)

  ;; dyna[ls t] means 0: unknown solution, 1: solution found, 2: no solution
  (one?
    (if (not (zero? dyn))
	
	dyn
	
	;; set the array but return the variable
	{ dyna[ls t] <- (one-two
			  (if (null? L)
			      #f
			      (begin
				{c <- (first L)}
				{R <- (rest L)}
				(cond [ {c = t} #t ] ;; c is the solution
				      [ {c > t} (ssigma-dyna-define-anywhere R t) ] ;; c is to big
to be a solution but can be an approximation
				      ;; c < t at this point
				      ;; c is part of the solution or his approximation
				      ;; or c is not part of solution or his approximation
				      [ else {(ssigma-dyna-define-anywhere R {t - c}) or
(ssigma-dyna-define-anywhere R t)} ] )))) } )))



Damien


On Sun, Jun 6, 2021 at 7:32 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:

> Try
> ,expand (new-set! X 7)  at the repl.
>
> That will however not do what you want. define does not work like that.
> Define adds the binding in what is called the current lexical environment:
> a define inside an if will not be visible outside that if (even if it would
> have been valid syntax).
>
> What are you trying to write?
>
> --
>   Linus Björnstam
>
> On Sun, 6 Jun 2021, at 17:16, Damien Mattei wrote:
> > macros... i can not figure out how to make this one works:
> > (define-syntax new-set!
> >   (syntax-rules ()
> >      ((_ var expr)  (if (defined? (quote var))
> >                                (set! var expr)
> >                                (define var expr)))))
> >
> > scheme@(guile-user)> (new-set! x 7)
> > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > ice-9/boot-9.scm:1669:16: In procedure raise-exception:
> > Unbound variable: x
> >
> > Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> > scheme@(guile-user) [1]> ,bt
> > In current input:
> >      24:0  1 (_)
> > In ice-9/boot-9.scm:
> >   1669:16  0 (raise-exception _ #:continuable? _)
> >
> > any idea?
> >
> > Damien
> >
> > On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam
> > <linus.internet@fastmail.se> wrote:
> > > I implemented this hack before guile 3 got defines in function bodies:
> https://hg.sr.ht/~bjoli/guile-define
> > >
> > > Even I guile 3 it allows a more liberal placement of define, but it
> won't work for things like bodies of imported macros (like match)
> > > --
> > >   Linus Björnstam
> > >
> > > On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > > > hello,
> > > > i'm was considering that i want to be able to define a variable
> > > > anywhere in
> > > > code, the way Python do. Few scheme can do that (Bigloo i know)
> > > > ( the list here is not exact:
> > > >
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > > > )
> > > >  is it possible in guile to do it? i do not know, so could it be
> added
> > > > to
> > > > the language specification for future release?
> > > >
> > > > regards,
> > > > Damien
> > > >
>


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

* Re: define anywhere
  2021-06-09  9:29       ` Damien Mattei
@ 2021-06-09 10:11         ` Linus Björnstam
  2021-06-11  8:15           ` Damien Mattei
  0 siblings, 1 reply; 14+ messages in thread
From: Linus Björnstam @ 2021-06-09 10:11 UTC (permalink / raw)
  To: Damien Mattei, guile-user; +Cc: Jean-Paul Roy

Hi Damien!

The problem that define only defines a variable in the current lexical environment. Even if a definition in an if block would work from a syntactical standpoint, it would be pointless.

As per r6rs, internal (define ...) are akin to letrec, but are only allowed in definition context. Guile 3 relaxes this for function bodies, whereas the macros I linked above relaxes this for some additional forms. It cannot however "export" bindings to any outer environment.

What you can do is use module reflection to create top-level variables in the current module, but that makes everyone flinch about 105% of the time. 

-- 
  Linus Björnstam

On Wed, 9 Jun 2021, at 11:29, Damien Mattei wrote:
> hello, i'm just answering now  because my ten years old Mac book pro 
> definitely died sunday evening RIP 
> 
> i was trying to make macro that set! variable if they exist or create 
> it before if the variable is not defined (tested by the Guile procedure 
> 'defined?' which is not R*RS in any version)
> 
> but it seems that Guile 3.0 'define' can not create variable in local 
> blocks, as in Python :
> (if test 
>     (begin
>          (define x 7)
>          (foo x))
>      (else-block))
> will not work.
> 
> This behavior force the definition of all variable at the upper level 
> of the function, the only advantage of guile 3.0 is perhaps (i have not 
> tested it fully) that you can put define not only at beginning of 
> function.
> 
> This would be interesting to be able to define local variables with 
> 'define',defining variable only in the block they are usefull.
> 
> With those limitation in mind i defined a few more macros:
> (define-syntax <+
>   (syntax-rules ()
>     ((_ var expr) (define var expr))))
> 
> { x <+ 7 } is equivalent to : (<- x 7) or (define x 7)
> 
> :=  is the same as <+
> (define-syntax :=
>   (syntax-rules ()
>     ((_ var expr) (define var expr))))
> 
> { x := 7 } is equivalent to : (:= x 7) or (define x 7)
> 
> ;; definition without a value assigned
> ;;  (def x) 
> (define-syntax def
>   (syntax-rules ()
>     ((_ var) (define var '()))))
> 
> i added all that to my previous macro system in my Scheme+ 
> (unfortunately my github account is on my dead mac,so no commit for now)
> 
> i do not think i can do more for now, define anywhere in code could be 
> a good solution to let letrec let* that in my opinion should no longer 
> exist in Scheme.
> 
> Here is an example on the famous sub set sum problem algorithm  in two 
> versions one with define 'anywhere' and the other with my previous 
> macro of Scheme+:
> scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889 
> 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027))
> scheme@(guile-user)> (start-ssigma-sol-approx-pack-define-anywhere 
> L-init 19836)
> $1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 5027)
> scheme@(guile-user)> (apply + '(1 3 4 16 17 256 275 723 1040 1041 1284 
> 1344 1520 3000 4285 5027))
> $2 = 19836
> 
> code:
> *Preview:* 
> 
> (define (start-ssigma-sol-approx-pack-define-anywhere L t) ;; Sub Set 
> Sum problem (find solution or approximation)
>   ;; { } are for infix notation as defined in SRFI 105
>   ;; <+ and := are equivalent to (define var value) 
>   { best-sol <+ (lambda (L1 L2)
> 		  
> 		  {s1 <+ (apply + L1)}
> 		  {s2 <+ (apply + L2)}
> 		  
> 		  (if {(abs {t - s1}) <= (abs {t - s2})}
> 		      L1
> 		      L2)) }
> 
>   ;; := is the same macro as <+
>   { best-sol3 := (lambda (L1 L2 L3)
> 		   
> 		   {L22 <+ (best-sol L2 L3)}
> 		   (best-sol L1 L22)) }
> 
>   
>   { ssigma-sol-approx <+ (lambda (L)
> 			   ;; def is a macro for declared but unasigned variable, it is same 
> as (define var '())
> 			   (def c) 
> 			   (def R)
> 			   
> 			   (if (null? L)
> 			       L
> 			       (begin {c <- (first L)}
> 				      {R <- (rest L)}
> 				      
> 				      (cond [ {c = t} (list c) ] ;; c is the solution
> 					    [ {c > t} (best-sol (list c) (ssigma-sol-approx R)) ] ;; c is 
> to big to be a solution but could be an approximation
> 					    ;; c < t at this point, 3 possibilities :
> 					    ;; c is the best solution
> 					    ;; c is part of the solution or his approximation
> 					    ;; or c is not part of solution or his approximation
> 					    [ else (best-sol3 (list c) ;; c is the best solution
> 							      
> 							      ;; c part of solution or is approximation
> 							      (cons c (start-ssigma-sol-approx-pack-define-anywhere R {t 
> - c})) ;; we have to find a solution or an approximation for t-c now
> 									    
> 							      ;; c is not part of solution or his approximation
> 							      (ssigma-sol-approx R))])))) }
> 		   
> 	       
>   ;; start the function
>   (ssigma-sol-approx L))
> *
> *
> 
> with other macros of Scheme+ code looked like this:*
> *
> 
> *
> *
> 
> *Preview:* 
> 
> ;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889 
> 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027))
> ;; scheme@(guile-user)> (start-ssigma-sol-approx-pack L-init 19836)
> ;; $1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 
> 5027)
> 
> (define (start-ssigma-sol-approx-pack L t) ;; Sub Set Sum problem (find 
> solution or approximation)
>  
> 
>   (letrec-arrow* [ best-sol ← (lambda (L1 L2)
> 			   
> 				(let-arrow*  [ s1 ← (apply + L1)
> 					       s2 ← (apply + L2) ]
> 				    
> 					     (if {(abs {t - s1}) <= (abs {t - s2})}
> 						 L1
> 						 L2)))
> 
> 		   best-sol3 ← (lambda (L1 L2 L3)
> 			     
> 				 (let [(L22 (best-sol L2 L3))]
> 				   (best-sol L1 L22)))
> 	       
> 	           ssigma-sol-approx ← (lambda (L)
> 				     				      
> 				      (if (null? L)
> 					  
> 					  L
> 					  
> 					  (let-arrow* [ c ← (first L)
> 						        R ← (rest L)  ]
> 					    
> 					    (cond [ {c = t} (list c) ] ;; c is the solution
> 						  [ {c > t} (best-sol (list c) (ssigma-sol-approx R)) ] ;; c is 
> to big to be a solution but could be an approximation
> 						  ;; c < t at this point, 3 possibilities :
> 						  ;; c is the best solution
> 						  ;; c is part of the solution or his approximation
> 						  ;; or c is not part of solution or his approximation
> 						  [ else (best-sol3 (list c) ;; c is the best solution
> 								    
> 								    ;;(begin
> 								      ;; (display "append c=") (display c) (newline)
> 								    ;; c part of solution or is approximation
> 								    (cons c (start-ssigma-sol-approx-pack R {t - c}));;) ;; we 
> have to find a solution or an approximation for t-c now
> 								    
> 								    ;; c is not part of solution or his approximation
> 								    (ssigma-sol-approx R))]))))
> 
> 	       
> 	       ]
> 
> 	     ;; start the function
> 	     (ssigma-sol-approx L)))
> 
> 
> the use of define anywhere will make code more readable, infixe 
> notation and macros too...
> 
> A last example in Scheme+ with arrays on the dynamic solution of subset 
> sum problem (just answer if a solution exist or not):
> 
> *Preview:* 
> 
> (include "../library-FunctProg/first-and-rest.scm")
> (include "../library-FunctProg/guile/array.scm")
> (include "../library-FunctProg/pair.scm")
> (include "../library-FunctProg/number.scm")
> (include "../library-FunctProg/list.scm")
> (include "../library-FunctProg/let.scm")
> 
> 
> (define L-init '(1 3 4 16 17 24 45 64 197 256 275 323 540 723 889 915 
> 1040 1041 1093 1099 1111 1284 1344 1520 2027 2500 2734 3000 3267 3610 
> 4285 5027))
> 
> (define t-init 35267)
> 
> (define ls (length L-init))
> 
> (define dyna (make-array 0 {ls + 1} {t-init + 1}))
> 
> (define (one-two b)
>   (if b 1 2))
> 
> (define cpt 0)
> 
> ;; scheme@(guile-user)> (ssigma-dyna-define-anywhere L-init t-init)
> ;; $1 = #t
> ;; scheme@(guile-user)> cpt
> ;; $2 = 147801
> (define (ssigma-dyna-define-anywhere L t)
> 
>   {cpt <- {cpt + 1}} ;; cpt is defined at toplevel
>   
>   ;;(display L) (display " ") (display t) (newline)
>   
>   {ls <+ (length L)}
>   {dyn <+ {dyna[ls t]}}
> 
>   (def c)
>   (def R)
>   
>   ;; dyna[ls t] means 0: unknown solution, 1: solution found, 2: no solution
>   (one?
>     (if (not (zero? dyn))
> 	
> 	dyn
> 	
> 	;; set the array but return the variable
> 	{ dyna[ls t] <- (one-two
> 			  (if (null? L)
> 			      #f
> 			      (begin
> 				{c <- (first L)}
> 				{R <- (rest L)}
> 				(cond [ {c = t} #t ] ;; c is the solution
> 				      [ {c > t} (ssigma-dyna-define-anywhere R t) ] ;; c is to big 
> to be a solution but can be an approximation
> 				      ;; c < t at this point
> 				      ;; c is part of the solution or his approximation
> 				      ;; or c is not part of solution or his approximation
> 				      [ else {(ssigma-dyna-define-anywhere R {t - c}) or 
> (ssigma-dyna-define-anywhere R t)} ] )))) } )))
> 
> 
> Damien
> 
> 
> On Sun, Jun 6, 2021 at 7:32 PM Linus Björnstam 
> <linus.internet@fastmail.se> wrote:
> > Try 
> > ,expand (new-set! X 7)  at the repl. 
> > 
> > That will however not do what you want. define does not work like that. Define adds the binding in what is called the current lexical environment: a define inside an if will not be visible outside that if (even if it would have been valid syntax).
> > 
> > What are you trying to write?
> > 
> > -- 
> >   Linus Björnstam
> > 
> > On Sun, 6 Jun 2021, at 17:16, Damien Mattei wrote:
> > > macros... i can not figure out how to make this one works:
> > > (define-syntax new-set!
> > >   (syntax-rules ()
> > >      ((_ var expr)  (if (defined? (quote var))
> > >                                (set! var expr)
> > >                                (define var expr))))) 
> > > 
> > > scheme@(guile-user)> (new-set! x 7)
> > > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > > ice-9/boot-9.scm:1669:16: In procedure raise-exception:
> > > Unbound variable: x
> > > 
> > > Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> > > scheme@(guile-user) [1]> ,bt
> > > In current input:
> > >      24:0  1 (_)
> > > In ice-9/boot-9.scm:
> > >   1669:16  0 (raise-exception _ #:continuable? _)
> > > 
> > > any idea?
> > > 
> > > Damien
> > > 
> > > On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam 
> > > <linus.internet@fastmail.se> wrote:
> > > > I implemented this hack before guile 3 got defines in function bodies: https://hg.sr.ht/~bjoli/guile-define
> > > > 
> > > > Even I guile 3 it allows a more liberal placement of define, but it won't work for things like bodies of imported macros (like match)
> > > > -- 
> > > >   Linus Björnstam
> > > > 
> > > > On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > > > > hello,
> > > > > i'm was considering that i want to be able to define a variable 
> > > > > anywhere in
> > > > > code, the way Python do. Few scheme can do that (Bigloo i know)
> > > > > ( the list here is not exact:
> > > > > https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > > > > )
> > > > >  is it possible in guile to do it? i do not know, so could it be added 
> > > > > to
> > > > > the language specification for future release?
> > > > > 
> > > > > regards,
> > > > > Damien
> > > > > 



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

* Re: define anywhere
  2021-06-05 15:23 ` Linus Björnstam
  2021-06-06  8:09   ` Linus Björnstam
       [not found]   ` <CADEOadcwk1D9of6C1owQQSqFNRvUmA=wJKSngySbGsfdDUzoMw@mail.gmail.com>
@ 2021-06-11  8:07   ` Damien Mattei
  2021-06-11 23:43   ` Damien Mattei
  3 siblings, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2021-06-11  8:07 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-user

i did not noticed there was an attachment link :-(
i read your code, great work...
i will read it more later
Damien

On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:

> I implemented this hack before guile 3 got defines in function bodies:
> https://hg.sr.ht/~bjoli/guile-define
>
> Even I guile 3 it allows a more liberal placement of define, but it won't
> work for things like bodies of imported macros (like match)
> --
>   Linus Björnstam
>
> On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > hello,
> > i'm was considering that i want to be able to define a variable
> > anywhere in
> > code, the way Python do. Few scheme can do that (Bigloo i know)
> > ( the list here is not exact:
> >
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > )
> >  is it possible in guile to do it? i do not know, so could it be added
> > to
> > the language specification for future release?
> >
> > regards,
> > Damien
> >
>


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

* Re: define anywhere
  2021-06-06 13:36       ` Linus Björnstam
  2021-06-06 16:45         ` Dr. Arne Babenhauserheide
@ 2021-06-11  8:08         ` Damien Mattei
  1 sibling, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2021-06-11  8:08 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-user

On Sun, Jun 6, 2021 at 3:36 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:

> Oh, my version is an error. I implemented the macro before definitions in
> (some) expression contexts were a thing.
>

do you talk about what? the linked code?

>
> Andy did a marvellous thing implementinf the new letrec: if it is
> possible, letrec will have no overhead. It also automatically handle
> dependant clauses: even though it might seem counterintuitive, the
> guile-proper version will do the right thing always. The syntactic
> differentiation mine does is just there for our feeble human minds, at
> least with regards to what guile-proper is doing.
>
> So TL/DR: guiles way is the correct way.
>
> --
>   Linus Björnstam
>
> On Sun, 6 Jun 2021, at 14:02, Dr. Arne Babenhauserheide wrote:
> >
> > Linus Björnstam <linus.internet@fastmail.se> writes:
> >
> > > becomes ONE letrec under gulie3, whereas my library turns it into
> > > (letrec ((a 2) (b 3))
> > >   (display "hej")
> > >   (letrec ((c 3))
> > >     (+ a b c)))
> > >
> > > That should be an easy fix, again if there is any interest.
> >
> > I’m not sure which approach I prefer. Your approach is more precise, but
> > I slightly lean towards the Guile3-version, because it does not change
> > behaviour when I put a pretty-print between two defines.
> >
> > Though I would want a precise approach inside other forms (like when:
> > separating inside when and outside when).
> >
> > Best wishes,
> > Arne
> > --
> > Unpolitisch sein
> > heißt politisch sein
> > ohne es zu merken
> >
> > Attachments:
> > * signature.asc
>


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

* Re: define anywhere
  2021-06-09 10:11         ` Linus Björnstam
@ 2021-06-11  8:15           ` Damien Mattei
  0 siblings, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2021-06-11  8:15 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: Jean-Paul Roy, guile-user

about :
https://www.gnu.org/software/guile/manual/html_node/Module-System-Reflection.html
i do not want to implement things that are not portable to others schemes
using standart macros and SRFI,
for now my Scheme+ is a set of macros portable to other scheme and use only
SRFI if they are implemented in a particular Scheme
I use Guile and not Racket ot Bigloo because Racket do not use the standart
SRFI 105 infix notation, and Bigloo or Kawa have no implementation of infix
SRFI 105.

Damien

On Wed, Jun 9, 2021 at 12:11 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:

> Hi Damien!
>
> The problem that define only defines a variable in the current lexical
> environment. Even if a definition in an if block would work from a
> syntactical standpoint, it would be pointless.
>
> As per r6rs, internal (define ...) are akin to letrec, but are only
> allowed in definition context. Guile 3 relaxes this for function bodies,
> whereas the macros I linked above relaxes this for some additional forms.
> It cannot however "export" bindings to any outer environment.
>
> What you can do is use module reflection to create top-level variables in
> the current module, but that makes everyone flinch about 105% of the time.
>
> --
>   Linus Björnstam
>
> On Wed, 9 Jun 2021, at 11:29, Damien Mattei wrote:
> > hello, i'm just answering now  because my ten years old Mac book pro
> > definitely died sunday evening RIP
> >
> > i was trying to make macro that set! variable if they exist or create
> > it before if the variable is not defined (tested by the Guile procedure
> > 'defined?' which is not R*RS in any version)
> >
> > but it seems that Guile 3.0 'define' can not create variable in local
> > blocks, as in Python :
> > (if test
> >     (begin
> >          (define x 7)
> >          (foo x))
> >      (else-block))
> > will not work.
> >
> > This behavior force the definition of all variable at the upper level
> > of the function, the only advantage of guile 3.0 is perhaps (i have not
> > tested it fully) that you can put define not only at beginning of
> > function.
> >
> > This would be interesting to be able to define local variables with
> > 'define',defining variable only in the block they are usefull.
> >
> > With those limitation in mind i defined a few more macros:
> > (define-syntax <+
> >   (syntax-rules ()
> >     ((_ var expr) (define var expr))))
> >
> > { x <+ 7 } is equivalent to : (<- x 7) or (define x 7)
> >
> > :=  is the same as <+
> > (define-syntax :=
> >   (syntax-rules ()
> >     ((_ var expr) (define var expr))))
> >
> > { x := 7 } is equivalent to : (:= x 7) or (define x 7)
> >
> > ;; definition without a value assigned
> > ;;  (def x)
> > (define-syntax def
> >   (syntax-rules ()
> >     ((_ var) (define var '()))))
> >
> > i added all that to my previous macro system in my Scheme+
> > (unfortunately my github account is on my dead mac,so no commit for now)
> >
> > i do not think i can do more for now, define anywhere in code could be
> > a good solution to let letrec let* that in my opinion should no longer
> > exist in Scheme.
> >
> > Here is an example on the famous sub set sum problem algorithm  in two
> > versions one with define 'anywhere' and the other with my previous
> > macro of Scheme+:
> > scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889
> > 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027))
> > scheme@(guile-user)> (start-ssigma-sol-approx-pack-define-anywhere
> > L-init 19836)
> > $1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285 5027)
> > scheme@(guile-user)> (apply + '(1 3 4 16 17 256 275 723 1040 1041 1284
> > 1344 1520 3000 4285 5027))
> > $2 = 19836
> >
> > code:
> > *Preview:*
> >
> > (define (start-ssigma-sol-approx-pack-define-anywhere L t) ;; Sub Set
> > Sum problem (find solution or approximation)
> >   ;; { } are for infix notation as defined in SRFI 105
> >   ;; <+ and := are equivalent to (define var value)
> >   { best-sol <+ (lambda (L1 L2)
> >
> >                 {s1 <+ (apply + L1)}
> >                 {s2 <+ (apply + L2)}
> >
> >                 (if {(abs {t - s1}) <= (abs {t - s2})}
> >                     L1
> >                     L2)) }
> >
> >   ;; := is the same macro as <+
> >   { best-sol3 := (lambda (L1 L2 L3)
> >
> >                  {L22 <+ (best-sol L2 L3)}
> >                  (best-sol L1 L22)) }
> >
> >
> >   { ssigma-sol-approx <+ (lambda (L)
> >                          ;; def is a macro for declared but unasigned
> variable, it is same
> > as (define var '())
> >                          (def c)
> >                          (def R)
> >
> >                          (if (null? L)
> >                              L
> >                              (begin {c <- (first L)}
> >                                     {R <- (rest L)}
> >
> >                                     (cond [ {c = t} (list c) ] ;; c is
> the solution
> >                                           [ {c > t} (best-sol (list c)
> (ssigma-sol-approx R)) ] ;; c is
> > to big to be a solution but could be an approximation
> >                                           ;; c < t at this point, 3
> possibilities :
> >                                           ;; c is the best solution
> >                                           ;; c is part of the solution
> or his approximation
> >                                           ;; or c is not part of
> solution or his approximation
> >                                           [ else (best-sol3 (list c) ;;
> c is the best solution
> >
> >                                                             ;; c part of
> solution or is approximation
> >                                                             (cons c
> (start-ssigma-sol-approx-pack-define-anywhere R {t
> > - c})) ;; we have to find a solution or an approximation for t-c now
> >
>
> >                                                             ;; c is not
> part of solution or his approximation
> >
>  (ssigma-sol-approx R))])))) }
> >
> >
> >   ;; start the function
> >   (ssigma-sol-approx L))
> > *
> > *
> >
> > with other macros of Scheme+ code looked like this:*
> > *
> >
> > *
> > *
> >
> > *Preview:*
> >
> > ;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723 889
> > 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027))
> > ;; scheme@(guile-user)> (start-ssigma-sol-approx-pack L-init 19836)
> > ;; $1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285
> > 5027)
> >
> > (define (start-ssigma-sol-approx-pack L t) ;; Sub Set Sum problem (find
> > solution or approximation)
> >
> >
> >   (letrec-arrow* [ best-sol ← (lambda (L1 L2)
> >
> >                               (let-arrow*  [ s1 ← (apply + L1)
> >                                              s2 ← (apply + L2) ]
> >
> >                                            (if {(abs {t - s1}) <= (abs
> {t - s2})}
> >                                                L1
> >                                                L2)))
> >
> >                  best-sol3 ← (lambda (L1 L2 L3)
> >
> >                                (let [(L22 (best-sol L2 L3))]
> >                                  (best-sol L1 L22)))
> >
> >                  ssigma-sol-approx ← (lambda (L)
> >
> >                                     (if (null? L)
> >
> >                                         L
> >
> >                                         (let-arrow* [ c ← (first L)
> >                                                       R ← (rest L)  ]
> >
> >                                           (cond [ {c = t} (list c) ] ;;
> c is the solution
> >                                                 [ {c > t} (best-sol
> (list c) (ssigma-sol-approx R)) ] ;; c is
> > to big to be a solution but could be an approximation
> >                                                 ;; c < t at this point,
> 3 possibilities :
> >                                                 ;; c is the best solution
> >                                                 ;; c is part of the
> solution or his approximation
> >                                                 ;; or c is not part of
> solution or his approximation
> >                                                 [ else (best-sol3 (list
> c) ;; c is the best solution
> >
> >
>  ;;(begin
> >                                                                     ;;
> (display "append c=") (display c) (newline)
> >                                                                   ;; c
> part of solution or is approximation
> >                                                                   (cons
> c (start-ssigma-sol-approx-pack R {t - c}));;) ;; we
> > have to find a solution or an approximation for t-c now
> >
> >                                                                   ;; c
> is not part of solution or his approximation
> >
>  (ssigma-sol-approx R))]))))
> >
> >
> >              ]
> >
> >            ;; start the function
> >            (ssigma-sol-approx L)))
> >
> >
> > the use of define anywhere will make code more readable, infixe
> > notation and macros too...
> >
> > A last example in Scheme+ with arrays on the dynamic solution of subset
> > sum problem (just answer if a solution exist or not):
> >
> > *Preview:*
> >
> > (include "../library-FunctProg/first-and-rest.scm")
> > (include "../library-FunctProg/guile/array.scm")
> > (include "../library-FunctProg/pair.scm")
> > (include "../library-FunctProg/number.scm")
> > (include "../library-FunctProg/list.scm")
> > (include "../library-FunctProg/let.scm")
> >
> >
> > (define L-init '(1 3 4 16 17 24 45 64 197 256 275 323 540 723 889 915
> > 1040 1041 1093 1099 1111 1284 1344 1520 2027 2500 2734 3000 3267 3610
> > 4285 5027))
> >
> > (define t-init 35267)
> >
> > (define ls (length L-init))
> >
> > (define dyna (make-array 0 {ls + 1} {t-init + 1}))
> >
> > (define (one-two b)
> >   (if b 1 2))
> >
> > (define cpt 0)
> >
> > ;; scheme@(guile-user)> (ssigma-dyna-define-anywhere L-init t-init)
> > ;; $1 = #t
> > ;; scheme@(guile-user)> cpt
> > ;; $2 = 147801
> > (define (ssigma-dyna-define-anywhere L t)
> >
> >   {cpt <- {cpt + 1}} ;; cpt is defined at toplevel
> >
> >   ;;(display L) (display " ") (display t) (newline)
> >
> >   {ls <+ (length L)}
> >   {dyn <+ {dyna[ls t]}}
> >
> >   (def c)
> >   (def R)
> >
> >   ;; dyna[ls t] means 0: unknown solution, 1: solution found, 2: no
> solution
> >   (one?
> >     (if (not (zero? dyn))
> >
> >       dyn
> >
> >       ;; set the array but return the variable
> >       { dyna[ls t] <- (one-two
> >                         (if (null? L)
> >                             #f
> >                             (begin
> >                               {c <- (first L)}
> >                               {R <- (rest L)}
> >                               (cond [ {c = t} #t ] ;; c is the solution
> >                                     [ {c > t}
> (ssigma-dyna-define-anywhere R t) ] ;; c is to big
> > to be a solution but can be an approximation
> >                                     ;; c < t at this point
> >                                     ;; c is part of the solution or his
> approximation
> >                                     ;; or c is not part of solution or
> his approximation
> >                                     [ else {(ssigma-dyna-define-anywhere
> R {t - c}) or
> > (ssigma-dyna-define-anywhere R t)} ] )))) } )))
> >
> >
> > Damien
> >
> >
> > On Sun, Jun 6, 2021 at 7:32 PM Linus Björnstam
> > <linus.internet@fastmail.se> wrote:
> > > Try
> > > ,expand (new-set! X 7)  at the repl.
> > >
> > > That will however not do what you want. define does not work like
> that. Define adds the binding in what is called the current lexical
> environment: a define inside an if will not be visible outside that if
> (even if it would have been valid syntax).
> > >
> > > What are you trying to write?
> > >
> > > --
> > >   Linus Björnstam
> > >
> > > On Sun, 6 Jun 2021, at 17:16, Damien Mattei wrote:
> > > > macros... i can not figure out how to make this one works:
> > > > (define-syntax new-set!
> > > >   (syntax-rules ()
> > > >      ((_ var expr)  (if (defined? (quote var))
> > > >                                (set! var expr)
> > > >                                (define var expr)))))
> > > >
> > > > scheme@(guile-user)> (new-set! x 7)
> > > > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > > > ;;; <stdin>:24:0: warning: possibly unbound variable `x'
> > > > ice-9/boot-9.scm:1669:16: In procedure raise-exception:
> > > > Unbound variable: x
> > > >
> > > > Entering a new prompt.  Type `,bt' for a backtrace or `,q' to
> continue.
> > > > scheme@(guile-user) [1]> ,bt
> > > > In current input:
> > > >      24:0  1 (_)
> > > > In ice-9/boot-9.scm:
> > > >   1669:16  0 (raise-exception _ #:continuable? _)
> > > >
> > > > any idea?
> > > >
> > > > Damien
> > > >
> > > > On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam
> > > > <linus.internet@fastmail.se> wrote:
> > > > > I implemented this hack before guile 3 got defines in function
> bodies: https://hg.sr.ht/~bjoli/guile-define
> > > > >
> > > > > Even I guile 3 it allows a more liberal placement of define, but
> it won't work for things like bodies of imported macros (like match)
> > > > > --
> > > > >   Linus Björnstam
> > > > >
> > > > > On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > > > > > hello,
> > > > > > i'm was considering that i want to be able to define a variable
> > > > > > anywhere in
> > > > > > code, the way Python do. Few scheme can do that (Bigloo i know)
> > > > > > ( the list here is not exact:
> > > > > >
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > > > > > )
> > > > > >  is it possible in guile to do it? i do not know, so could it be
> added
> > > > > > to
> > > > > > the language specification for future release?
> > > > > >
> > > > > > regards,
> > > > > > Damien
> > > > > >
>


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

* Re: define anywhere
  2021-06-05 15:23 ` Linus Björnstam
                     ` (2 preceding siblings ...)
  2021-06-11  8:07   ` Damien Mattei
@ 2021-06-11 23:43   ` Damien Mattei
  3 siblings, 0 replies; 14+ messages in thread
From: Damien Mattei @ 2021-06-11 23:43 UTC (permalink / raw)
  To: Linus Björnstam, Jean-Paul Roy; +Cc: guile-user

hello Linus,

finally i tested your code with my program and it works.

define worked in if in ssigma-sol-approx
( in


*Preview:*

;; (load "SssRec.scm")


;;(use-modules (syntax define));;(use-modules
(guile/define))(use-modules (guile define))
(include "first-and-rest.scm")(include "list.scm")(include
"postfix.scm")(include "let.scm")(include "definition.scm")(include
"guile/array.scm")(include "block.scm")
;; if data are disordered the algo works also
;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723
889 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027));;
scheme@(guile-user)>  (start-ssigma-sol-approx-linus L-init 19836);;
$1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285
5027)(define (start-ssigma-sol-approx-linus L t) ;; Sub Set Sum
problem (find solution or approximation)
  ;; { } are for infix notation as defined in SRFI 105
  ;; <+ and := are equivalent to (define var value)
  { best-sol <+ (lambda (L1 L2)
		
		  {s1 <+ (apply + L1)}
		  {s2 <+ (apply + L2)}
		
		  (if {(abs {t - s1}) <= (abs {t - s2})}
		      L1
		      L2)) }

  ;; := is the same macro as <+
  { best-sol3 := (lambda (L1 L2 L3)
		
		   {L22 <+ (best-sol L2 L3)}
		   (best-sol L1 L22)) }


  { ssigma-sol-approx <+ (lambda (L)
			   ;; def is a macro for declared but unasigned variable, it is
same as (define var '())
			   ;;(def c)
			   ;;(def R)
			
			   (if (null? L)
			       L
			       ($ (define c (first L)) ;; $ = begin
				  (if {c = t}
				      (list c)  ;; c is the solution
				      ($ (define R (rest L))
					 (if {c > t}
					     (best-sol (list c) (ssigma-sol-approx R))  ;; c is to big to
be a solution but could be an approximation
					     ;; c < t at this point, 3 possibilities :
					     ;; c is the best solution
					     ;; c is part of the solution or his approximation
					     ;; or c is not part of solution or his approximation
					     (best-sol3 (list c) ;; c is the best solution
							;; c part of solution or is approximation
							(cons c (start-ssigma-sol-approx-linus R {t - c})) ;; we have
to find a solution or an approximation for t-c now
							;; c is not part of solution or his approximation
							(ssigma-sol-approx R)))))))) }
		
	
  ;; start the function
  (ssigma-sol-approx L))



without you module, my solution was:

https://github.com/damien-mattei/library-FunctProg/blob/master/SssRec.scm

*Preview:*

;; scheme@(guile-user)> (define L-init '(1 3 4 16 17 64 256 275 723
889 1040 1041 1093 1111 1284 1344 1520 2027 2734 3000 4285 5027));;
scheme@(guile-user)>  (start-ssigma-sol-approx-basic L-init 19836);;
$1 = (1 3 4 16 17 256 275 723 1040 1041 1284 1344 1520 3000 4285
5027);; scheme@(guile-user)> (apply + $1);; $2 = 19836(define
(start-ssigma-sol-approx-basic L t) ;; Sub Set Sum problem (find
solution or approximation)
  ;; { } are for infix notation as defined in SRFI 105
  ;; <+ and := are equivalent to (define var value)
  { best-sol <+ (lambda (L1 L2)
		
		  {s1 <+ (apply + L1)}
		  {s2 <+ (apply + L2)}
		
		  (if {(abs {t - s1}) <= (abs {t - s2})}
		      L1
		      L2)) }

  ;; := is the same macro as <+
  { best-sol3 := (lambda (L1 L2 L3)
		
		   {L22 <+ (best-sol L2 L3)}
		   (best-sol L1 L22)) }


  { ssigma-sol-approx <+ (lambda (L)
			   ;; def is a macro for declared but unasigned variable, it is
same as (define var '())
			   (def c)
			   (def R)
			
			   (if (null? L)
			       L
			       ($ {c <- (first L)} ;; $ = begin
				  (if {c = t}
				      (list c)  ;; c is the solution
				      ($ {R <- (rest L)}
					 (if {c > t}
					     (best-sol (list c) (ssigma-sol-approx R))  ;; c is to big to
be a solution but could be an approximation
					     ;; c < t at this point, 3 possibilities :
					     ;; c is the best solution
					     ;; c is part of the solution or his approximation
					     ;; or c is not part of solution or his approximation
					     (best-sol3 (list c) ;; c is the best solution
							;; c part of solution or is approximation
							(cons c (start-ssigma-sol-approx-basic R {t - c})) ;; we have
to find a solution or an approximation for t-c now
							;; c is not part of solution or his approximation
							(ssigma-sol-approx R)))))))) }
		
	
  ;; start the function
  (ssigma-sol-approx L))


just had a few problem making Guile 3.0 find the module from an other sub
directory, but not the falt of you module, my config i think?


Damien


On Sat, Jun 5, 2021 at 5:24 PM Linus Björnstam <linus.internet@fastmail.se>
wrote:

> I implemented this hack before guile 3 got defines in function bodies:
> https://hg.sr.ht/~bjoli/guile-define
>
> Even I guile 3 it allows a more liberal placement of define, but it won't
> work for things like bodies of imported macros (like match)
> --
>   Linus Björnstam
>
> On Sat, 5 Jun 2021, at 00:27, Damien Mattei wrote:
> > hello,
> > i'm was considering that i want to be able to define a variable
> > anywhere in
> > code, the way Python do. Few scheme can do that (Bigloo i know)
> > ( the list here is not exact:
> >
> https://www.reddit.com/r/scheme/comments/b73fdz/placement_of_define_inside_lambda_bodies_in/
> > )
> >  is it possible in guile to do it? i do not know, so could it be added
> > to
> > the language specification for future release?
> >
> > regards,
> > Damien
> >
>


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

end of thread, other threads:[~2021-06-11 23:43 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-04 22:27 define anywhere Damien Mattei
2021-06-05  2:10 ` Taylan Kammer
2021-06-05  8:50   ` Dr. Arne Babenhauserheide
2021-06-05 15:23 ` Linus Björnstam
2021-06-06  8:09   ` Linus Björnstam
2021-06-06 12:02     ` Dr. Arne Babenhauserheide
2021-06-06 13:36       ` Linus Björnstam
2021-06-06 16:45         ` Dr. Arne Babenhauserheide
2021-06-11  8:08         ` Damien Mattei
     [not found]   ` <CADEOadcwk1D9of6C1owQQSqFNRvUmA=wJKSngySbGsfdDUzoMw@mail.gmail.com>
     [not found]     ` <28842df5-d82b-4819-b7ad-2148265f7a83@www.fastmail.com>
2021-06-09  9:29       ` Damien Mattei
2021-06-09 10:11         ` Linus Björnstam
2021-06-11  8:15           ` Damien Mattei
2021-06-11  8:07   ` Damien Mattei
2021-06-11 23:43   ` 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).