guile-devel
[Top][All Lists]
Advanced

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

"when" and "unless" syntax


From: Neil W. Van Dyke
Subject: "when" and "unless" syntax
Date: Fri, 8 Mar 2002 21:54:52 -0500

On the off chance there's still time for two semi-standard bits of
trivial yet useful syntax -- "when" and "unless" -- to make it into 1.6,
I banged out sample implementations and documentation.

I find these convenient in making hairy expressions more readable.
Example in non-hairy expression:

         (if (not (foo))        ==        (unless (foo)
             (begin                         (alpha)   
               (alpha)                      (bravo)   
               (bravo)                      (charlie))
               (charlie)))

No worries if there's not time to consider adding "when" and "unless";
just thought it was worth a try.


*** In file ice-9/boot-9.scm ***


(define-macro (when test . body)
  `(if ,test (begin ,@body)))

(define-macro (unless test . body)
  `(if (not ,test) (begin ,@body)))


*** In file doc/ref/scheme-control.texi ***


@deffn syntax when test consequent1 consequent2 @dots{}

When @var{test} evaluates to a true value, each of the consequent
expressions is evaluated in order, and the value of the last
consequent is provided as the value of the @code{when} form.  When
@var{test} evaluates to @code{#f}, none of the consequents is
evaluated, and the value of the @code{when} form is unspecified.  As
shown below, the @code{when} form is a syntactic shorthand for the
@code{if} form that is convenient when no alternate expression is
needed.

@begin lisp
(when @var{test} @var{expr1} @var{expr2} @dots{}) @equiv{} (if @var{test} 
(begin @var{expr1} @var{expr2} @dots{}))
@end lisp
@end deffn

@deffn syntax unless test expr1 expr2 @dots{}

Unless @var{test} evaluates to a true value, each of the consequent
expressions is evaluated in order, and the value of the last
consequent is provided as the value of the @code{when} form.  When
@var{test} evaluates to a true value, none of the consequents is
evaluated, and the value of the @code{when} form is unspecified.  As
shown below, the @code{unless} form is a syntactic shorthand for the
@code{if} form that is convenient when no alternate expression is
needed.

@begin lisp
(unless @var{test} @var{expr1} @var{expr2} @dots{}) @equiv{} (if (not 
@var{test}) (begin @var{expr1} @var{expr2} @dots{}))
@end lisp
@end deffn



-- 
                                                        Neil W. Van Dyke
                                             http://www.neilvandyke.org/



reply via email to

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