unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [Patch] definitions in when,  unless,  do as well as in cond- and case-clauses
@ 2021-06-16 19:11 Linus Björnstam
  2021-06-17  9:06 ` Maxime Devos
  2022-02-03 10:48 ` Linus Björnstam
  0 siblings, 2 replies; 24+ messages in thread
From: Linus Björnstam @ 2021-06-16 19:11 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1056 bytes --]

Hi there!

This patch updates some derived conditional forms (and do and and-let*) to support definitions in expression context. Meaning it makes this valid code:

(cond 
  ((pred? arg) 
    (define a (something arg))
    (when (error-case a)
      (error "a is broken"))
    (define b (something2 a))
    (when  (= 3 *log-level*)
      (display "logging something1 and 2 as successful"))
    (define c (something3 b a))
    (when (odd? c)
       (error "something went wrong in something3"))
    c)
  (else 
    ;;chosen by fair dice roll. Guaranteed to be random
    4)))

While awful, is sometimes what the world makes us do. 

The change means cond, case, when and unless behaves like it does in racket. Do was below case, so I changed that as well, and will actually become yet another way guile is superior to racket. 

I did also change the documentation, but I learned english by watching Beverly Hills cop, so that might need some fine touches by a capable english speaker.
            

Best regards
  Linus Björnstam

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-definitions-in-derived-conditional-forms.patch --]
[-- Type: text/x-patch; name="0001-Allow-definitions-in-derived-conditional-forms.patch", Size: 4916 bytes --]

From 7af96162bf531b70e66170864cd536032b1c89c7 Mon Sep 17 00:00:00 2001
From: Linus <bjornstam.linus@fastmail.se>
Date: Wed, 16 Jun 2021 20:47:41 +0200
Subject: [PATCH] Allow definitions in derived conditional forms

This allows definitions in the bodies of when, unless, do, and
 and-let*. It also adds support for definitions in cond- and
case-clauses.

 * doc/ref/api-control.texi (when, unless, cond, case): update
   documentation to reflect changes.
 * module/ice-9/and-let-star.scm: Changed (begin ...) to (let () ...)
 * module/ice-9/boot-9.scm (when, unless, cond, case, do): changed
   (begin ...) to (let () ...)
---
 doc/ref/api-control.texi      | 10 +++++-----
 module/ice-9/and-let-star.scm |  6 +++---
 module/ice-9/boot-9.scm       | 12 ++++++------
 3 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/doc/ref/api-control.texi b/doc/ref/api-control.texi
index 5df5344c5..eaa2b4dbf 100644
--- a/doc/ref/api-control.texi
+++ b/doc/ref/api-control.texi
@@ -152,10 +152,10 @@ documentation:
 
 @example
 (define-syntax-rule (when test stmt stmt* ...)
-  (if test (begin stmt stmt* ...)))
+  (if test (let () stmt stmt* ...)))
 
 (define-syntax-rule (unless condition stmt stmt* ...)
-  (if (not test) (begin stmt stmt* ...)))
+  (if (not test) (let () stmt stmt* ...)))
 @end example
 
 That is to say, @code{when} evaluates its consequent statements in order
@@ -167,11 +167,11 @@ statements if @var{test} is false.
 Each @code{cond}-clause must look like this:
 
 @lisp
-(@var{test} @var{expression} @dots{})
+(@var{test} @var{expression-or-definition} @dots{})
 @end lisp
 
-where @var{test} and @var{expression} are arbitrary expressions, or like
-this
+where @var{test} is an arbitrary expression, and @var{expression-or-definition}
+is an arbitrary expression or a definition, like @code{define}.
 
 @lisp
 (@var{test} => @var{expression})
