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: escaping from a recursive call Date: Thu, 10 Nov 2022 14:48:59 +0100 Message-ID: References: <8735asoytw.fsf@laura> <87y1sknhb5.fsf@laura> <20221110123244.af0bdfe7bb0ed55f9b761dff@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="26239"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Nov 10 14:49:54 2022 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 1ot7wI-0006a6-04 for guile-user@m.gmane-mx.org; Thu, 10 Nov 2022 14:49:54 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ot7vh-0001hD-UY; Thu, 10 Nov 2022 08:49:17 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ot7vf-0001Zv-C9 for guile-user@gnu.org; Thu, 10 Nov 2022 08:49:15 -0500 Original-Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ot7vd-0006bP-64 for guile-user@gnu.org; Thu, 10 Nov 2022 08:49:15 -0500 Original-Received: by mail-ej1-x62d.google.com with SMTP id f5so5112267ejc.5 for ; Thu, 10 Nov 2022 05:49:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=4ktyDoyQq1f1/QIvakAWO6docNE0GCI2iISJ2HlC0Xc=; b=H/iFTMLznN+r6qbC1p3mj8rLE9bXcEMQp2hEhPiJBczzHo/RxDjn83ixcgqsTupHEA UuPB27NpFsYfHeA47xgGwDhoXkTwX6foVJOTaWduxsGOV643LhN4yDeQHdask/lcdPtr dcWZpF8jd6PeCX4FiE6NUx7lYeBQT7GC52Ve43vQ8cqxnnaysAXwjKViQoa0z5eA80Il OEDWYwLjaQQeeyIvPIYc6yKgtmJvcXqx0vnWzdHc1d2xqVHM6cucbbtA0M7Kwrw5zMjx QCxPMVq6JIzKu4iiigVuBW2DsMipc9+ybuqREJJSD1LhzFNssZmGkG/R5N6Iv1hBO+pN tKIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4ktyDoyQq1f1/QIvakAWO6docNE0GCI2iISJ2HlC0Xc=; b=190Z/stDsZTBukxeWbeaSG4jmEVjSGXMUzdAmZL0c6v/8C4tLibCGCHtZmeHPVBDcA mHgOA5TwhGlCbIxc/TkfcG3kEe8BvhTpBg9/x+cjFn5mn3rWg0kKY8BCSLG7S4FvsIIz g12KPEX64f6AZaifGlJj1VdJmwwvr+zycRtClNvzUZe2X8yZSM4uqnTjDk2LEppWwWVY thWx0+sompoi4MOKb2OwnoQdGCyDSPGlyYH6qL/Hev9jnR+WTEWmcJFMZhoR860uFxxI BZeOjhclvocyYnozM24MShhvv95DPMTECtu4HPQTuMFRPtO6TV850ZqjlpzU0sVm7K8f mhgQ== X-Gm-Message-State: ACrzQf0lpN2yCuR5V8+ptX3ABx4A/Y3xZvnLL5QszqNLfwDQi5ypK+OK jWyQ1/5jjA/JVyrK3z3sAiAXaNanaIKvndOcLoG4SIVJoHaoyw== X-Google-Smtp-Source: AMsMyM71Fm7+Z42L2auE/d0aQ00hUtOAfQ4BmFzpnqmBBTtVtb4aWleWH6nh8zaTRiDV07+lWPF8LR+CcwFeuIzWZjw= X-Received: by 2002:a17:907:783:b0:76e:f290:8b5 with SMTP id xd3-20020a170907078300b0076ef29008b5mr60775961ejb.395.1668088150881; Thu, 10 Nov 2022 05:49:10 -0800 (PST) In-Reply-To: <20221110123244.af0bdfe7bb0ed55f9b761dff@gmail.com> Received-SPF: pass client-ip=2a00:1450:4864:20::62d; envelope-from=damien.mattei@gmail.com; helo=mail-ej1-x62d.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.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 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-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:18716 Archived-At: solutions have been posted, i'm using mine, which update my 'def macro: ;; (def (bar n);; (cond ((= n 0) 'end0);; ((= n 7) (return-rec 'end7));; (else (cons n (bar {n - 1}))))) ;; scheme@(guile-user)> (bar 5);; $4 = (5 4 3 2 1 . end0);; scheme@(guile-user)> (bar 10);; $5 = end7 (define-syntax def (lambda (stx) (syntax-case stx () ;; multiple definitions without values assigned ;; (def (x y z)) ((_ (var1 ...)) #`(begin (define var1 '()) ...)) ;; (def (foo) (when #t (return "hello") "bye")) ;; ((_ ( ...) * ...) ;; (let ((ret-id (datum->syntax stx 'return))) ;; #`(define ( ...) ;; (call/cc (lambda (#,ret-id) * ...))))) ((_ ( ...) * ...) (let ((ret-id (datum->syntax stx 'return)) (ret-rec-id (datum->syntax stx 'return-rec))) #`(define ( ...) (call/cc (lambda (#,ret-rec-id) (apply (rec (lambda ( ...) (call/cc (lambda (#,ret-id) * ...)))) (list ...))))))) ;; single definition without a value assigned ;; (def x) ((_ var) #`(define var '())) ;; (def x 7) ((_ var expr) #`(define var expr)) ((_ err ...) #`(syntax-error "Bad def form")) ))) i have tested it with my code and it works well,for now. Also i can not notice any slowing of the code and that is important ad 'def is a replacement often used in my projects now. I will update my Scheme+ with it soon, when i have time. Regards, Damien On Thu, Nov 10, 2022 at 1:33 PM Chris Vine wrote: > On Wed, 09 Nov 2022 12:55:42 -0500 > Olivier Dion via General Guile related discussions > wrote: > > On Wed, 09 Nov 2022, Damien Mattei wrote: > > > but in the general case , i want a macro that can do it on any > function > > > (i'm not sure it can be done because the continuation have to be > captured > > > just before the call to the function and be inlined at the good > > > place....) > > > > I'm not aware of any control mechanism that are implicit in Guile. You > > almost always have to deal with a continuation object. However, nothing > > prevent you to invent your own control flow wrapper. > > You can construct an anaphoric macro with that in mind. This introduces > an imperative-style 'loop' macro which carries within the loop block a > 'break' keyword which will cause the loop to exit: > > (use-modules (ice-9 control)) ;; for call/ec > > (define-syntax loop > (lambda (x) > (syntax-case x () > [(k e ...) > (with-syntax ([break (datum->syntax #'k 'break)]) > #'(call/ec > (lambda (break) > (let f () e ... (f)))))]))) > > (display (let ([n 3] [lst '()]) > (loop > (if (= n 0) (break lst)) > (set! lst (cons 'a lst)) > (set! n (- n 1))))) > (newline) > > However explicit control of loops is better in my view. Imperative > loops usually end up with mutable bindings, as in the example above. > >