unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: Taylan Kammer <taylan.kammer@gmail.com>,
	Matt Wette <matt.wette@gmail.com>,
	Maxime Devos <maximedevos@telenet.be>
Cc: Jean-Paul Roy <jean-paul.roy@unice.fr>,
	guile-devel <guile-devel@gnu.org>
Subject: Re: new function
Date: Fri, 24 Sep 2021 06:41:18 +0200	[thread overview]
Message-ID: <CADEOadcWLy1+p+YBKhiVoucp2JLVLDXHWjU-vvgh2PvhRLco3w@mail.gmail.com> (raw)
In-Reply-To: <CADEOadf5GTwNB8=QcMk+rF5pcum1nGQJRK8i+nWdmdbdMYx1FQ@mail.gmail.com>

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

so i drop off the idea of a single assignment operator, there will be 2 ,<-
for single assignment when the variable has been previously defined (at
upper level), and another <+ that add a variable in the environment using
define.
Other possibility is to have a single assignment operator <- and to declare
the variables not previously defined at upper level.
All this was already done in Scheme+ :

here is what looks like
 the module definition :
(define-module (Scheme+Guile)

  #:export (def $bracket-apply$ <- -> <+ declare $ condx <> )

  )

(include "def.scm")
(include "Scheme+Guile/array-square-brackets.scm")
(include "Scheme+Guile/assignment.scm")
(include "declare.scm")
(include "condx.scm")
(include "block.scm")
(include "not-equal.scm")

and so, the code for the 2 assignment operator :

*Preview:*