diff --git a/module/ice-9/and-let-star.scm b/module/ice-9/and-let-star.scm
index 2d53ff384..9427c1733 100644
--- a/module/ice-9/and-let-star.scm
+++ b/module/ice-9/and-let-star.scm
@@ -53,12 +53,12 @@
       ((_ orig-form ((var expr)) . body)
        (identifier? #'var)
        #'(let ((var expr))
-           (and var (begin . body))))
+           (and var (let () . body))))
       ((_ orig-form ((expr)) . body)
-       #'(and expr (begin . body)))
+       #'(and expr (let () . body)))
       ((_ orig-form (var) . body)
        (identifier? #'var)
-       #'(and var (begin . body)))
+       #'(and var (let () . body)))
 
       ;; Handle bad clauses.
       ((_ orig-form (bad-clause . rest) . body)
diff --git a/module/ice-9/boot-9.scm b/module/ice-9/boot-9.scm
index 2323b1ec5..675f38633 100644
--- a/module/ice-9/boot-9.scm
+++ b/module/ice-9/boot-9.scm
@@ -417,10 +417,10 @@ If returning early, return the return value of F."
 (include-from-path "ice-9/quasisyntax")
 
 (define-syntax-rule (when test stmt stmt* ...)
-  (if test (begin stmt stmt* ...)))
+  (if test (let () stmt stmt* ...)))
 
 (define-syntax-rule (unless test stmt stmt* ...)
-  (if (not test) (begin stmt stmt* ...)))
+  (if (not test) (let () stmt stmt* ...)))
 
 (define-syntax else
   (lambda (x)
@@ -461,7 +461,7 @@ If returning early, return the return value of F."
                          ((else e e* ...)
                           (lambda (tail)
                             (if (null? tail)
-                                #'((begin e e* ...))
+                                #'((let () e e* ...))
                                 (bad-clause "else must be the last clause"))))
                          ((else . _) (bad-clause))
                          ((test => receiver)
@@ -488,7 +488,7 @@ If returning early, return the return value of F."
                          ((test e e* ...)
                           (lambda (tail)
                             #`((if test
-                                   (begin e e* ...)
+                                   (let () e e* ...)
                                    #,@tail))))
                          (_ (bad-clause))))
                      #'(clause clauses ...))))))))
@@ -534,7 +534,7 @@ If returning early, return the return value of F."
                                ((=> receiver ...)
                                 (bad-clause
                                  "wrong number of receiver expressions"))
-                               ((e e* ...) #'(begin e e* ...))
+                               ((e e* ...) #'(let () e e* ...))
                                (_ (bad-clause)))))
                          (syntax-case #'test (else)
                            ((datums ...)
@@ -585,7 +585,7 @@ If returning early, return the return value of F."
                (begin
                  (if #f #f)
                  expr ...)
-               (begin
+               (let ()
                  command
                  ...
                  (loop (do "step" var step ...)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 24+ messages in thread

end of thread, other threads:[~2022-02-07  5:56 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-16 19:11 [Patch] definitions in when, unless, do as well as in cond- and case-clauses Linus Björnstam
2021-06-17  9:06 ` Maxime Devos
2021-06-17 12:57   ` Linus Björnstam
2021-06-17 13:54     ` Maxime Devos
2022-02-03 10:48 ` Linus Björnstam
     [not found]   ` <CAGua6m1Cqe=aLOD7-jfzoqxP1aOxvWn3=UgcKyk+Dx7_51Bq3w@mail.gmail.com>
2022-02-04 20:11     ` Fwd: " Stefan Israelsson Tampe
2022-02-04 20:58       ` Maxime Devos
     [not found]         ` <CAGua6m11r27x=rq4S4fF09vHY+S-Az7GOZfmoT41hEBDSkLJQg@mail.gmail.com>
2022-02-04 21:40           ` Fwd: " Stefan Israelsson Tampe
2022-02-04 23:39           ` Maxime Devos
2022-02-05  1:14             ` Stefan Israelsson Tampe
2022-02-05 10:55               ` Maxime Devos
2022-02-05 17:31                 ` Stefan Israelsson Tampe
2022-02-05 17:36                   ` Maxime Devos
2022-02-06  6:44                   ` Linus Björnstam
2022-02-06  9:27                     ` tomas
2022-02-06  9:40                       ` Maxime Devos
2022-02-06  9:45                       ` Linus Björnstam
2022-02-06 10:48                         ` tomas
2022-02-06 20:13                           ` Stefan Israelsson Tampe
2022-02-06 20:28                             ` Maxime Devos
2022-02-06 20:31                               ` tomas
     [not found]                               ` <CAGua6m3BHMXOhT3PHZcxdqHqHomTFESO31j5CcSNK3pbA7K-Kg@mail.gmail.com>
2022-02-06 21:26                                 ` Maxime Devos
2022-02-06 21:27                                   ` Maxime Devos
2022-02-07  5:56                             ` Linus Björnstam

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).