From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Mattei Newsgroups: gmane.lisp.guile.user Subject: Re: Fwd: new function Date: Tue, 21 Sep 2021 15:04:29 +0200 Message-ID: References: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13176"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user To: Matt Wette Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Sep 21 15:05:18 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSfSY-0003Dx-4N for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 15:05:18 +0200 Original-Received: from localhost ([::1]:51528 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mSfSW-0000uZ-E8 for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 09:05:16 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47452) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSfS6-0000u7-7o for guile-user@gnu.org; Tue, 21 Sep 2021 09:04:50 -0400 Original-Received: from mail-il1-x12a.google.com ([2607:f8b0:4864:20::12a]:34789) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mSfS4-0005Og-2h for guile-user@gnu.org; Tue, 21 Sep 2021 09:04:49 -0400 Original-Received: by mail-il1-x12a.google.com with SMTP id w1so22589459ilv.1 for ; Tue, 21 Sep 2021 06:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=RbguUSQV4TsrJyVFydjNr1S/Ta6+i/PTQ3EQlVPiDQU=; b=iZpls+R14VHdnmlCWeDjyZH8putBEXX5E/YCnnFcig+cpZEGsW7SMj5QMVhwGf15Zj 3vKYjwM5W9KpBlpCjOYP1kIS4LPz8dgeIZlQqwb/AM0N96lsQQmDFqEbXvODxETDSdD9 JeCTQ6Dn4k2uJRKhIF1bl84XQp4jNzqB+y2gS4K/KjqhzZzfnTr5tkGTZ5fuXKaH3b/s q+aSyElm1yHobIYfmT98zTC0LTjFHGkfLwif1nvUSsolKb2odmTtnSB23BfNiSuULR9N XVmGHdqAw77VBOB49UysorqF8PTNu1u6zUI7qRnO+arfN3DKVocwprOEQOlfh2V/NT+J 2Fxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RbguUSQV4TsrJyVFydjNr1S/Ta6+i/PTQ3EQlVPiDQU=; b=nAPe9MMC3NdwKybEZsEAHYWGc8fXjEmVf0VVAEhijkmJeKsthGg/iOvzmZqg1+4r6Y MosQCr/UCcSQ8tcvj+IwxR6EVX2oSYM11ckhO4byKWA2RijHdgtEEijaLylJwb96EKHb KsQkkSEwIVPQmYt8DJNNwLxq+YL7DPqR4PdWjsXq5OJyW6AMTVGYipSnwqDudz7mm6HK ePOQM8EYWygkrbO5N3TPpFCdIAJum220j8p0UZFjHxWvP45IzdXW0FMRnH8Nf3lNS3dR bOcKPxjd7xoCMVJdII7LdSsHcXKFS3muoZrPZvAZHv77W3d6mL+mzCBQB1OW9LOudkfO qmag== X-Gm-Message-State: AOAM5334cRlDNVvn7weMFQAc2yb17n58+WjUGh9tCzgrTBIKTKmINaVf +R+xCn2chu5jZzQkyi3ns6X/EpqH4HFRg5T0RA0= X-Google-Smtp-Source: ABdhPJylsBkzRwb0PfL7s/YLW8dPaYlLCjKMaNND4nG4EfF8rm5QBMRKmlRMcfySpLNN//hn2Mows9SRINjJ2yHmByM= X-Received: by 2002:a92:cd46:: with SMTP id v6mr21480377ilq.66.1632229480592; Tue, 21 Sep 2021 06:04:40 -0700 (PDT) In-Reply-To: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com> Received-SPF: pass client-ip=2607:f8b0:4864:20::12a; envelope-from=damien.mattei@gmail.com; helo=mail-il1-x12a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17768 Archived-At: 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 ;;; : 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 ;;; : 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=# # scheme@(guile-user)> x 3 it does not seem to help me, perheaps if i can test the return of # .... Damien On Sun, Sep 19, 2021 at 4:46 PM Matt Wette 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 > >> Date: Sun, Sep 19, 2021 at 9:54 AM > >> Subject: new function > >> To: > >> > >> > >> 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)> > > > > > > >