864 |
@section SRFI-2 - and-let* |
@section SRFI-2 - and-let* |
865 |
@cindex SRFI-2 |
@cindex SRFI-2 |
866 |
|
|
867 |
@c FIXME::martin: Review me! |
@noindent |
868 |
|
The following syntax can be obtained with |
|
@findex and-let* |
|
|
The syntactic form @code{and-let*} combines the conditional evaluation |
|
|
form @code{and} with the binding form @var{let*}. Each argument |
|
|
expression will be evaluated sequentially, bound to a variable (if a |
|
|
variable name is given), but only as long as no expression returns |
|
|
the false value @code{#f}. |
|
|
|
|
|
Use @code{(use-modules (srfi srfi-2)} to access this syntax form. |
|
|
|
|
|
A short example will demonstrate how it works. In the first expression, |
|
|
@var{x} will get bound to 1, but the next expression (@code{#f}) is |
|
|
false, so evaluation of the form is stopped, and @code{#f} is returned. |
|
|
In the next expression, @var{x} is bound to 1, @var{y} is bound to |
|
|
@code{#t} and since no expression in the binding section was false, the |
|
|
body of the @code{and-let*} expression is evaluated, which in this case |
|
|
returns the value of @var{x}. |
|
869 |
|
|
870 |
@lisp |
@lisp |
871 |
(and-let* ((x 1) (y #f)) 42) |
(use-modules (srfi srfi-2)) |
|
@result{} |
|
|
#f |
|
|
(and-let* ((x 1) (y #t)) x) |
|
|
@result{} |
|
|
1 |
|
872 |
@end lisp |
@end lisp |
873 |
|
|
874 |
|
@deffn {library syntax} and-let* (clause @dots{}) body @dots{} |
875 |
|
A combination of @code{and} and @code{let*}. |
876 |
|
|
877 |
|
Each @var{clause} is evaluated in turn, and if @code{#f} is obtained |
878 |
|
then evaluation stops and @code{#f} is returned. If all are |
879 |
|
non-@code{#f} then @var{body} is evaluated and the last form gives the |
880 |
|
return value. Each @var{clause} should be one of the following, |
881 |
|
|
882 |
|
@table @code |
883 |
|
@item (symbol expr) |
884 |
|
Evaluate @var{expr}, check for @code{#f}, and bind it to @var{symbol}. |
885 |
|
Like @code{let*}, that binding is available to subsequent clauses. |
886 |
|
@item (expr) |
887 |
|
Evaluate @var{expr} and check for @code{#f}. |
888 |
|
@item symbol |
889 |
|
Get the value bound to @var{symbol} and check for @code{#f}. |
890 |
|
@end table |
891 |
|
|
892 |
|
Notice that @code{(expr)} has an ``extra'' pair of parentheses, for |
893 |
|
instance @code{((eq? x y))}. One way to remember this is to imagine |
894 |
|
the @code{symbol} in @code{(symbol expr)} is omitted. |
895 |
|
|
896 |
|
@code{and-let*} is good for calculations where a @code{#f} value means |
897 |
|
termination, but where a non-@code{#f} value is going to be needed in |
898 |
|
subsequent expressions. |
899 |
|
|
900 |
|
The following illustrates this, it returns text between brackets |
901 |
|
@samp{[...]} in a string, or @code{#f} if there are no such brackets |
902 |
|
(ie.@: either @code{string-index} gives @code{#f}). |
903 |
|
|
904 |
|
@example |
905 |
|
(define (extract-brackets str) |
906 |
|
(and-let* ((start (string-index str #\[)) |
907 |
|
(end (string-index str #\] start))) |
908 |
|
(substring str (1+ start) end))) |
909 |
|
@end example |
910 |
|
|
911 |
|
The following shows plain variables and expressions tested too. |
912 |
|
@code{diagnostic-levels} is taken to be an alist associating a |
913 |
|
diagnostic type with a level. @code{str} is printed only if the type |
914 |
|
is known and its level is high enough. |
915 |
|
|
916 |
|
@example |
917 |
|
(define (show-diagnostic type str) |
918 |
|
(and-let* (want-diagnostics |
919 |
|
(level (assq-ref diagnostic-levels type)) |
920 |
|
((>= level current-diagnostic-level))) |
921 |
|
(display str))) |
922 |
|
@end example |
923 |
|
|
924 |
|
The advantage of @code{and-let*} is that an extended sequence of |
925 |
|
expressions and tests doesn't require lots of nesting as would arise |
926 |
|
from separate @code{and} and @code{let*}, or from @code{cond} with |
927 |
|
@code{=>}. |
928 |
|
|
929 |
|
@end deffn |
930 |
|
|
931 |
|
|
932 |
@node SRFI-4 |
@node SRFI-4 |
933 |
@section SRFI-4 - Homogeneous numeric vector datatypes. |
@section SRFI-4 - Homogeneous numeric vector datatypes. |