(define (subset-sum-guile L t)

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

  {cpt <- {cpt + 1}} ;; cpt has been already defined at toplevel

  ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution

  (condx [{dyn <> 0} (one? dyn)]
	 [(null? L) {dyna[ls t] <- 2}  #f] ;; return #f
	
	 [exec {c <+ (first L)}]	
	 ;; c is the solution
	 [{c = t} {dyna[ls t] <- 1}  #t]  ;; return #t
	
	 [exec {R <+ (rest L)}]	
	 ;; continue searching a solution in the rest
	 [{c > t} {s <+ (subset-sum-guile R t)}
	          {dyna[ls t] <- (one-two s)}
		  s] ;; return boolean value
			
	 ;; else : c < t at this point
	 ;; c is part of a solution OR not part of a solution
	 [else {s <+ {(subset-sum-guile R {t - c}) or (subset-sum-guile R t)}}
	       {dyna[ls t] <- (one-two s)}
	       s])) ;; return boolean value


and the code which use a single operator and a *declare* (it is a define
unspecified):

*Preview:*

(define (subset-sum-guile-dec L t)

  (declare ls dyn c R s)

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

  {cpt <- {cpt + 1}} ;; cpt has been already defined at toplevel

  ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution

  (condx [{dyn <> 0} (one? dyn)]
	 [(null? L) {dyna[ls t] <- 2}  #f] ;; return #f
	
	 [exec {c <- (first L)}]	
	 ;; c is the solution
	 [{c = t} {dyna[ls t] <- 1}  #t]  ;; return #t
	
	 [exec {R <- (rest L)}]	
	 ;; continue searching a solution in the rest
	 [{c > t} {s <- (subset-sum-guile-dec R t)}
	          {dyna[ls t] <- (one-two s)}
		  s] ;; return boolean value
			
	 ;; else : c < t at this point
	 ;; c is part of a solution OR not part of a solution
	 [else {s <- {(subset-sum-guile-dec R {t - c}) or (subset-sum-guile-dec R t)}}
	       {dyna[ls t] <- (one-two s)}
	       s])) ;; return boolean value


the two syntax can be mixed but i do not think it is a good idea to have 2
coding style in the same project, also the position of 'declare' or '<+' is
depending of the possibility of the used Scheme implementation to have
define placed everywhere more or less, in the worse case the 'declare' or
'<+' have to placed at top of the body of the procedure.
The way python declare them, note that i have no regret to not be able to
define variables nested in a branch of code the way python do it that can
be seen from the whole procedure.I do not think it is a good idea and
useful. But i hoped to be able to avoid a 'declare' at the top or somewhere
but declaration even in Scheme could be necessary with some typed Scheme or
when using object oriented features of some Scheme perheaps that used typed
variables (for overloading function? not sure it exist in Scheme also ,even
in OOP).So this is not a great problem.

Damien


On Thu, Sep 23, 2021 at 11:53 PM Damien Mattei <damien.mattei@gmail.com>
wrote:

> yes i'm using GNU Guile 3.0.7,
> https://www.gnu.org/software/guile/docs/master/guile.html/Syntax-Transformer-Helpers.html
>
> i have tested a lot , even define-once again and i choose to use to
> assignment operators and portable code because the non-portable function do
> not bring more, finally it was not a bad idea to ask for a new function
> because we can do it with the actual toolbox...
> Damien
>
> On Thu, Sep 23, 2021 at 10:48 PM Taylan Kammer <taylan.kammer@gmail.com>
> wrote:
>
>> Responding to myself:
>>
>> On 23.09.2021 22:27, Taylan Kammer wrote:
>>
>> > I can't seem to find syntax-local-binding in Guile 2.2 or 3.0.  Did you
>> > have to import some special module, or are you using another version?
>>
>> Worked when I imported (system syntax internal).
>>
>> > Either way, I suspect that the following will not work with your macro:
>> >
>> >   (let ()
>> >     (let ()
>> >       (<$ x 1))
>> >     (display x)
>> >     (newline))
>>
>> Indeed it doesn't work, though for a different reason:
>>
>>   While compiling expression:
>>   Syntax error:
>>   unknown file:43:8: body should end with an expression in form (let ()
>> (<$ x 1))
>>
>> That's because indeed the inner let expands into:
>>
>>   (let ()
>>     (define x 1))
>>
>> And there has to be at least one expression after the define.  So I tried:
>>
>>   (let ()
>>     (let ()
>>       (<$ x 1)
>>       (newline))
>>     (display x)
>>     (newline))
>>
>> And as I expected, it says 'x' is unbound:
>>
>>   ;;; <stdin>:44:45: warning: possibly unbound variable `x'
>>   <$ : global scope : x
>>
>>   ice-9/boot-9.scm:1685:16: In procedure raise-exception:
>>   Unbound variable: x
>>
>> The only way it will work is if you never use nested scopes, but that will
>> lead to very strange Scheme code, and there will probably be many cases
>> where you accidentally use a nested scope without immediately noticing it.
>>
>> Note also that definitions aren't allowed everywhere.  Consider this:
>>
>>   (let ()
>>     (if 'whatever
>>         (<$ x 1)
>>         (<$ x 2))
>>     (display x)
>>     (newline))
>>
>> It leads to:
>>
>>   While compiling expression:
>>   Syntax error:
>>   unknown file:49:17: definition in expression context, where definitions
>> are not allowed, in form (define x 1)
>>
>> Because the arms of 'if' aren't allowed to be definitions.
>>
>> --
>> Taylan
>>
>

[-- Attachment #2: Type: text/html, Size: 24740 bytes --]

      reply	other threads:[~2021-09-24  4:41 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-19  7:54 new function Damien Mattei
2021-09-19 14:41 ` Matt Wette
2021-09-19 14:43   ` Matt Wette
2021-09-19 21:38 ` Taylan Kammer
2021-09-21 12:26   ` Damien Mattei
2021-09-22  8:44   ` Damien Mattei
2021-09-22  9:06     ` William ML Leslie
2021-09-22  9:53       ` Damien Mattei
2021-09-22 18:51         ` Taylan Kammer
2021-09-22 21:52           ` William ML Leslie
2021-09-23  8:40             ` Damien Mattei
2021-09-23 19:03             ` Taylan Kammer
2021-09-23 17:27           ` Damien Mattei
2021-09-23 18:00             ` Maxime Devos
2021-09-23 20:27             ` Taylan Kammer
2021-09-23 20:42               ` Damien Mattei
2021-09-23 20:48               ` Taylan Kammer
2021-09-23 21:53                 ` Damien Mattei
2021-09-24  4:41                   ` Damien Mattei [this message]

Reply instructions:

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

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

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

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

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

  git send-email \
    --in-reply-to=CADEOadcWLy1+p+YBKhiVoucp2JLVLDXHWjU-vvgh2PvhRLco3w@mail.gmail.com \
    --to=damien.mattei@gmail.com \
    --cc=guile-devel@gnu.org \
    --cc=jean-paul.roy@unice.fr \
    --cc=matt.wette@gmail.com \
    --cc=maximedevos@telenet.be \
    --cc=taylan.kammer@gmail.com \
    /path/to/YOUR_REPLY

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

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