1 2% Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. 3% All rights reserved. 4% 5% Redistribution and use in source and binary forms, with or without 6% modification, are permitted provided that the following conditions are 7% met: 8% 9% - Redistributions of source code must retain the above copyright 10% notice, this list of conditions and the following disclaimer. 11% 12% - Redistributions in binary form must reproduce the above copyright 13% notice, this list of conditions and the following disclaimer in 14% the documentation and/or other materials provided with the 15% distribution. 16% 17% - Neither the name of The Numerical ALgorithms Group Ltd. nor the 18% names of its contributors may be used to endorse or promote products 19% derived from this software without specific prior written permission. 20% 21% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22% IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23% TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 24% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 25% OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES-- LOSS OF USE, DATA, OR 28% PROFITS-- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33 34% ********************************************************************* 35\head{chapter}{Introduction to the \Language{} Interactive Language}{ugLang} 36% ********************************************************************* 37 38In this chapter we look at some of the basic components of the 39\Language{} language that you can use interactively. 40We show how to create a \spadgloss{block} of expressions, 41how to form loops and list iterations, how to modify the sequential 42evaluation of a block and how to use {\tt if-then-else} to 43evaluate parts of your program conditionally. 44We suggest you first read the boxed material in each section and then 45proceed to a more thorough reading of the chapter. 46 47% ********************************************************************* 48\head{section}{Immediate and Delayed Assignments}{ugLangAssign} 49% ********************************************************************* 50 51A \spadgloss{variable} in \Language{} refers to a value. 52A variable has a name beginning with an uppercase or lowercase alphabetic 53character, \spadSyntax{%}, or \spadSyntax{!}. 54Successive characters (if any) can be any of the above, digits, or 55\spadSyntax{?}. 56Case is distinguished. 57The following are all examples of valid, distinct variable names: 58\begin{verbatim} 59a tooBig? a1B2c3%!? 60A %j numberOfPoints 61beta6 %J numberofpoints 62\end{verbatim} 63 64The \spadSyntax{:=} operator is the immediate \spadgloss{assignment} 65operator. 66\index{assignment!immediate} 67Use it to associate a value with a variable. 68\index{immediate assignment} 69 70\beginImportant 71The syntax for immediate assignment for a single variable is 72\begin{center} 73{\it variable} \spad{:=} {\it expression} 74\end{center} 75The value returned by an immediate assignment is the value of {\it expression}. 76\endImportant 77 78\xtc{ 79The right-hand side of the expression is evaluated, 80yielding \spad{1}. This value is then assigned to \spad{a}. 81}{ 82\spadcommand{a := 1 \bound{a}} 83} 84\xtc{ 85The right-hand side of the expression is evaluated, 86yielding \spad{1}. This value is then assigned to \spad{b}. 87Thus \spad{a} and \spad{b} both have the value \spad{1} after the sequence 88of assignments. 89}{ 90\spadcommand{b := a \free{a}\bound{b}} 91} 92\xtc{ 93What is the value of \spad{b} if \spad{a} is 94assigned the value \spad{2}? 95}{ 96\spadcommand{a := 2 \bound{a2}} 97} 98\xtc{ 99As you see, the value of \spad{b} is left unchanged. 100}{ 101\spadcommand{b \free{b}} 102} 103This is what we mean when we say this kind of assignment is 104{\it immediate}; 105\spad{b} has no dependency on \spad{a} after the initial assignment. 106This is the usual notion of assignment found in programming 107languages such as C, 108\index{C language!assignment} 109PASCAL 110\index{PASCAL!assignment} 111and FORTRAN. 112\index{FORTRAN!assignment} 113 114\Language{} provides delayed assignment with \spadSyntax{==}. 115\index{assignment!delayed} 116This implements a 117\index{delayed assignment} 118delayed evaluation of the right-hand side and dependency 119checking. 120 121\beginImportant 122The syntax for delayed assignment is 123\begin{center} 124{\it variable} \spad{==} {\it expression} 125\end{center} 126The value returned by a delayed assignment is \void{}. 127\endImportant 128 129\xtc{ 130Using \spad{a} and \spad{b} as above, these are the corresponding delayed 131assignments. 132}{ 133\spadcommand{a == 1 \bound{ad}} 134} 135\xtc{ 136}{ 137\spadcommand{b == a \free{ad}\bound{bd}} 138} 139\xtc{ 140The right-hand side of each delayed assignment 141is left unevaluated until the 142variables on the left-hand sides are evaluated. 143Therefore this evaluation and \ldots 144}{ 145\spadcommand{a \free{ad}} 146} 147\xtc{ 148this evaluation seem the same as before. 149}{ 150\spadcommand{b \free{bd}} 151} 152\xtc{ 153If we change \spad{a} to \spad{2} 154}{ 155\spadcommand{a == 2 \bound{ad2}} 156} 157\xtc{ 158then 159\spad{a} evaluates to \spad{2}, as expected, but 160}{ 161\spadcommand{a \free{ad2}} 162} 163\xtc{ 164the value of \spad{b} reflects the change to \spad{a}. 165}{ 166\spadcommand{b \free{bd ad2}} 167} 168 169It is possible to set several variables at the same time 170\index{assignment!multiple immediate} 171by using 172\index{multiple immediate assignment} 173a \spadgloss{tuple} of variables and a tuple of expressions.\footnote{A 174\spadgloss{tuple} is a collection of things separated by commas, often 175surrounded by parentheses.} 176 177% ---------------------------------------------------------------------- 178\beginImportant 179The syntax for multiple immediate assignments is 180\begin{center} 181{\tt ( \subscriptIt{var}{1}, \subscriptIt{var}{2}, \ldots, \subscriptIt{var}{N} ) := ( \subscriptIt{expr}{1}, \subscriptIt{expr}{2}, \ldots, \subscriptIt{expr}{N} ) } 182\end{center} 183The value returned by an immediate assignment is the value of 184\subscriptIt{expr}{N}. 185\endImportant 186% ---------------------------------------------------------------------- 187 188\xtc{ 189This sets \spad{x} to \spad{1} and \spad{y} to \spad{2}. 190}{ 191\spadcommand{(x,y) := (1,2) \bound{x}\bound{y}} 192} 193Multiple immediate assignments are parallel in the sense that the 194expressions on the right are all evaluated before any assignments 195on the left are made. 196However, the order of evaluation of these expressions is undefined. 197\xtc{ 198You can use multiple immediate assignment to swap the 199values held by variables. 200}{ 201\spadcommand{(x,y) := (y,x) \free{x y}\bound{swap}} 202} 203\xtc{ 204\spad{x} has the previous value of \spad{y}. 205}{ 206\spadcommand{x \free{swap}} 207} 208\xtc{ 209\spad{y} has the previous value of \spad{x}. 210}{ 211\spadcommand{y \free{swap}} 212} 213 214There is no syntactic form for multiple delayed assignments. 215See the discussion in 216\spadref{ugUserDelay} 217about how \Language{} differentiates between delayed assignments and 218user functions of no arguments. 219 220% ********************************************************************* 221\head{section}{Blocks}{ugLangBlocks} 222% ********************************************************************* 223 224%% 225%% We should handle tabs in pile correctly but so far we do not. 226%% 227 228A \spadgloss{block} is a sequence of expressions evaluated 229in the order that they appear, except as modified by control expressions 230such as \spad{break}, 231\spadkey{break} 232\spad{return}, 233\spadkey{return} 234\spad{iterate} and 235\spadkey{iterate} 236\spad{if-then-else} constructions. 237The value of a block is the value of the expression last evaluated 238in the block. 239 240To leave a block early, use \spadSyntax{=>}. 241For example, \spad{i < 0 => x}. 242The expression before the \spadSyntax{=>} must evaluate to 243\spad{true} or \spad{false}. 244The expression following the \spadSyntax{=>} is the return value 245for the block. 246 247A block can be constructed in two ways: 248\begin{enumerate} 249\item the expressions can be separated by semicolons 250and the resulting expression surrounded by parentheses, and 251\item the expressions can be written on succeeding lines with each line 252indented the same number of spaces (which must be greater than zero). 253\index{indentation} 254A block entered in this form is 255called a \spadgloss{pile}. 256\end{enumerate} 257Only the first form is available if you are entering expressions 258directly to \Language{}. 259Both forms are available in {\bf .input} files. 260 261\beginImportant 262The syntax for a simple block of expressions entered interactively is 263\begin{center} 264{\tt ( \subscriptIt{expression}{1}; \subscriptIt{expression}{2}; \ldots; \subscriptIt{expression}{N} )} 265\end{center} 266The value returned by a block is the value of an 267\spadSyntax{=>} expression, or \subscriptIt{expression}{N} 268if no \spadSyntax{=>} is encountered. 269\endImportant 270 271In {\bf .input} files, blocks can also be written using 272\spadglossSee{piles}{pile}. 273The examples throughout this book are assumed to come from {\bf .input} files. 274 275\xtc{ 276In this example, we assign a rational number to \spad{a} using a block 277consisting of three expressions. 278This block is written as a pile. 279Each expression in the pile has the same indentation, in this case two 280spaces to the right of the first line. 281}{ 282\begin{spadsrc} 283a := 284 i := gcd(234,672) 285 i := 3*i^5 - i + 1 286 1 / i 287\end{spadsrc} 288} 289\xtc{ 290Here is the same block written on one line. 291This is how you are required to enter it at the input prompt. 292}{ 293\spadcommand{a := (i := gcd(234,672); i := 3*i^5 - i + 1; 1 / i)} 294} 295\xtc{ 296Blocks can be used to put several expressions on one line. 297The value returned is that of the last expression. 298}{ 299\spadcommand{(a := 1; b := 2; c := 3; [a,b,c]) \bound{a b c}} 300} 301 302\Language{} gives you two ways of writing a block and the 303preferred way in an {\bf .input} file is to use a pile. 304\index{file!input} 305Roughly speaking, a pile is 306a block whose constituent expressions are indented the same amount. 307You begin a pile by starting a new line for the first expression, 308indenting it to the right of the previous line. 309You then enter the second expression on a new line, vertically aligning 310it with the first line. And so on. 311If you need to enter an inner pile, further indent its lines to the right 312of the outer pile. 313\Language{} knows where a pile ends. 314It ends when a subsequent line is indented to the left of the pile or 315the end of the file. 316 317\xtc{ 318Blocks can be used to perform several steps before an assignment 319(immediate or delayed) is made. 320}{ 321\begin{spadsrc}[\free{a b}] 322d := 323 c := a^2 + b^2 324 sqrt(c * 1.3) 325\end{spadsrc} 326} 327\xtc{ 328Blocks can be used in the arguments to functions. 329(Here \spad{h} is assigned \spad{2.1 + 3.5}.) 330}{ 331\begin{spadsrc}[\bound{h}] 332h := 2.1 + 333 1.0 334 3.5 335\end{spadsrc} 336} 337\xtc{ 338Here the second argument to \spadfun{eval} is \spad{x = z}, where 339the value of \spad{z} is computed in the first line of the block 340starting on the second line. 341}{ 342\begin{spadsrc} 343eval(x^2 - x*y^2, 344 z := %pi/2.0 - exp(4.1) 345 x = z 346 ) 347\end{spadsrc} 348} 349\xtc{ 350Blocks can be used in the clauses of \spad{if-then-else} 351expressions (see \spadref{ugLangIf}). 352}{ 353\spadcommand{if h > 3.1 then 1.0 else (z := cos(h); max(z,0.5)) \free{h}} 354} 355\xtc{ 356This is the pile version of the last block. 357}{ 358\begin{spadsrc}[\free{h}] 359if h > 3.1 then 360 1.0 361 else 362 z := cos(h) 363 max(z,0.5) 364\end{spadsrc} 365} 366\xtc{ 367Blocks can be nested. 368}{ 369\spadcommand{a := (b := factorial(12); c := (d := eulerPhi(22); factorial(d));b+c)} 370} 371\xtc{ 372This is the pile version of the last block. 373}{ 374\begin{spadsrc} 375a := 376 b := factorial(12) 377 c := 378 d := eulerPhi(22) 379 factorial(d) 380 b+c 381\end{spadsrc} 382} 383 384\xtc{ 385Since \spad{c + d} does equal \spad{3628855}, \spad{a} has the value 386of \spad{c} and the last line is never evaluated. 387}{ 388\begin{spadsrc} 389a := 390 c := factorial 10 391 d := fibonacci 10 392 c + d = 3628855 => c 393 d 394\end{spadsrc} 395} 396 397% ********************************************************************* 398\head{section}{if-then-else}{ugLangIf} 399% ********************************************************************* 400 401Like many other programming languages, \Language{} uses the three 402% Remark: following strangeness with \spadkey is because \spadkey 403% only creates an index term. Multiple sequential index macros without 404% intervening text cause strange spaces in the text. 405keywords \spadkey{if} \spad{if, then} \spadkey{then} and \spad{else} 406\spadkey{else} to form 407\index{conditional} 408conditional expressions. 409The \spad{else} part of the conditional is optional. 410The expression between the \spad{if} and \spad{then} keywords 411is a 412\spadgloss{predicate}: an expression that evaluates to or is convertible to 413either {\tt true} or {\tt false}, that is, 414a \spadtype{Boolean}. 415\exptypeindex{Boolean} 416 417% ---------------------------------------------------------------------- 418\beginImportant 419The syntax for conditional expressions is 420\begin{center} 421{\tt if {\it predicate} then \subscriptIt{expression}{1} else \subscriptIt{expression}{2}} 422\end{center} 423where the \spad{else} \subscriptIt{\it expression}{2} part is optional. 424The value returned from a conditional expression is 425\subscriptIt{\it expression}{1} if the predicate evaluates to \spad{true} 426and \subscriptIt{\it expression}{2} otherwise. 427If no \spad{else} clause is given, the value is always \void{}. 428\endImportant 429% ---------------------------------------------------------------------- 430 431An \spad{if-then-else} expression always returns a value. 432If the 433\spad{else} clause is missing then the entire expression returns 434\void{}. 435If both clauses are present, the type of the value returned by \spad{if} 436is obtained by resolving the types of the values of the two clauses. 437See \spadref{ugTypesResolve} 438for more information. 439 440The predicate must evaluate to, or be convertible to, an object of type 441\spadtype{Boolean}: {\tt true} or {\tt false}. 442By default, the equal sign \spadopFrom{=}{Equation} creates 443\index{equation} 444an equation. 445 446\xtc{ 447This is an equation. 448\exptypeindex{Equation} 449In particular, it is an object of type \spadtype{Equation Polynomial Integer}. 450}{ 451\spadcommand{x + 1 = y} 452} 453However, for predicates in \spad{if} expressions, \Language{} 454\index{equality testing} 455places a default target type of \spadtype{Boolean} on the 456predicate and equality testing is performed. 457Thus you need not qualify the \spadSyntax{=} in any way. 458In other contexts you may need to tell \Language{} that you want 459to test for equality rather than create an equation. 460In those cases, use \spadSyntax{@} and a target type of 461\spadtype{Boolean}. 462See \spadref{ugTypesPkgCall} for more information. 463 464The compound symbol meaning ``not equal'' in \Language{} is 465\index{inequality testing} 466\spadop{~=}. 467This can be used directly without a package call or a target specification. 468The expression 469\spad{a ~= b} is directly translated into 470\spad{not (a = b)}. 471 472Many other functions have return values of type \spadtype{Boolean}. 473These include \spadop{<}, \spadop{<=}, \spadop{>}, 474\spadop{>=}, \spadop{~=} and \spad{member?}. 475By convention, operations with names ending in \spadSyntax{?} 476return \spadtype{Boolean} values. 477 478The usual rules for piles are suspended for conditional expressions. 479In {\bf .input} files, the \spad{then} and 480\spad{else} keywords can begin in the same column as the corresponding 481\spad{if} but may also appear to the right. 482Each of the following styles of writing \spad{if-then-else} 483expressions is acceptable: 484\begin{verbatim} 485if i>0 then output("positive") else output("nonpositive") 486 487if i > 0 then output("positive") 488 else output("nonpositive") 489 490if i > 0 then output("positive") 491else output("nonpositive") 492 493if i > 0 494then output("positive") 495else output("nonpositive") 496 497if i > 0 498 then output("positive") 499 else output("nonpositive") 500\end{verbatim} 501 502A block can follow the \spad{then} or \spad{else} keywords. 503In the following two assignments to \spad{a}, the \spad{then} and \spad{else} 504clauses each are followed by two-line piles. 505The value returned in each is the value of the second line. 506 507\begin{verbatim} 508a := 509 if i > 0 then 510 j := sin(i * pi()) 511 exp(j + 1/j) 512 else 513 j := cos(i * 0.5 * pi()) 514 log(abs(j)^5 + 1) 515 516a := 517 if i > 0 518 then 519 j := sin(i * pi()) 520 exp(j + 1/j) 521 else 522 j := cos(i * 0.5 * pi()) 523 log(abs(j)^5 + 1) 524\end{verbatim} 525These are both equivalent to the following: 526\begin{verbatim} 527a := 528 if i > 0 then (j := sin(i * pi()); exp(j + 1/j)) 529 else (j := cos(i * 0.5 * pi()); log(abs(j)^5 + 1)) 530\end{verbatim} 531 532% ********************************************************************* 533\head{section}{Loops}{ugLangLoops} 534% ********************************************************************* 535 536A \spadgloss{loop} is an expression that contains another expression, 537\index{loop} 538called the {\it loop body}, which is to be evaluated zero or more 539\index{loop!body} 540times. 541All loops contain the \spad{repeat} keyword and return \void{}. 542Loops can contain inner loops to any depth. 543 544\beginImportant 545The most basic loop is of the form 546\begin{center} 547\spad{repeat} {\it loopBody} 548\end{center} 549Unless {\it loopBody} contains a \spad{break} or \spad{return} expression, 550the loop repeats forever. 551The value returned by the loop is \void{}. 552\endImportant 553 554% ********************************************************************* 555\head{subsection}{Compiling vs. Interpreting Loops}{ugLangLoopsCompInt} 556% ********************************************************************* 557 558\Language{} tries to determine completely the type of every 559object in a loop and then to translate the loop body to LISP or even to 560machine code. 561This translation is called \spadglossSee{compilation}{compiler}. 562 563If \Language{} decides that it cannot compile the loop, it issues a 564\index{loop!compilation} 565message stating the problem and then the following message: 566% 567\begin{center} 568{\bf We will attempt to step through and interpret the code.} 569\end{center} 570% 571It is still possible that \Language{} can evaluate the loop but in 572\spadgloss{interpret-code mode}. 573See \spadref{ugUserCompInt} where this is discussed in terms 574\index{panic!avoiding} 575of compiling versus interpreting functions. 576 577% ********************************************************************* 578\head{subsection}{return in Loops}{ugLangLoopsReturn} 579% ********************************************************************* 580 581A \spad{return} expression is used to exit a function with 582\index{loop!leaving via return} 583a particular value. 584In particular, if a \spad{return} is in a loop within the 585\spadkey{return} 586function, the loop is terminated whenever the \spad{return} 587is evaluated. 588%> This is a bug! The compiler should never accept allow 589%> Void to be the return type of a function when it has to use 590%> resolve to determine it. 591\xtc{ 592Suppose we start with this. 593}{ 594\begin{spadsrc}[\bound{f}] 595f() == 596 i := 1 597 repeat 598 if factorial(i) > 1000 then return i 599 i := i + 1 600\end{spadsrc} 601} 602\xtc{ 603When \spad{factorial(i)} is big enough, control passes from 604inside the loop all the way outside the function, returning the 605value of \spad{i} (or so we think). 606}{ 607\spadcommand{f() \free{f}} 608} 609 610What went wrong? 611Isn't it obvious that this function should return an integer? 612Well, \Language{} makes no attempt to analyze the structure of a 613loop to determine if it always returns a value because, in 614general, this is impossible. 615So \Language{} has this simple rule: the type of the function is 616determined by the type of its body, in this case a block. 617The normal value of a block is the value of its last expression, 618in this case, a loop. 619And the value of every loop is \void{}! 620So the return type of \userfun{f} is \spadtype{Void}. 621 622There are two ways to fix this. 623The best way is for you to tell \Language{} what the return type 624of \spad{f} is. 625You do this by giving \spad{f} a declaration \spad{f: () -> Integer} 626prior to calling for its value. 627This tells \Language{}: ``trust me---an integer is returned.'' 628We'll explain more about this in the next chapter. 629Another clumsy way is to add a dummy expression as follows. 630 631\xtc{ 632Since we want an integer, let's stick in a dummy final expression that is 633an integer and will never be evaluated. 634}{ 635\begin{spadsrc}[\bound{f1}] 636f() == 637 i := 1 638 repeat 639 if factorial(i) > 1000 then return i 640 i := i + 1 641 0 642\end{spadsrc} 643} 644\xtc{ 645When we try \userfun{f} again we get what we wanted. 646See 647\spadref{ugUserBlocks} 648for more information. 649}{ 650\spadcommand{f() \free{f1}} 651} 652 653% ********************************************************************* 654\head{subsection}{break in Loops}{ugLangLoopsBreak} 655% ********************************************************************* 656 657The \spad{break} keyword is often more useful 658\spadkey{break} 659in terminating 660\index{loop!leaving via break} 661a loop. 662%> and more in keeping with the ideas of structured programming. 663A \spad{break} causes control to transfer to the expression 664immediately following the loop. 665As loops always return \void{}, 666you cannot return a value with \spad{break}. 667That is, \spad{break} takes no argument. 668 669\xtc{ 670This example is a modification of the last example in 671\texht{the previous section}{\spadref{ugLangLoopsReturn}}. 672Instead of using \spad{return}, we'll use \spad{break}. 673}{ 674\begin{spadsrc}[\bound{f1}] 675f() == 676 i := 1 677 repeat 678 if factorial(i) > 1000 then break 679 i := i + 1 680 i 681\end{spadsrc} 682} 683\xtc{ 684The loop terminates when \spad{factorial(i)} gets big enough, 685the last line of the function evaluates to the corresponding ``good'' 686value of \spad{i}, and the function terminates, returning that value. 687}{ 688\spadcommand{f() \free{f1}} 689} 690\xtc{ 691You can only use \spad{break} to terminate the evaluation of one loop. 692Let's consider a loop within a loop, that is, a loop with a nested loop. 693First, we initialize two counter variables. 694}{ 695\spadcommand{(i,j) := (1, 1) \bound{i}\bound{j}} 696} 697\xtc{ 698Nested loops must have multiple \spad{break} 699\index{loop!nested} 700expressions at the appropriate nesting level. 701How would you rewrite this so \spad{(i + j) > 10} is only evaluated once? 702}{ 703\begin{spadsrc}[\free{i j}] 704repeat 705 repeat 706 if (i + j) > 10 then break 707 j := j + 1 708 if (i + j) > 10 then break 709 i := i + 1 710\end{spadsrc} 711} 712 713% ********************************************************************* 714\head{subsection}{break vs. {\tt =>} in Loop Bodies}{ugLangLoopsBreakVs} 715% ********************************************************************* 716 717Compare the following two loops: 718 719\begin{verbatim} 720i := 1 i := 1 721repeat repeat 722 i := i + 1 i := i + 1 723 i > 3 => i if i > 3 then break 724 output(i) output(i) 725\end{verbatim} 726 727In the example on the left, the values 728\mathOrSpad{2} and \mathOrSpad{3} for \spad{i} are displayed 729but then the \spadSyntax{=>} does not allow control to reach the call to 730\spadfunFrom{output}{OutputForm} again. 731The loop will not terminate 732until you run out of space or interrupt the execution. 733The variable \spad{i} will continue to be incremented because 734the \spadSyntax{=>} only means to leave the {\it block,} not the loop. 735 736In the example on the right, 737upon reaching \mathOrSpad{4}, the \spad{break} will be 738executed, and both the block and the loop will terminate. 739This is one of the reasons why both \spadSyntax{=>} and \spad{break} are 740provided. 741Using a \spad{while} clause (see below) with the \spadSyntax{=>} 742\spadkey{while} 743lets you simulate the action of \spad{break}. 744 745% ********************************************************************* 746\head{subsection}{More Examples of break}{ugLangLoopsBreakMore} 747% ********************************************************************* 748 749Here we give four examples of \spad{repeat} loops that 750terminate when a value exceeds a given bound. 751 752\texht{\vskip 1pc}{} 753\xtc{ 754First, initialize \spad{i} as the loop counter. 755}{ 756\spadcommand{i := 0 \bound{i}} 757} 758\xtc{ 759Here is the first loop. 760When the square of \spad{i} exceeds \spad{100}, the loop terminates. 761}{ 762\begin{spadsrc}[\free{i}\bound{i1}] 763repeat 764 i := i + 1 765 if i^2 > 100 then break 766\end{spadsrc} 767} 768\xtc{ 769Upon completion, \spad{i} should have the value \spad{11}. 770}{ 771\spadcommand{i \free{i1}} 772} 773% 774% 775\xtc{ 776Do the same thing except use \spadSyntax{=>} instead 777an \spad{if-then} expression. 778}{ 779\spadcommand{i := 0 \bound{i2}} 780} 781\xtc{ 782}{ 783\begin{spadsrc}[\free{i2}\bound{i3}] 784repeat 785 i := i + 1 786 i^2 > 100 => break 787\end{spadsrc} 788} 789\xtc{ 790}{ 791\spadcommand{i \free{i3}} 792} 793% 794% 795\xtc{ 796As a third example, we use a simple loop to compute \spad{n!}. 797}{ 798\spadcommand{(n, i, f) := (100, 1, 1) \bound{n}\bound{i4}\bound{f}} 799} 800\xtc{ 801Use \spad{i} as the iteration variable and \spad{f} 802to compute the factorial. 803}{ 804\begin{spadsrc}[\bound{f1}\bound{i5}\free{f i4 n}] 805repeat 806 if i > n then break 807 f := f * i 808 i := i + 1 809\end{spadsrc} 810} 811\xtc{ 812Look at the value of \spad{f}. 813}{ 814\spadcommand{f \free{f1}} 815} 816% 817% 818\xtc{ 819Finally, we show an example of nested loops. 820First define a four by four matrix. 821}{ 822\spadcommand{m := matrix [[21,37,53,14], [8,-24,22,-16], [2,10,15,14], [26,33,55,-13]] \bound{m2}} 823} 824\xtc{ 825Next, set row counter \spad{r} and column counter \spad{c} to 826\mathOrSpad{1}. 827Note: if we were writing a function, these would all be local 828variables rather than global workspace variables. 829}{ 830\spadcommand{(r, c) := (1, 1) \bound{r}\bound{c}} 831} 832\xtc{ 833Also, let \spad{lastrow} and 834\spad{lastcol} be the final row and column index. 835}{ 836\spadcommand{(lastrow, lastcol) := (nrows(m), ncols(m)) \bound{lastrow}\bound{lastcol}\free{m2}} 837} 838% 839\xtc{ 840Scan the rows looking for the first negative element. 841We remark that you can reformulate this example in a better, more 842concise form by using a \spad{for} clause with \spad{repeat}. 843See 844\spadref{ugLangLoopsForIn} 845for more information. 846}{ 847\begin{spadsrc}[\free{m2 r c lastrow lastcol}] 848repeat 849 if r > lastrow then break 850 c := 1 851 repeat 852 if c > lastcol then break 853 if elt(m,r,c) < 0 then 854 output [r, c, elt(m,r,c)] 855 r := lastrow 856 break -- don't look any further 857 c := c + 1 858 r := r + 1 859\end{spadsrc} 860} 861 862% ********************************************************************* 863\head{subsection}{iterate in Loops}{ugLangLoopsIterate} 864% ********************************************************************* 865 866\Language{} provides an \spad{iterate} expression that 867\spadkey{iterate} 868skips over the remainder of a loop body and starts the next loop iteration. 869\xtc{ 870We first initialize a counter. 871}{ 872\spadcommand{i := 0 \bound{i}} 873} 874\xtc{ 875Display the even integers from \spad{2} to \spad{5}. 876}{ 877\begin{spadsrc}[\free{i}] 878repeat 879 i := i + 1 880 if i > 5 then break 881 if odd?(i) then iterate 882 output(i) 883\end{spadsrc} 884} 885 886% ********************************************************************* 887\head{subsection}{while Loops}{ugLangLoopsWhile} 888% ********************************************************************* 889 890The \spad{repeat} in a loop can be modified by adding one or 891more \spad{while} clauses. 892\spadkey{while} 893Each clause contains a \spadgloss{predicate} 894immediately following the \spad{while} keyword. 895The predicate is tested {\it before} 896the evaluation of the body of the loop. 897The loop body is evaluated whenever the predicates in a \spad{while} 898clause are all \spad{true}. 899 900\beginImportant 901The syntax for a simple loop using \spad{while} is 902\begin{center} 903\spad{while} {\it predicate} \spad{repeat} {\it loopBody} 904\end{center} 905The {\it predicate} is evaluated before {\it loopBody} is evaluated. 906A \spad{while} loop terminates immediately when {\it predicate} 907evaluates to \spad{false} or when a \spad{break} or \spad{return} 908expression is evaluated in {\it loopBody}. 909The value returned by the loop is \void{}. 910\endImportant 911 912\xtc{ 913Here is a simple example of using \spad{while} in a loop. 914We first initialize the counter. 915}{ 916\spadcommand{i := 1 \bound{i}} 917} 918\xtc{ 919The steps involved in computing this example are 920(1) set \spad{i} to \spad{1}, (2) test the condition \spad{i < 1} and 921determine that it is not true, and (3) do not evaluate the 922loop body and therefore do not display \spad{"hello"}. 923}{ 924\begin{spadsrc}[\free{i}] 925while i < 1 repeat 926 output "hello" 927 i := i + 1 928\end{spadsrc} 929} 930\xtc{ 931If you have multiple predicates to be tested use the 932logical \spad{and} operation to separate them. 933\Language{} evaluates these predicates from left to right. 934}{ 935\spadcommand{(x, y) := (1, 1) \bound{x}\bound{y}} 936} 937\xtc{ 938}{ 939\begin{spadsrc}[\free{x y}] 940while x < 4 and y < 10 repeat 941 output [x,y] 942 x := x + 1 943 y := y + 2 944\end{spadsrc} 945} 946\xtc{ 947A \spad{break} expression can be included in a loop body to terminate a 948loop even if the predicate in any \spad{while} clauses are not \spad{false}. 949}{ 950\spadcommand{(x, y) := (1, 1) \bound{x1}\bound{y1}} 951} 952\xtc{ 953This loop has multiple \spad{while} clauses and the loop terminates 954before any one of their conditions evaluates to \spad{false}. 955}{ 956\begin{spadsrc}[\free{x1 y1}] 957while x < 4 while y < 10 repeat 958 if x + y > 7 then break 959 output [x,y] 960 x := x + 1 961 y := y + 2 962\end{spadsrc} 963} 964\xtc{ 965Here's a different version of the nested loops that looked 966for the first negative element in a matrix. 967}{ 968\spadcommand{m := matrix [[21,37,53,14], [8,-24,22,-16], [2,10,15,14], [26,33,55,-13]] \bound{m2}} 969} 970\xtc{ 971Initialized the row index to \spad{1} and 972get the number of rows and columns. 973If we were writing a function, these would all be 974local variables. 975}{ 976\spadcommand{r := 1 \bound{r}} 977} 978\xtc{ 979}{ 980\spadcommand{(lastrow, lastcol) := (nrows(m), ncols(m)) \bound{lastrow}\bound{lastcol}\free{m2}} 981} 982% 983\xtc{ 984Scan the rows looking for the first negative element. 985}{ 986\begin{spadsrc}[\free{m2 r lastrow lastcol}] 987while r <= lastrow repeat 988 c := 1 -- index of first column 989 while c <= lastcol repeat 990 if elt(m,r,c) < 0 then 991 output [r, c, elt(m,r,c)] 992 r := lastrow 993 break -- don't look any further 994 c := c + 1 995 r := r + 1 996\end{spadsrc} 997} 998 999% ********************************************************************* 1000\head{subsection}{for Loops}{ugLangLoopsForIn} 1001% ********************************************************************* 1002 1003\Language{} provides the \spad{for} 1004\spadkey{for} 1005and \spad{in} 1006\spadkey{in} 1007keywords in \spad{repeat} loops, 1008allowing you to iterate across all 1009\index{iteration} 1010elements of a list, or to have a variable take on integral values 1011from a lower bound to an upper bound. 1012We shall refer to these modifying clauses of \spad{repeat} loops as 1013\spad{for} clauses. 1014These clauses can be present in addition to \spad{while} clauses. 1015As with all other types of \spad{repeat} loops, \spad{break} can 1016\spadkey{break} 1017be used to prematurely terminate the evaluation of the loop. 1018 1019\beginImportant 1020The syntax for a simple loop using \spad{for} is 1021\begin{center} 1022\spad{for} {\it iterator} \spad{repeat} {\it loopBody} 1023\end{center} 1024The {\it iterator} has several forms. 1025Each form has an end test which is evaluated 1026before {\it loopBody} is evaluated. 1027A \spad{for} loop terminates immediately when the end test 1028succeeds (evaluates to \spad{true}) or when a \spad{break} or \spad{return} 1029expression is evaluated in {\it loopBody}. 1030The value returned by the loop is \void{}. 1031\endImportant 1032 1033% ********************************************************************* 1034\head{subsection}{for i in n..m repeat}{ugLangLoopsForInNM} 1035% ********************************************************************* 1036 1037If \spad{for} 1038\spadkey{for} 1039is followed by a variable name, the \spad{in} 1040\spadkey{in} 1041keyword and then an integer segment of the form \spad{n..m}, 1042\index{segment} 1043the end test for this loop is the predicate \spad{i > m}. 1044The body of the loop is evaluated \spad{m-n+1} times if this 1045number is greater than 0. 1046If this number is less than or equal to 0, the loop body is not evaluated 1047at all. 1048 1049The variable \spad{i} has the value 1050\spad{n, n+1, ..., m} for successive iterations 1051of the loop body. 1052The loop variable is a \spadgloss{local variable} 1053within the loop body: its value is not available outside the loop body 1054and its value and type within the loop body completely mask any outer 1055definition of a variable with the same name. 1056 1057% 1058\xtc{ 1059This loop prints the values of 1060\texht{${10}^3$, ${11}^3$, and $12^3$}{\spad{10^3, 11^3, and 12^3}}: 1061}{ 1062\spadcommand{for i in 10..12 repeat output(i^3)} 1063} 1064% 1065\xtc{ 1066Here is a sample list. 1067}{ 1068\spadcommand{a := [1,2,3] \bound{a}} 1069} 1070\xtc{ 1071Iterate across this list, using \spadSyntax{.} to access the elements of a list and 1072the \spadop{#} operation to count its elements. 1073}{ 1074\spadcommand{for i in 1..\#a repeat output(a.i) \free{a}} 1075} 1076% 1077This type of iteration is applicable to anything that uses \spadSyntax{.}. 1078You can also use it with functions that use indices to extract elements. 1079% 1080\xtc{ 1081Define \spad{m} to be a matrix. 1082}{ 1083\spadcommand{m := matrix [[1,2],[4,3],[9,0]] \bound{m}} 1084} 1085\xtc{ 1086Display the rows of \spad{m}. 1087}{ 1088\spadcommand{for i in 1..nrows(m) repeat output row(m,i) \free{m}} 1089} 1090% 1091You can use \spad{iterate} with \spad{for}-loops. 1092\spadkey{iterate} 1093\xtc{ 1094Display the even integers in a segment. 1095}{ 1096\begin{spadsrc} 1097for i in 1..5 repeat 1098 if odd?(i) then iterate 1099 output(i) 1100\end{spadsrc} 1101} 1102 1103See \xmpref{Segment} for more information about segments. 1104 1105% ********************************************************************* 1106\head{subsection}{for i in n..m by s repeat}{ugLangLoopsForInNMS} 1107% ********************************************************************* 1108 1109By default, the difference between values taken on by a variable in loops 1110such as \spad{for i in n..m repeat ...} is \mathOrSpad{1}. 1111It is possible to supply another, possibly negative, step value by using 1112the \spad{by} 1113\spadkey{by} 1114keyword along with \spad{for} and \spad{in}. 1115Like the upper and lower bounds, the step value following the 1116\spad{by} keyword must be an integer. 1117Note that the loop 1118\spad{for i in 1..2 by 0 repeat output(i)} 1119will not terminate by itself, as the step value does not change the index 1120from its initial value of \mathOrSpad{1}. 1121 1122\xtc{ 1123This expression displays the odd integers between two bounds. 1124}{ 1125\spadcommand{for i in 1..5 by 2 repeat output(i)} 1126} 1127\xtc{ 1128Use this to display the numbers in reverse order. 1129}{ 1130\spadcommand{for i in 5..1 by -2 repeat output(i)} 1131} 1132 1133% ********************************************************************* 1134\head{subsection}{for i in n.. repeat}{ugLangLoopsForInN} 1135% ********************************************************************* 1136 1137If the value after the \spadSyntax{..} 1138is omitted, the loop has no end test. 1139A potentially infinite loop is thus created. 1140The variable is given the successive values \spad{n, n+1, n+2, ...} 1141and the loop is terminated only if a \spad{break} or \spad{return} 1142expression is evaluated in the loop body. 1143However you may also add some other modifying clause on the 1144\spad{repeat} (for example, a \spad{while} clause) to stop the loop. 1145 1146\xtc{ 1147This loop displays the integers greater than or equal to \spad{15} 1148and less than the first prime greater than \spad{15}. 1149}{ 1150\spadcommand{for i in 15.. while not prime?(i) repeat output(i)} 1151} 1152 1153% ********************************************************************* 1154\head{subsection}{for x in l repeat}{ugLangLoopsForInXL} 1155% ********************************************************************* 1156 1157Another variant of the \spad{for} loop has the form: 1158\begin{center} 1159{\it \spad{for} x \spad{in} list \spad{repeat} loopBody} 1160\end{center} 1161This form is used when you want to iterate directly over the 1162elements of a list. 1163In this form of the \spad{for} loop, the variable 1164\spad{x} takes on the value of each successive element in \spad{l}. 1165The end test is most simply stated in English: ``are there no more 1166\spad{x} in \spad{l}?'' 1167 1168\xtc{ 1169If \spad{l} is this list, 1170}{ 1171\spadcommand{l := [0,-5,3] \bound{l}} 1172} 1173\xtc{ 1174display all elements of \spad{l}, one per line. 1175}{ 1176\spadcommand{for x in l repeat output(x) \free{l}} 1177} 1178 1179Since the list constructing expression \spad{expand [n..m]} 1180creates the list 1181\spad{[n, n+1, ..., m]}\footnote{This list is empty if \spad{n > m}.}, 1182 you might be tempted to think that the loops 1183\begin{verbatim} 1184for i in n..m repeat output(i) 1185\end{verbatim} 1186and 1187\begin{verbatim} 1188for x in expand [n..m] repeat output(x) 1189\end{verbatim} 1190are equivalent. 1191The second form first creates the list 1192\spad{expand [n..m]} (no matter how large it might be) and 1193then does the iteration. 1194The first form potentially runs in much less space, as the index variable 1195\spad{i} is simply incremented once per loop and the list is not actually 1196created. 1197Using the first form is much more efficient. 1198% 1199\xtc{ 1200Of course, sometimes you really want to iterate across a specific list. 1201This displays each of the factors of \spad{2400000}. 1202}{ 1203\spadcommand{for f in factors(factor(2400000)) repeat output(f)} 1204} 1205 1206% ********************************************************************* 1207\head{subsection}{``Such that'' Predicates}{ugLangLoopsForInPred} 1208% ********************************************************************* 1209 1210A \spad{for} loop can be followed by a \spadSyntax{|} and then a 1211predicate. 1212The predicate qualifies the use of the values from the iterator following 1213the \spad{for}. 1214Think of the vertical bar 1215\spadSyntax{|} as the phrase ``such that.'' 1216\xtc{ 1217This loop expression 1218prints out the integers \spad{n} in the given segment 1219such that \spad{n} is odd. 1220}{ 1221\spadcommand{for n in 0..4 | odd? n repeat output n} 1222} 1223 1224\beginImportant 1225A \spad{for} loop can also be written 1226\begin{center} 1227\spad{for} {\it iterator} \spad{|} {\it predicate} \spad{repeat} {\it loopBody} 1228\end{center} 1229which is equivalent to: 1230\begin{center} 1231\spad{for} {\it iterator} \spad{repeat if} 1232{\it predicate} \spad{then} {\it loopBody} \spad{else} \spad{iterate} 1233\end{center} 1234\endImportant 1235 1236The predicate need not refer only to the variable in the \spad{for} clause: 1237any variable in an outer scope can be part of the predicate. 1238\xtc{ 1239In this example, the predicate on the inner \spad{for} loop uses 1240\spad{i} from the outer loop and the \spad{j} from the \spad{for} 1241\index{iteration!nested} 1242clause that it directly modifies. 1243}{ 1244\begin{spadsrc} 1245for i in 1..50 repeat 1246 for j in 1..50 | factorial(i+j) < 25 repeat 1247 output [i,j] 1248\end{spadsrc} 1249} 1250 1251% ********************************************************************* 1252\head{subsection}{Parallel Iteration}{ugLangLoopsPar} 1253% ********************************************************************* 1254 1255The last example of 1256\texht{the previous section}{\spadref{ugLangLoopsForInPred}} 1257gives an example of 1258\spadgloss{nested iteration}: a loop is contained 1259\index{iteration!nested} 1260in another loop. 1261\index{iteration!parallel} 1262Sometimes you want to iterate across two lists in parallel, or perhaps 1263you want to traverse a list while incrementing a variable. 1264 1265\beginImportant 1266The general syntax of a repeat loop is 1267\begin{center} 1268{\tt \subscriptIt{iterator}{1} \subscriptIt{iterator}{2} \ldots \subscriptIt{iterator}{N} repeat {\it loopBody}} 1269\end{center} 1270where each {\it iterator} is either a \spad{for} or a \spad{while} clause. 1271The loop terminates immediately when the end test of any {\it iterator} 1272succeeds or when a \spad{break} or \spad{return} expression is evaluated 1273in {\it loopBody}. 1274The value returned by the loop is \void{}. 1275\endImportant 1276 1277\xtc{ 1278Here we write a loop to iterate across 1279two lists, computing the sum of the pairwise product 1280of elements. Here is the first list. 1281}{ 1282\spadcommand{l := [1,3,5,7] \bound{l}} 1283} 1284\xtc{ 1285And the second. 1286}{ 1287\spadcommand{m := [100,200] \bound{m}} 1288} 1289\xtc{ 1290The initial value of the sum counter. 1291}{ 1292\spadcommand{sum := 0 \bound{sum}} 1293} 1294\xtc{ 1295The last two elements of \spad{l} are not used in the calculation 1296because \spad{m} has two fewer elements than \spad{l}. 1297}{ 1298\begin{spadsrc}[\bound{doit}\free{sum l m}] 1299for x in l for y in m repeat 1300 sum := sum + x*y 1301\end{spadsrc} 1302} 1303\xtc{ 1304Display the ``dot product.'' 1305}{ 1306\spadcommand{sum \free{doit}} 1307} 1308 1309\xtc{ 1310Next, we write a loop to compute the sum of the products of the loop elements with 1311their positions in the loop. 1312}{ 1313\spadcommand{l := [2,3,5,7,11,13,17,19,23,29,31,37] \bound{l1}} 1314} 1315\xtc{ 1316The initial sum. 1317}{ 1318\spadcommand{sum := 0 \bound{sum1}} 1319} 1320\xtc{ 1321Here looping stops when the list \spad{l} is exhausted, even though 1322the \spad{for i in 0..} specifies no terminating condition. 1323}{ 1324\spadcommand{for i in 0.. for x in l repeat sum := i * x \bound{doit1}\free{sum1 l1}} 1325} 1326\xtc{ 1327Display this weighted sum. 1328}{ 1329\spadcommand{sum \free{doit1}} 1330} 1331 1332When \spadSyntax{|} is used to qualify any of the \spad{for} clauses in a 1333parallel iteration, the variables in the predicates can be from an outer 1334scope or from a \spad{for} clause in or to the left of a modified clause. 1335 1336This is correct: 1337% output from following is too long to show 1338\begin{verbatim} 1339for i in 1..10 repeat 1340 for j in 200..300 | odd? (i+j) repeat 1341 output [i,j] 1342\end{verbatim} 1343This is not correct since the variable \spad{j} has not been 1344defined outside the inner loop. 1345\begin{verbatim} 1346for i in 1..10 | odd? (i+j) repeat -- wrong, j not defined 1347 for j in 200..300 repeat 1348 output [i,j] 1349\end{verbatim} 1350 1351%>% ********************************************************************* 1352%>\head{subsection}{Mixing Loop Modifiers}{ugLangLoopsMix} 1353%>% ********************************************************************* 1354 1355\xtc{ 1356This example shows that it is possible to mix several of the 1357\index{loop!mixing modifiers} 1358forms of \spad{repeat} modifying clauses on a loop. 1359}{ 1360\begin{spadsrc} 1361for i in 1..10 1362 for j in 151..160 | odd? j 1363 while i + j < 160 repeat 1364 output [i,j] 1365\end{spadsrc} 1366} 1367% 1368Here are useful rules for composing loop expressions: 1369\begin{enumerate} 1370\item \spad{while} predicates can only refer to variables that 1371are global (or in an outer scope) 1372or that are defined in \spad{for} clauses to the left of the 1373predicate. 1374\item A ``such that'' predicate (something following \spadSyntax{|}) 1375must directly follow a \spad{for} clause and can only refer to 1376variables that are global (or in an outer scope) 1377or defined in the modified \spad{for} clause 1378or any \spad{for} clause to the left. 1379\end{enumerate} 1380 1381% ********************************************************************* 1382\head{section}{Creating Lists and Streams with Iterators}{ugLangIts} 1383% ********************************************************************* 1384 1385All of what we did for loops in \spadref{ugLangLoops} 1386\index{iteration} 1387can be transformed into expressions that create lists 1388\index{list!created by iterator} 1389and streams. 1390\index{stream!created by iterator} 1391The \spad{repeat,} \spad{break} or \spad{iterate} words are not used but 1392all the other ideas carry over. 1393Before we give you the general rule, here are some examples which 1394give you the idea. 1395 1396\xtc{ 1397This creates a simple list of the integers from \spad{1} to \spad{10}. 1398}{ 1399\spadcommand{mylist := [i for i in 1..10] \bound{mylist}} 1400} 1401\xtc{ 1402Create a stream of the integers greater than or equal to \spad{1}. 1403}{ 1404\spadcommand{mystream := [i for i in 1..] \bound{mystream}} 1405} 1406\xtc{ 1407This is a list of the prime integers between \spad{1} and \spad{10}, 1408inclusive. 1409}{ 1410\spadcommand{[i for i in 1..10 | prime? i]} 1411} 1412\xtc{ 1413This is a stream of the prime integers greater than or equal to \spad{1}. 1414}{ 1415\spadcommand{[i for i in 1.. | prime? i]} 1416} 1417\xtc{ 1418This is a list of the integers between \spad{1} and \spad{10}, 1419inclusive, whose squares are less than \spad{700}. 1420}{ 1421\spadcommand{[i for i in 1..10 while i*i < 700]} 1422} 1423\xtc{ 1424This is a stream of the integers greater than or equal to \spad{1} 1425whose squares are less than \spad{700}. 1426}{ 1427\spadcommand{[i for i in 1.. while i*i < 700]} 1428} 1429 1430Got the idea? 1431Here is the general rule. 1432\index{collection} 1433 1434\beginImportant 1435The general syntax of a collection is 1436\begin{center} 1437{\tt [ {\it collectExpression} \subscriptIt{iterator}{1} \subscriptIt{iterator}{2} \ldots \subscriptIt{iterator}{N} ]} 1438\end{center} 1439where each \subscriptIt{iterator}{i} is either a \spad{for} or a 1440\spad{while} clause. 1441The loop terminates immediately when the end test of any 1442\subscriptIt{iterator}{i} succeeds or when a \spad{return} expression is 1443evaluated in {\it collectExpression}. 1444The value returned by the collection is either a list or a stream of 1445elements, one for each iteration of the {\it collectExpression}. 1446\endImportant 1447 1448Be careful when you use \spad{while} 1449\index{stream!using while @{using {\tt while}}} 1450to create a stream. 1451By default, \Language{} tries to compute and display the first ten elements 1452of a stream. 1453If the \spad{while} condition is not satisfied quickly, \Language{} 1454can spend a long (possibly infinite) time trying to compute 1455\index{stream!number of elements computed} 1456the elements. 1457Use \spadsys{)set streams calculate} to change the default 1458to something else. 1459\syscmdindex{set streams calculate} 1460This also affects the number of terms computed and displayed for power 1461series. 1462For the purposes of this book, we have used this system 1463command to display fewer than ten terms. 1464\xtc{ 1465Use nested iterators to create lists of 1466\index{iteration!nested} 1467lists which can then be given as an argument to \spadfun{matrix}. 1468}{ 1469\spadcommand{matrix [[x^i+j for i in 1..3] for j in 10..12]} 1470} 1471\xtc{ 1472You can also create lists of streams, streams of lists and 1473streams of streams. 1474Here is a stream of streams. 1475}{ 1476\spadcommand{[[i/j for i in j+1..] for j in 1..]} 1477} 1478\xtc{ 1479You can use parallel iteration across lists and streams to create 1480\index{iteration!parallel} 1481new lists. 1482}{ 1483\spadcommand{[i/j for i in 3.. by 10 for j in 2..]} 1484} 1485\xtc{ 1486Iteration stops if the end of a list or stream is reached. 1487}{ 1488\spadcommand{[i^j for i in 1..7 for j in 2.. ]} 1489} 1490%\xtc{ 1491%or a while condition fails. 1492%}{ 1493%\spadcommand{[i^j for i in 1.. for j in 2.. while i + j < 5 ]} 1494%} 1495\xtc{ 1496As with loops, you can combine these modifiers to make very 1497complicated conditions. 1498}{ 1499\spadcommand{[[[i,j] for i in 10..15 | prime? i] for j in 17..22 | j = squareFreePart j]} 1500} 1501 1502See \xmpref{List} and \xmpref{Stream} for more information on creating and 1503manipulating lists and streams, respectively. 1504 1505% ********************************************************************* 1506\head{section}{An Example: Streams of Primes}{ugLangStreamsPrimes} 1507% ********************************************************************* 1508 1509We conclude this chapter with an example of the creation and manipulation 1510of infinite streams of prime integers. 1511This might be useful for experiments with numbers or other applications 1512where you are using sequences of primes over and over again. 1513As for all streams, the stream of primes is only computed as far out as you 1514need. 1515Once computed, however, all the primes up to that point are saved for 1516future reference. 1517 1518Two useful operations provided by the \Language{} library are 1519\spadfunFrom{prime?}{IntegerPrimesPackage} and 1520\spadfunFrom{nextPrime}{IntegerPrimesPackage}. 1521A straight-forward way to create a stream of 1522prime numbers is to start with the stream of positive integers \spad{[2,..]} and 1523filter out those that are prime. 1524\xtc{ 1525Create a stream of primes. 1526}{ 1527\spadcommand{primes : Stream Integer := [i for i in 2.. | prime? i]} 1528} 1529A more elegant way, however, is to use the \spadfunFrom{stream}{Stream} 1530operation from \spadtype{Stream}. 1531Given an initial value \spad{a} and a function \spad{f}, 1532\spadfunFrom{stream}{Stream} 1533constructs the stream \spad{[a, f(a), f(f(a)), ...]}. 1534This function gives you the quickest method of getting the stream of primes. 1535\xtc{ 1536This is how you use 1537\spadfunFrom{stream}{Stream} to 1538generate an infinite stream of primes. 1539}{ 1540\spadcommand{primes := stream(nextPrime,2)} 1541} 1542% decl seems necessary 1543\xtc{ 1544Once the stream is generated, you might only be interested in 1545primes starting at a particular value. 1546}{ 1547\spadcommand{smallPrimes := [p for p in primes | p > 1000] \bound{smallPrimes}} 1548} 1549\xtc{ 1550Here are the first 11 primes greater than 1000. 1551}{ 1552\spadcommand{[p for p in smallPrimes for i in 1..11] \free{smallPrimes}} 1553} 1554\xtc{ 1555Here is a stream of primes between 1000 and 1200. 1556}{ 1557\spadcommand{[p for p in smallPrimes while p < 1200] \free{smallPrimes}} 1558} 1559\xtc{ 1560To get these expanded into a finite stream, 1561you call \spadfunFrom{complete}{Stream} on the stream. 1562}{ 1563\spadcommand{complete \%} 1564} 1565\xtc{ 1566Twin primes are consecutive odd number pairs which are prime. 1567Here is the stream of twin primes. 1568}{ 1569\spadcommand{twinPrimes := [[p,p+2] for p in primes | prime?(p + 2)]} 1570} 1571\xtc{ 1572Since we already have the primes computed we can 1573avoid the call to \spadfunFrom{prime?}{IntegerPrimesPackage} 1574by using a double iteration. 1575This time we'll just generate a stream of the first of the twin primes. 1576}{ 1577\spadcommand{firstOfTwins:= [p for p in primes for q in rest primes | q=p+2]} 1578} 1579 1580Let's try to compute the infinite stream of triplet primes, 1581the set of primes \spad{p} such that \spad{[p,p+2,p+4]} 1582are primes. For example, \spad{[3,5,7]} is a triple prime. 1583We could do this by a triple \spad{for} iteration. 1584A more economical way is to use \userfun{firstOfTwins}. 1585This time however, put a semicolon at the end of the line. 1586 1587\xtc{Create the stream of firstTriplets. 1588Put a semicolon at the end so that no 1589elements are computed. 1590}{ 1591\spadcommand{firstTriplets := [p for p in firstOfTwins for q in rest firstOfTwins | q = p+2];} 1592} 1593 1594What happened? 1595As you know, by default 1596\Language{} displays the first ten 1597elements of a stream when you first display it. 1598And, therefore, it needs to compute them! 1599If you want {\it no} elements computed, just terminate the expression by a 1600semicolon (\spadSyntax{;}).\footnote{ 1601Why does this happen? The semi-colon prevents the display of the 1602result of evaluating the expression. 1603Since no stream elements are needed for display (or anything else, so far), 1604none are computed. 1605} 1606 1607\xtc{ 1608Compute the first triplet prime. 1609}{ 1610\spadcommand{firstTriplets.1} 1611} 1612 1613If you want to compute another, just ask for it. 1614But wait a second! 1615Given three consecutive odd integers, one of them must be divisible 1616by 3. Thus there is only one triplet prime. 1617But suppose that you did not know this and wanted to know what was the 1618tenth triplet prime. 1619\begin{verbatim} 1620firstTriples.10 1621\end{verbatim} 1622To compute the tenth triplet prime, \Language{} first must compute the second, 1623the third, and so on. 1624But since there isn't even a second triplet prime, \Language{} will 1625compute forever. 1626Nonetheless, this effort can produce a useful result. 1627After waiting a bit, hit 1628\texht{\fbox{\bf Ctrl}--\fbox{\bf c}}{{\bf Ctrl-c}}. 1629The system responds as follows. 1630\begin{verbatim} 1631 >> System error: 1632 Console interrupt. 1633 You are being returned to the top level of 1634 the interpreter. 1635\end{verbatim} 1636Let's say that you want to know how many primes have been computed. 1637Issue 1638\begin{verbatim} 1639numberOfComputedEntries primes 1640\end{verbatim} 1641and, for this discussion, let's say that the result is \spad{2045.} 1642\xtc{ 1643How big is the \eth{2045} prime? 1644}{ 1645\spadcommand{primes.2045} 1646} 1647 1648What you have learned is that there are no triplet primes between 5 and 164917837. 1650Although this result is well known (some might even say trivial), there 1651are many experiments you could make where the result is not known. 1652What you see here is a paradigm for testing of hypotheses. 1653Here our hypothesis could have been: ``there is more than one triplet 1654prime.'' 1655We have tested this hypothesis for 17837 cases. 1656With streams, you can let your machine run, interrupt it to see how far 1657it has progressed, 1658then start it up and let it continue from where it left off. 1659 1660%> RDJ note to RSS: 1661%> Expressions not statements or lines-- 1662%> By an expression I mean any syntactically correct program fragment. 1663%> Everything in AXIOM is an expression since every fragment has a value and a type. 1664%> In most languages including LISP, a "statement" is different from an expression: 1665%> it is executed for side-effect only and an error is incurred if you assign it a value. 1666%> This "gimmick" takes care of incomplete expressions such as "if x > 0 then y" in blocks. 1667%> In LISP, "u := (if x > 0 then y)" is illegal but in AXIOM it is legal. 1668%> Also, in AXIOM the value of a repeat loop is void even though you might be 1669%> be able to prove that it always returns a valid value (you have an example of this)! 1670%> This will be considered a bug not a feature. But it is how things stand. 1671%> In any case---this point should be in a box somewhere since it is key 1672%> to a user's understanding to the language. I am not sure where. You only 1673%> gain an appreciation for it after are awhile in chapter 5. 1674