Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > bd5c3d824c3db63ffd9226c15941e6ad > files > 1453

mozart-1.4.0-1mdv2010.0.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>2 Experimental Extensions</TITLE><LINK href="ozdoc.css" rel="stylesheet" type="text/css"></HEAD><BODY><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node1.html#chapter.official">&lt;&lt; Prev</A></TD><TD><A href="index.html">- Up -</A></TD></TR></TABLE><DIV id="chapter.extensions"><H1><A name="chapter.extensions">2 Experimental Extensions</A></H1><P>The extensions that remain experimental in Mozart&nbsp;1.1.1 support the use of loops as expressions, i.&nbsp;e. loops that return a value. These extensions are supported by means of additional loop features.</P><H2><A name="label3">2.1 While and Until</A></H2><P> </P><DL><DT><CODE>while:E</CODE></DT><DD><P>expression <CODE>E</CODE> is checked on each iteration, just before entering the body of the loop. If it evaluates to <CODE><SPAN class="keyword">false</SPAN></CODE>, we break out of the loop (see <CODE>break</CODE> feature).</P></DD><DT><CODE>until:E</CODE></DT><DD><P>expression <CODE>E</CODE> is checked on each iteration, just after the body of the loop. If it evaluates to <CODE><SPAN class="keyword">true</SPAN></CODE>, we break out of the loop.</P></DD></DL><P> Note that it is allowed to use both features in the same loop. </P><H2><A name="label4">2.2 Find And Return a Value</A></H2><P>The simplest way for a loop to return a value is to <EM>explicitly</EM> do so. </P><DL><DT><CODE>return:R</CODE></DT><DD><P>binds <CODE>R</CODE> to a unary procedure. Invoking <CODE>{R&nbsp;E1}</CODE> immediately terminates the loop returning <CODE>E1</CODE> as its value.</P></DD><DT><CODE>default:E2</CODE></DT><DD><P>if the loop terminates without the return procedure having been called, then <CODE>E2</CODE> is returned as the ``default'' value of the loop. Currently <CODE>E2</CODE> 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</P></DD></DL><P></P><H2><A name="label5">2.3 List Accumulator</A></H2><P>A loop can be given a <EM>hidden</EM> 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: </P><DL><DT><CODE>collect:C</CODE></DT><DD><P>binds <CODE>C</CODE> to a unary procedure to accumulate elements of the list. Thus <CODE>{C&nbsp;E3}</CODE> adds <CODE>E3</CODE> 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.</P></DD><DT><CODE>append:A</CODE></DT><DD><P>binds <CODE>A</CODE> to a unary procedure to append a list of values to the accumulator.</P></DD><DT><CODE>prepend:P</CODE></DT><DD><P>Similar, but for prepending to the accumulator.</P></DD></DL><P> Note that it is possible to use the features above in conjunction with <CODE>return</CODE>. Thus you can accumulate a list, but you can also return a different value e.&nbsp;g. in an exceptional situation.</P><H2><A name="label6">2.4 Integer Accumulator</A></H2><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><H3><A name="label7">2.4.1 Computing an Extremum</A></H3><P>One way to use an ``hidden'' integer accumulator is for computing the maximum or minimum of a set of values. </P><DL><DT><CODE>maximize:M</CODE></DT><DD><P>binds <CODE>M</CODE> to a unary procedure to accumulate a maximum.</P></DD><DT><CODE>minimum:M</CODE></DT><DD><P>similar but to accumulate a minimum</P></DD></DL><P> Note that <CODE>minimum</CODE> and <CODE>maximum</CODE> can both be used in the same loop to update the value of the hidden accumulator. Feature <CODE>default</CODE> can be used to provide a default value in case nothing is accumulated. Feature <CODE>return</CODE> can also be used to return an arbitrary value in exceptional cases.</P><H3><A name="label8">2.4.2 Summing Values</A></H3><P>Another way to use the integer accumulator is for computing the sum of a sequence of values. </P><DL><DT><CODE>sum:S</CODE></DT><DD><P>The initial value of the accumulator is 0. Invoking <CODE>{S&nbsp;E}</CODE> adds <CODE>E</CODE> to the accumulator.</P></DD></DL><P></P><H3><A name="label9">2.4.3 Multiplying Values</A></H3><P>Same idea, but using multiplication </P><DL><DT><CODE>multiply:M</CODE></DT><DD><P>The initial value of the accumulator is 1. Invoking <CODE>{M&nbsp;E}</CODE> multiplies the value of the accumulator by <CODE>E</CODE>.</P></DD></DL><P></P><H2><A name="label10">2.5 Lazy Generators</A></H2><P>A loop, using the features described below, is an expression which denotes a list of values that is lazily computed concurrently (i.&nbsp;e. on a separate thread). </P><DL><DT><CODE>yield:E</CODE></DT><DD><P>contributes the value of <CODE>E</CODE> to the stream, then blocks until more of the stream is needed. The <CODE>yield</CODE> feature is very similar to the <CODE>collect</CODE> feature.</P></DD><DT><CODE>yieldAppend</CODE></DT><DD><P>this is to <CODE>append</CODE> like <CODE>yield</CODE> is to <CODE>collect</CODE>.</P></DD></DL><P> </P><H2><A name="label11">2.6 Generators</A></H2><P>A generator is a nullary function that returns the <EM>next</EM> 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</CODE> or <CODE>failure</CODE>). Correspondingly, the folowing new iterator syntax is introduced: </P><DL><DT><CODE>X&nbsp;<SPAN class="keyword">from</SPAN><SPAN class="type">&nbsp;G</SPAN></CODE></DT><DD><P>where <CODE>G</CODE> is a generator. </P></DD></DL><P> </P></DIV><TABLE align="center" border="0" cellpadding="6" cellspacing="6" class="nav"><TR bgcolor="#DDDDDD"><TD><A href="node1.html#chapter.official">&lt;&lt; Prev</A></TD><TD><A href="index.html">- Up -</A></TD></TR></TABLE><HR><ADDRESS><A href="http://www.ps.uni-sb.de/~duchier/">Denys&nbsp;Duchier</A><BR><SPAN class="version">Version 1.4.0 (20090610)</SPAN></ADDRESS></BODY></HTML>