From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.user Subject: Re: Fwd: new function Date: Sun, 19 Sep 2021 07:45:40 -0700 Message-ID: <1f773384-058a-f2e3-69cf-b0e23033ec2d@gmail.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22423"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 19 16:46:08 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 1mRy51-0005bi-K2 for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 16:46:07 +0200 Original-Received: from localhost ([::1]:42696 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mRy4z-0006Ko-VI for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 10:46:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57578) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRy4j-0006KQ-5C for guile-user@gnu.org; Sun, 19 Sep 2021 10:45:49 -0400 Original-Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]:36799) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mRy4d-0000f5-Di for guile-user@gnu.org; Sun, 19 Sep 2021 10:45:48 -0400 Original-Received: by mail-pj1-x1030.google.com with SMTP id z14-20020a17090a8b8e00b0019cc29ceef1so1029884pjn.1 for ; Sun, 19 Sep 2021 07:45:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=PFNv5wy1KbRQ3bjv6PJZkZDJf/hDGcLPo0dpRc/fjU8=; b=hubp7m7YHvQf+vohq3RzD67Usd6zqcnOvnd60ksq8+EvlFpgsvh/kYzUcBveUCdlG7 u8tx06oAMDmUrpQu9f8mw6+j2SUYUZDzpMOpro63Va1ZuAZZ8vYsmm49wS/S6KNG5HxR TldQ+NHxm+5WImxwMPKZc5b4k5v09+i3BmwikgkB65OPcmRHojWnuEGZUZGawbpWPmPK jjuXU6GBss9b5e2DVyFMGUHE4dn754n1ymHddggZjbeYt9YznhWdwXHyQDPN6Krbxcyf 2onc9TDNS3XwUTIEKXCK2G6pR7lxe0IgrY52uaNCcP2Ormofq7fcKI3VQyO3gh/Dq7Q+ vfnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=PFNv5wy1KbRQ3bjv6PJZkZDJf/hDGcLPo0dpRc/fjU8=; b=eOAlwkAG1o4i9y/ugNDqWXIm/5d4pRvdsZR4Fkb0t5TFBuy5+8Wn6Sixd+oeSSJnJB QKGEkb5w30rBLyQ6CQMCJTHQa+yN65t2mqshYsg7OiLbd2hKzWVrUfcoTfCEv8P1cMi3 Avtzf6ffFhInNnOWVRae7xwgjumLDUxfYyxJTWWwemJKsGu0jQhKQpxko+W5niipccA/ Btqpro0rvSDk6mcy4URpSFWqli492PXMWMrLxnJA4bMrIs7JsoYhU+N5V/EWNNkAUzjP phVA3j/TlaDGOX5H3mBmCoSxTr5jeqnuhSGrbhPuZptkuCUWh0n1/ii84vXV4TXBQvbI kv6w== X-Gm-Message-State: AOAM532WVbvEe7zTh8MMU5tttAWyxwCjgRIdo+uj/KgYcBeOZ7ri8lCN GpZ9dsMhsBlQLj0Ocvk/f9qhNlxtW3M= X-Google-Smtp-Source: ABdhPJxzdwLRRs4pA676ZAJtqLY8mkgLIaGou7JqoG+vqHl+9zB0A8ggO4Nsze8v4IwzRvLEJxTqMQ== X-Received: by 2002:a17:90a:d686:: with SMTP id x6mr32864859pju.8.1632062741876; Sun, 19 Sep 2021 07:45:41 -0700 (PDT) Original-Received: from [192.168.2.163] (64-52-176-132.championbroadband.com. [64.52.176.132]) by smtp.gmail.com with ESMTPSA id d5sm10460363pjs.53.2021.09.19.07.45.41 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Sep 2021 07:45:41 -0700 (PDT) In-Reply-To: Content-Language: en-US Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=matt.wette@gmail.com; helo=mail-pj1-x1030.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, NICE_REPLY_A=-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-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:17758 Archived-At: 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)>