guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: new function


From: Damien Mattei
Subject: Re: Fwd: new function
Date: Tue, 21 Sep 2021 15:04:29 +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))
scheme@(guile-user)> x
;;; <unknown-location>: 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]> ,q
scheme@(guile-user)> (foo2)
x=2
1
scheme@(guile-user)> (foo2)
x=2
1
scheme@(guile-user)> x
;;; <unknown-location>: 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]> ,q
scheme@(guile-user)> (define x 3)
scheme@(guile-user)> (foo2)
x=#<unspecified>
#<unspecified>
scheme@(guile-user)> x
3

it does not seem to help me, perheaps if i can test the return of
#<unspecified> ....

Damien

On Sun, Sep 19, 2021 at 4:46 PM Matt Wette <matt.wette@gmail.com> wrote:

>
>
> On 9/19/21 3:38 AM, Maxime Devos wrote:
> > Damien Mattei schreef op zo 19-09-2021 om 11:18 [+0200]:
> >> ---------- Forwarded message ---------
> >> From: Damien Mattei <damien.mattei@gmail.com>
> >> Date: Sun, Sep 19, 2021 at 9:54 AM
> >> Subject: new function
> >> To: <guile-devel@gnu.org>
> >>
> >>
> >> hello,
> >> i'm developing an extension to Scheme
> >> and i need a procedure or macro that define a variable only if it is not
> >> bind and if it is just set! it.
> > Could you give an example in which the hypothetical define-or-set! is
> used?
> > I wonder where such a thing would be useful.
> >
> >> I can not do it in Guile or any Scheme,and i'm desperately searching a
> way
> >> to do that. I finally conclude that it can be done only by adding it in
> the
> >> language.
> >>
> >> Can someone include a such function in Guile next release?
> >> i know guile have a predicate defined?
> > defined? only works on global variables, not on lexicals,
> > and depends on the (current-module), which is not necessarily
> > the module defined? is used in.
> >
> >>   to test binfing of a vairable but
> >> writing a macro with it is not possible because define can be used in an
> >> expression context.
> > You can use 'syntax-local-binding'.
> >
> > (use-modules (system syntax))
> > (define-syntax define-or-set!
> >    (lambda (s)
> >      (syntax-case s ()
> >        ((_ var value)
> >         (case (syntax-local-binding #'var)
> >           ((lexical displaced-lexical) #'(set! var value))
> >           ((global) #'(define var value))
> >           (else ???))))))
> >
> > ,expand (define-or-set! a 0) ; $_ = (define a 0)
> > ,expand (let ((a 0)) (define-or-set! a 0) a) ; $_ = (let ((a 0)) (set! a
> 0) a)
> > (define-or-set! a 0)
> > ,expand (define-or-set! a 0) ; $_ = (define a 0) (might or might not be
> acceptable for your use case
> > ,expand (let () (define-or-set! a 0) (define-or-set! a 1) a) ; $_ = (let
> () (define a 0) (set! a 1))
> >
> > Greetings,
> > Maxime.
>
> Does this work for you?
>
> scheme@(guile-user)> (define a 1)
> scheme@(guile-user)> (define a 2)
> scheme@(guile-user)> a
> $2 = 2
> scheme@(guile-user)>
>
>
>
>
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]