unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: Maxime Devos <maximedevos@telenet.be>
Cc: guile-user <guile-user@gnu.org>, Matt Wette <matt.wette@gmail.com>
Subject: Re: Fwd: new function
Date: Wed, 22 Sep 2021 09:52:48 +0200	[thread overview]
Message-ID: <CADEOadcn0=_JMS6NSCWqQjxkmmDXubSX-2msa_bOsrOuV-Qtfw@mail.gmail.com> (raw)
In-Reply-To: <52130eefb713942a36aa5717ef4c916173567ce3.camel@telenet.be>

i do not understand well what you want to mean with those example.
For me  define-once does not seems to be a solution, it act as define too
much ,does not set! variable if already exist and can not use to set it
again because, as define it is forbidden twice in the same block for the
same variable:

scheme@(guile-user)> (define (foo3)
  (define-once  x 1)
  (if #t
      (let ()
        (define-once x 2)
        ;;(set! x 2)
        (display "x=")
        (display x)
        (newline)
        (define-once x 3)
        ;;(set! x 2)
        (display "x=")
        (display x)
        (newline)

        )
      'never)
  (display x)
  (newline))
While compiling expression:
Syntax error:
unknown file:51:6: invalid or duplicate identifier in definition in form
(let () (define-once x 2) (display "x=") (display x) (newline) (define-once
x 3) (display "x=") (display x) (newline))

i will try (use-modules (system syntax)) that yesterday seems to give
better results

On Tue, Sep 21, 2021 at 9:03 PM Maxime Devos <maximedevos@telenet.be> wrote:

> Damien Mattei schreef op di 21-09-2021 om 15:04 [+0200]:
> > i have tested define-once
> > http://www.gnu.org/software/guile/docs/master/guile.html/Top-Level.html
> > (the defvar of Lisp)and idea are:
> > -unfortunately it is considered by scheme as a define,so there is some
> > context where it is not allowed in my code
> > -seems to work fine at toplevel (as mentioned in doc) but strange
> behavior
> > in a function, i did not understand really what happened but i got some
> > #unspecified value.
> >
> > here are my test code:
> > cheme@(guile-user)> (define (foo2)
> >   (define-once  x 1)
> >   (if #t
> >       (let ()
> >         (define-once x 2)
> >         ;;(set! x 2)
> >         (display "x=")
> >         (display x)
> >         (newline))
> >       'never)
> >   (display x)
> >   (newline))
>
> Possibly you want (added a set? argument for demonstration):
>
> (define (foo2 set?)
>   (define x) ; define an (undefined or unbound, not sure about
> terminology) variable
>   (if set?
>       (let ()
>         (set! x 2) ; change the value of x
>         (display "x=")
>         (display x)
>         (newline))
>       'never)
>   (display x)
>   (newline))
>
> That should be portable and avoids global state.
>
> scheme@(guile-user)> x
> ;;; <stdin>:20:0: warning: possibly unbound variable `x'
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> Unbound variable: x
> scheme@(guile-user) [1]> ,q
>
> scheme@(guile-user) [1]> (foo2 #f)
> #<unspecified> ; I expected an error as would result from ...
>
> ;; ... this ...
> scheme@(guile-user)> (variable-ref (make-undefined-variable))
> ice-9/boot-9.scm:1685:16: In procedure raise-exception:
> In procedure variable-ref: Unbound variable: #<variable 7f6de46cde80
> value: #<undefined>>
>
> Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
> scheme@(guile-user) [2]>
>
> ;; instead  of #<unpecified> but whatever ...
>
> scheme@(guile-user) [1]> (foo2 #t)
> x=2
> 2
> scheme@(guile-user) [1]> (foo2 #t)
> x=2
> 2
>
>
> scheme@(guile-user) [1]> (define x 3)
> scheme@(guile-user) [1]> (foo2 #t)
> x=2 ; foo2 doesn't use the global variable 'x'
> 2
>
> scheme@(guile-user) [1]> x
> $1 = 3
>
> Does this seem reasonable to you?
>


  reply	other threads:[~2021-09-22  7:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CADEOadfF+rR=fW0fS-PkQkDN8EHFB1sJnkeePMJyc23m9shLgA@mail.gmail.com>
2021-09-19  9:18 ` Fwd: new function Damien Mattei
2021-09-19 10:38   ` Maxime Devos
2021-09-19 14:45     ` Matt Wette
2021-09-19 14:56       ` Maxime Devos
2021-09-21 13:04       ` Damien Mattei
2021-09-21 19:03         ` Maxime Devos
2021-09-22  7:52           ` Damien Mattei [this message]
2021-09-22 18:03             ` Maxime Devos
2021-09-22 19:12           ` Taylan Kammer

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='CADEOadcn0=_JMS6NSCWqQjxkmmDXubSX-2msa_bOsrOuV-Qtfw@mail.gmail.com' \
    --to=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=matt.wette@gmail.com \
    --cc=maximedevos@telenet.be \
    /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).