<!--
  - Authors:
  -   Denys Duchier     <duchier@ps.uni-sb.de>
  -
  - Copyright:
  -   Denys Duchier, 1999
  -
  - Last change:
  -   $Date: 2002/01/04 14:38:59 $ by $Author: duchier $
  -   $Revision: 1.2.2.1 $
  -
  - This file is part of Mozart, an implementation
  - of Oz 3
  -    http://www.mozart-oz.org
  -
  - See the file "LICENSE" or
  -    http://www.mozart-oz.org/LICENSE.html
  - for information on usage and redistribution
  - of this file, and for a DISCLAIMER OF ALL
  - WARRANTIES.
  -
  -->
<chapter id=chapter.extensions>
  <title>Experimental Extensions</title>
  <p>The extensions that remain experimental in Mozart&nbsp;1.1.1
support the use of loops as expressions, &ie; loops that return a
value.  These extensions are supported by means of additional loop
features.</p>
  <section>
    <title/While and Until/
    <p>
      <list>
        <entry/<code/while:E//
	<item/expression <code/E/ is checked on each iteration, just
	before entering the body of the loop.  If it evaluates to
	<code/false/, we break out of the loop (see <code/break/
	feature)./
	<entry/<code/until:E//
	<item/expression <code/E/ is checked on each iteration, just
	after the body of the loop.  If it evaluates to <code/true/,
	we break out of the loop./
      </list>
      Note that it is allowed to use both features in the same loop.
    </p>
  </section>
  <section>
    <title/Find And Return a Value/
    <p>The simplest way for a loop to return a value is to
<em/explicitly/ do so.
<list>
	<entry/<code/return:R//
	<item/binds <code/R/ to a unary procedure.  Invoking
<code/{R E1}/ immediately terminates the loop returning <code/E1/ as
its value./
	<entry/<code/default:E2//
	<item/if the loop terminates without the return procedure
having been called, then <code/E2/ is returned as the <q/default/
value of the loop.  Currently <code/E2/ is evaluated before starting
the loop, but this will hopefully change so that it is only evaluated
on exit if necessary.  If no default is specified and the return
procedure is not invoked, then the loop raises an exception on
termination/
      </list></p>
  </section>
  <section>
    <title/List Accumulator/
    <p>A loop can be given a <em/hidden/ accumulator in which
a list is incrementally constructed and returned as the value of
the loop when the latter terminates.  This functionality is obtained
by using one or more of the following loop features:
<list>
	<entry/<code/collect:C//
	<item/binds <code/C/ to a unary procedure to accumulate
elements of the list.  Thus <code/{C E3}/ adds <code/E3/ as another
element of the list.  The value will appear in the list in
chronological order of collection: the last one collected appears
as last element of the list./
	<entry/<code/append:A//
	<item/binds <code/A/ to a unary procedure to append a list
of values to the accumulator./
	<entry/<code/prepend:P//
	<item/Similar, but for prepending to the accumulator./
      </list>
Note that it is possible to use the features above in conjunction
with <code/return/.  Thus you can accumulate a list, but you can also
return a different value &eg; in an exceptional situation.</p>
  </section>
  <section>
    <title/Integer Accumulator/
    <p>This functionality is even less official than for the list
accumulator: we are not at all convinced that it is sufficiently
frequently convenient to warrant special support.  It is included
simply because it was also in the Common Lisp loop macro which was
one source of inspiration.</p>
    <subsection>
      <title/Computing an Extremum/
      <p>One way to use an <q/hidden/ integer accumulator is for
computing the maximum or minimum of a set of values.
<list>
	  <entry/<code/maximize:M//
	  <item/binds <code/M/ to a unary procedure to accumulate
a maximum./
	  <entry/<code/minimum:M//
	  <item/similar but to accumulate a minimum/
	</list>
Note that <code/minimum/ and <code/maximum/ can both be used in the same
loop to update the value of the hidden accumulator.  Feature
<code/default/ can be used to provide a default value in case nothing
is accumulated.  Feature <code/return/ can also be used to return an
arbitrary value in exceptional cases.</p>
    </subsection>
    <subsection>
      <title/Summing Values/
      <p>Another way to use the integer accumulator is for computing
the sum of a sequence of values.
<list>
	  <entry/<code/sum:S//
	  <item/The initial value of the accumulator is 0.  Invoking
<code/{S E}/ adds <code/E/ to the accumulator./
	</list></p>
    </subsection>
    <subsection>
      <title/Multiplying Values/
      <p>Same idea, but using multiplication
<list>
	  <entry/<code/multiply:M//
	  <item/The initial value of the accumulator is 1.  Invoking
<code/{M E}/ multiplies the value of the accumulator by <code/E/./
	</list></p>
    </subsection>
  </section>
  <section>
    <title/Generators/
    <p>A generator is a nullary function that returns the <em/next/
    value each time it is invoked, and raises an exception to indicate
    that all values have been exhausted (any exception will do, as
    long as it doesn't have label <code/error/ or <code/failure/).
    Correspondingly, the folowing new iterator syntax is introduced:
    <list>
      <entry/<code/X from G//
      <item/where <code/G/ is a generator.
    </list>
  </section>
</chapter>
<!--
Local Variables:
mode: sgml
mode: auto-fill
sgml-default-dtd-file: "chapter.ced"
sgml-auto-activate-dtd: t
adaptive-fill-mode: nil
End:
-->
