syntax.tex 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. \section{Syntax}
  2. \begin{Command}[semicolon]{;}
  3. The semicolon is a statement delimiter, indicating results are to be printed
  4. when used in interactive mode.
  5. \begin{Examples}
  6. (x+1)**2; & X^{2} + 2*X + 1 \\
  7. df(x**2 + 1,x); & 2*X
  8. \end{Examples}
  9. \begin{Comments}
  10. Entering a \key{Return} without a semicolon or dollar sign results in a
  11. prompt on the following line. A semicolon or dollar sign can be
  12. added at this point to execute the statement. In interactive mode, a
  13. statement that is ended with a semicolon and \key{Return} has its results
  14. printed on the screen.
  15. Inside a group statement \name{<<}\ldots\name{>>}
  16. or a \name{begin}\ldots\name{end} block, a
  17. semicolon or dollar sign separates individual REDUCE statements. Since
  18. results are not printed from a block without a specific \name{return}
  19. statement, there is no difference between using the semicolon or dollar
  20. sign. In a group statement, the last value produced is the value
  21. returned by the group statement. Thus, if a semicolon or dollar sign is
  22. placed between the last statement and the ending brackets, the group
  23. statement returns the value 0 or {\em nil}, rather than the value of the
  24. last statement.
  25. \end{Comments}
  26. \end{Command}
  27. \begin{Command}[dollar]{$}
  28. The dollar sign is a statement delimiter, indicating results are not to be
  29. printed when used in interactive mode.
  30. \begin{Examples}
  31. (x+1)**2$ &
  32. \explanationo{The workspace is set to $x^{2} + 2x + 1$
  33. but nothing shows on the screen} \\
  34. ws; & X^{2} + 2*X + 1
  35. \end{Examples}
  36. \begin{Comments}
  37. Entering a \key{Return} without a semicolon or dollar sign results in a
  38. prompt on the following line. A semicolon or dollar sign can
  39. be added at this point to execute the statement. In interactive mode, a
  40. statement that ends with a dollar sign \name{\$} and a \key{Return} is
  41. executed, but the results not printed.
  42. Inside a \nameref{group} statement \name{<<}\ldots\name{>>}
  43. or a \name{begin}\ldots\name{end} \nameref{block}, a
  44. semicolon or dollar sign separates individual REDUCE statements. Since
  45. results are not printed from a \nameref{block} without a specific
  46. \nameref{return}
  47. statement, there is no difference between using the semicolon or dollar
  48. sign.
  49. In a group statement, the last value produced is the value returned by the
  50. group statement. Thus, if a semicolon or dollar sign is placed between the
  51. last statement and the ending brackets, the group statement returns the
  52. value 0 or {\em nil}, rather than the value of the last statement.
  53. \end{Comments}
  54. \end{Command}
  55. \begin{Command}[percent]{%}
  56. The percent sign is used to precede comments; everything from a percent
  57. to the end of the line is ignored.
  58. \begin{Examples}
  59. df(x**3 + y,x);\% This is a comment \key{Return} & 3*X^{2} \\
  60. int(3*x**2,x) \%This is a comment; \key{Return} \\
  61. \explanation{A prompt is given, waiting for the semicolon that was not
  62. detected in the comment}
  63. \end{Examples}
  64. \begin{Comments}
  65. Statement delimiters \name{;} and \name{\$} are not detected between a
  66. percent sign and the end of the line.
  67. \end{Comments}
  68. \end{Command}
  69. % \begin{Operator}[ampersand]{&}
  70. %
  71. % ***** To be added *****
  72. %
  73. % \end{Operator}
  74. %
  75. %
  76. \begin{Operator}[dot]{.}
  77. \index{list}
  78. The . (dot) infix binary operator adds a new item to the beginning of an
  79. existing \nameref{list}. In high energy physics expressions,
  80. it can also be used
  81. to represent the scalar product of two Lorentz four-vectors.
  82. \begin{Syntax}
  83. \meta{item} \name{.} \meta{list}
  84. \end{Syntax}
  85. \meta{item} can be any REDUCE scalar expression, including a list;
  86. \meta{list} must be a \nameref{list} to avoid producing an error message.
  87. The dot operator is right associative.
  88. \begin{Examples}
  89. liss := a . \{\}; & LISS := \{A\} \\
  90. liss := b . liss; & LISS := \{B,A\} \\
  91. newliss := liss . liss; & NEWLISS := \{\{B,A\},B,A\} \\
  92. firstlis := a . b . \{c\}; & FIRSTLIS := \{A,B,C\} \\
  93. secondlis := x . y . \{z\}; & SECONDLIS := \{X,Y,Z\} \\
  94. for i := 1:3 sum part(firstlis,i)*part(secondlis,i);
  95. & A*X + B*Y + C*Z
  96. \end{Examples}
  97. \end{Operator}
  98. \begin{Operator}[assign]{:=}
  99. \index{assign}
  100. The \name{:=} is the assignment operator, assigning the value on the right-hand
  101. side to the identifier or other valid expression on the left-hand side.
  102. \begin{Syntax}
  103. \meta{restricted\_expression} \name{:=} \meta{expression}
  104. \end{Syntax}
  105. \meta{restricted\_expression} is ordinarily a single identifier, though simple
  106. expressions may be used (see Comments below). \meta{expression} is any
  107. valid REDUCE expression. If \meta{expression} is a \nameref{matrix}
  108. identifier, then
  109. \meta{restricted\_expression} can be a matrix identifier (redimensioned if
  110. necessary) which has each element set to the corresponding elements
  111. of the identifier on the right-hand side.
  112. \begin{Examples}
  113. a := x**2 + 1; & A := X^{2} + 1 \\
  114. a; & X^{2} + 1 \\
  115. first := second := third; & FIRST := SECOND := THIRD \\
  116. first; & THIRD \\
  117. second; & THIRD \\
  118. b := for i := 1:5 product i; & B := 120 \\
  119. b; & 120 \\
  120. w + (c := x + 3) + z; & W + X + Z + 3 \\
  121. c; & X + 3 \\
  122. y + b := c; & Y + B := C \\
  123. y; & - (B - C)
  124. \end{Examples}
  125. \begin{Comments}
  126. The assignment operator is right associative, as shown in the second and
  127. third examples. A string of such assignments has all but the last
  128. item set to the value of the last item. Embedding an assignment statement
  129. in another expression has the side effect of making the assignment, as well
  130. as causing the given replacement in the expression.
  131. Assignments of values to expressions rather than simple identifiers (such as in
  132. the last example above) can also be done, subject to the following remarks:
  133. \begin{itemize}
  134. \item[(i)]
  135. If the left-hand side is an identifier, an operator, or a power, the
  136. substitution rule is added to the rule table.
  137. \item[(ii)]
  138. If the operators \name{- + /} appear on the left-hand side, all but the first
  139. term of the expression is moved to the right-hand side.
  140. \item[(iii)]
  141. If the operator \name{*} appears on the left-hand side, any constant terms are
  142. moved to the right-hand side, but the symbolic factors remain.
  143. \end{itemize}
  144. Assignment is valid for \nameref{array} elements, but not for entire arrays.
  145. The assignment operator can also be used to attach functionality to operators.
  146. A recursive construction such as \name{a := a + b} is allowed, but when
  147. \name{a} is referenced again, the process of resubstitution continues
  148. until the expression stack overflows (you get an error message).
  149. Recursive assignments can be done safely inside controlled loop
  150. expressions, such as \nameref{for}\ldots or \nameref{repeat}\ldots\name{until}.
  151. \end{Comments}
  152. \end{Operator}
  153. \begin{Operator}[equalsign]{=}
  154. The \name{=} operator is a prefix or infix equality comparison operator.
  155. \begin{Syntax}
  156. \name{=}\(\meta{expression}\name{,}\meta{expression}\)
  157. or
  158. \meta{expression} \name{=} \meta{expression}
  159. \end{Syntax}
  160. \meta{expression} can be any REDUCE scalar expression.
  161. \begin{Examples}
  162. a := 4; & A := 4 \\
  163. if =(a,10) then write "yes" else write "no";
  164. & no \\
  165. b := c; & B := C \\
  166. if b = c then write "yes" else write "no";
  167. & yes \\
  168. on rounded; \\
  169. if 4.0 = 4 then write "yes" else write "no";
  170. & yes
  171. \end{Examples}
  172. \begin{Comments}
  173. This logical equality operator can only be used inside a conditional
  174. statement, such as \nameref{if}\ldots\name{then}\ldots\name{else}
  175. or \nameref{repeat}\ldots\name{until}. In other places the equal
  176. sign establishes an algebraic object of type \nameref{equation}.
  177. \end{Comments}
  178. \end{Operator}
  179. \begin{Operator}[replace]{=>}
  180. The \name{=>} operator is a binary operator used in \nameref{rule} lists to
  181. denote replacements.
  182. \begin{Examples}
  183. operator f; \\
  184. let f(x) => x^2; \\
  185. f(x); & x^{2}
  186. \end{Examples}
  187. \end{Operator}
  188. \begin{Operator}[plussign]{+}
  189. The \name{+} operator is a prefix or infix n-ary addition operator.
  190. \begin{Syntax}
  191. \meta{expression} \{ \name{+}\meta{expression}\}\repeated
  192. {\em or} \name{+}\(\meta{expression} \{,\meta{expression}\}\repeated\)
  193. \end{Syntax}
  194. \meta{expression} may be any valid REDUCE expression.
  195. \begin{Examples}
  196. x**4 + 4*x**2 + 17*x + 1; & X^{4} + 4*X^{2} + 17*X + 1 \\
  197. 14 + 15 + x; & X + 29 \\
  198. +(1,2,3,4,5); & 15
  199. \end{Examples}
  200. \begin{Comments}
  201. \name{+} is also valid as an addition operator for \nameref{matrix} variables
  202. that are of the same dimensions and for \nameref{equation}s.
  203. \end{Comments}
  204. \end{Operator}
  205. \begin{Operator}[minussign]{-}
  206. The \name{-} operator is a prefix or infix binary subtraction operator, as well
  207. as the unary minus operator.
  208. \begin{Syntax}
  209. \meta{expression} \name{-} \meta{expression}
  210. or \name{-}\(\meta{expression},\meta{expression}\)
  211. \end{Syntax}
  212. \meta{expression} may be any valid REDUCE expression.
  213. \begin{Examples}
  214. 15 - 4; & 11 \\
  215. x*(-5); & - 5*X \\
  216. a - b - 15; & A - B - 15 \\
  217. -(a,4); & A - 4
  218. \end{Examples}
  219. \begin{Comments}
  220. The subtraction operator is left associative, so that a - b - c is equivalent
  221. to (a - b) - c, as shown in the third example. The subtraction operator is
  222. also valid with \nameref{matrix} expressions of the correct dimensions
  223. and with \nameref{equation}s.
  224. \end{Comments}
  225. \end{Operator}
  226. \begin{Operator}[asterisk]{*}
  227. The \name{*} operator is a prefix or infix n-ary multiplication operator.
  228. \begin{Syntax}
  229. \meta{expression} \{ \name{*} \meta{expression}\}\repeated
  230. or \name{*}\(\meta{expression} \{,\meta{expression}\}\repeated\)
  231. \end{Syntax}
  232. \meta{expression} may be any valid REDUCE expression.
  233. \begin{Examples}
  234. 15*3; & 45 \\
  235. 24*x*yvalue*2; & 48*X*YVALUE \\
  236. *(6,x); & 6*X \\
  237. on rounded; \\
  238. 3*1.5*x*x*x; & 4.5*X^{3} \\
  239. off rounded; \\
  240. 2x**2; & 2*X^{2}
  241. \end{Examples}
  242. \begin{Comments}
  243. REDUCE assumes you are using an implicit multiplication operator when an
  244. identifier is preceded by a number, as shown in the last line above. Since
  245. no valid identifiers can begin with numbers, there is no ambiguity in
  246. making this assumption.
  247. The multiplication operator is also valid with \nameref{matrix} expressions
  248. of the
  249. proper dimensions: matrices \IFTEX{$A$}{A} and \IFTEX{$B$}{B}
  250. can be multiplied if
  251. \IFTEX{$A$}{A} is \IFTEX{$n \times m$}{n x m} and \IFTEX{$B$}{B} is
  252. \IFTEX{$m \times p$}{m x p}. Matrices and \nameref{equation}s can also be
  253. multiplied by scalars: the
  254. result is as if each element was multiplied by the scalar.
  255. \end{Comments}
  256. \end{Operator}
  257. \begin{Operator}[slash]{/}
  258. The \name{/} operator is a prefix or infix binary division operator or
  259. prefix unary \nameref{recip}rocal operator.
  260. \begin{Syntax}
  261. \meta{expression}\name{/}\meta{expression} {\em or}
  262. \name{/}\meta{expression}
  263. or \name{/}\(\meta{expression},\meta{expression}\)
  264. \end{Syntax}
  265. \meta{expression} may be any valid REDUCE expression.
  266. \begin{Examples}
  267. 20/5; & 4 \\
  268. 100/6; & \rfrac{50}{3} \\
  269. 16/2/x; & \rfrac{8}{X} \\
  270. /b; & \rfrac{1}{B} \\
  271. /(y,5); & \rfrac{Y}{5} \\
  272. on rounded; \\
  273. 35/4; & 8.75 \\
  274. /20; & 0.05
  275. \end{Examples}
  276. \begin{Comments}
  277. The division operator is left associative, so that \name{a/b/c} is equivalent
  278. to \name{(a/b)/c}. The division operator is also valid with square
  279. \nameref{matrix} expressions of the same dimensions: With \IFTEX{$A$}{A} and
  280. \IFTEX{$B$}{B} both \IFTEX{$n \times n$}{n x n} matrices and \IFTEX{$B$}{B}
  281. invertible, \IFTEX{$A/B$}{A/B} is
  282. given by \IFTEX{$A \times B^{-1}$}{A*B^{-1}}.
  283. Division of a matrix by a scalar is defined, with the results being the
  284. division of each element of the matrix by the scalar. Division of a
  285. scalar by a matrix is defined if the matrix is invertible, and has the
  286. effect of multiplying the scalar by the inverse of the matrix. When
  287. \name{/} is used as a reciprocal operator for a matrix, the inverse of
  288. the matrix is returned if it exists.
  289. \end{Comments}
  290. \end{Operator}
  291. \begin{Operator}[power]{**}
  292. The \name{**} operator is a prefix or infix binary exponentiation operator.
  293. \begin{Syntax}
  294. \meta{expression} \name{**}\meta{expression}
  295. or \name{**}\(\meta{expression},\meta{expression}\)
  296. \end{Syntax}
  297. \meta{expression} may be any valid REDUCE expression.
  298. \begin{Examples}
  299. x**15; & X^{15} \\
  300. x**y**z; & X^{Y*Z} \\
  301. x**(y**z); & X^{Y^{Z}} \\
  302. **(y,4); & Y^{4} \\
  303. on rounded; \\
  304. 2**pi; & 8.82497782708
  305. \end{Examples}
  306. \begin{Comments}
  307. The exponentiation operator is left associative, so that \name{a**b**c} is
  308. equivalent to \name{(a**b)**c}, as shown in the second example. Note
  309. that this is {\em not} \name{a**(b**c)}, which would be right associative.
  310. When \nameref{nat} is on (the default), REDUCE output produces raised
  311. exponents, as shown. The symbol \name{^}, which is the upper-case 6 on
  312. most keyboards, may be used in the place of \name{**}.
  313. A square \nameref{matrix} may also be raised to positive and negative powers
  314. with the exponentiation operator (negative powers require the matrix to be
  315. invertible). Scalar expressions and \nameref{equation}s may be raised to
  316. fractional and floating-point powers.
  317. \end{Comments}
  318. \end{Operator}
  319. \begin{Operator}[caret]{^}
  320. The \name{^} operator is a prefix or infix binary exponentiation operator.
  321. It is equivalent to \nameref{power} or **.
  322. \begin{Syntax}
  323. \meta{expression} \name{^}\meta{expression}
  324. or \name{^}\(\meta{expression},\meta{expression}\)
  325. \end{Syntax}
  326. \meta{expression} may be any valid REDUCE expression.
  327. \begin{Examples}
  328. x^15; & X^{15} \\
  329. x^y^z; & X^{Y*Z} \\
  330. x^(y^z); & X^{Y^{Z}} \\
  331. ^(y,4); & Y^{4} \\
  332. on rounded; \\
  333. 2^pi; & 8.82497782708
  334. \end{Examples}
  335. \begin{Comments}
  336. The exponentiation operator is left associative, so that \name{a^b^c} is
  337. equivalent to \name{(a^b)^c}, as shown in the second example. Note
  338. that this is \meta{not} \name{a^(b^c)}, which would be right associative.
  339. When \nameref{nat} is on (the default), REDUCE output produces raised
  340. exponents, as shown.
  341. A square \nameref{matrix} may also be raised to positive
  342. and negative powers with
  343. the exponentiation operator (negative powers require the matrix to be
  344. invertible). Scalar expressions and \nameref{equation}s
  345. may be raised to fractional and floating-point powers.
  346. \end{Comments}
  347. \end{Operator}
  348. \begin{Operator}[geqsign]{>=}
  349. \name{>=} is an infix binary comparison operator, which returns {\em true} if
  350. its first argument is greater than or equal to its second argument.
  351. \begin{Syntax}
  352. \meta{expression} \name{>=} \meta{expression}
  353. \end{Syntax}
  354. \meta{expression} must evaluate to an integer or floating-point number.
  355. \begin{Examples}
  356. if (3 >= 2) then yes; & yes \\
  357. a := 15; & A := 15 \\
  358. if a >= 20 then big else small;
  359. & small \\
  360. \end{Examples}
  361. \begin{Comments}
  362. The binary comparison operators can only be used for comparisons between
  363. numbers or variables that evaluate to numbers. The truth values returned
  364. by such a comparison can only be used inside programming constructs, such
  365. as \nameref{if}\ldots\name{then}\ldots\name{else}
  366. or \nameref{repeat}\ldots\name{until} or \nameref{while}\ldots\name{do}.
  367. \end{Comments}
  368. \end{Operator}
  369. \begin{Operator}[greater]{>}
  370. The \name{>} is an infix binary comparison operator that returns
  371. {\em true} if its first argument is strictly greater than its second.
  372. \begin{Syntax}
  373. \meta{expression} \name{>} \meta{expression}
  374. \end{Syntax}
  375. \meta{expression} must evaluate to a number, e.g., integer, rational or
  376. floating point number.
  377. \begin{Examples}
  378. on rounded; \\
  379. if 3.0 > 3 then write "different" else write "same"; & same \\
  380. off rounded; \\
  381. a := 20; & A := 20 \\
  382. if a > 20 then write "bigger" else write "not bigger"; & not bigger \\
  383. \end{Examples}
  384. \begin{Comments}
  385. The binary comparison operators can only be used for comparisons between
  386. numbers or variables that evaluate to numbers. The truth values returned
  387. by such a comparison can only be used inside programming constructs, such
  388. as \nameref{if}\ldots\name{then}\ldots\name{else} or
  389. \nameref{repeat}\ldots\name{until} or \nameref{while}\ldots\name{do}.
  390. \end{Comments}
  391. \end{Operator}
  392. \begin{Operator}[leqsign]{<=}
  393. \name{<=} is an infix binary comparison operator that returns
  394. {\em true} if its first argument is less than or equal to its second argument.
  395. \begin{Syntax}
  396. \meta{expression} \name{<=} \meta{expression}
  397. \end{Syntax}
  398. \meta{expression} must evaluate to a number, e.g., integer, rational or
  399. floating point number.
  400. \begin{Examples}
  401. a := 10; & A := 10 \\
  402. if a <= 10 then true; & true
  403. \end{Examples}
  404. \begin{Comments}
  405. The binary comparison operators can only be used for comparisons between
  406. numbers or variables that evaluate to numbers. The truth values returned
  407. by such a comparison can only be used inside programming constructs, such
  408. as \nameref{if}\ldots\name{then}\ldots\name{else} or
  409. \nameref{repeat}\ldots\name{until} or \nameref{while}\ldots\name{do}.
  410. \end{Comments}
  411. \end{Operator}
  412. \begin{Operator}[less]{<}
  413. \name{<} is an infix binary logical comparison operator that
  414. returns {\em true} if its first argument is strictly less than its second
  415. argument.
  416. \begin{Syntax}
  417. \meta{expression} \name{<} \meta{expression}
  418. \end{Syntax}
  419. \meta{expression} must evaluate to a number, e.g., integer, rational or
  420. floating point number.
  421. \begin{Examples}
  422. f := -3; & F := -3 \\
  423. if f < -3 then write "yes" else write "no"; & no
  424. \end{Examples}
  425. \begin{Comments}
  426. The binary comparison operators can only be used for comparisons between
  427. numbers or variables that evaluate to numbers. The truth values returned
  428. by such a comparison can only be used inside programming constructs, such
  429. as \nameref{if}\ldots\name{then}\ldots\name{else}
  430. or \nameref{repeat}\ldots\name{until} or
  431. \nameref{while}\ldots\name{do}.
  432. \end{Comments}
  433. \end{Operator}
  434. \begin{Operator}[tilde]{~}
  435. The \name{~} is used as a unary prefix operator in the left-hand
  436. sides of \nameref{rule}s to mark \nameref{free variable}s. A double tilde
  437. marks an optional \nameref{free variable}.
  438. \end{Operator}
  439. \begin{Command}[group]{<<}
  440. The \name{<<}\ldots\name{>>} command is a group statement,
  441. used to group statements
  442. together where REDUCE expects a single statement.
  443. %%%INCONSISTENT??? \name{or}
  444. \begin{Syntax}
  445. \name{<<}\meta{statement}\{; \meta{statement} \name{or}
  446. \$\meta{statement}\}\optional \name{>>}
  447. \end{Syntax}
  448. \meta{statement} may be any valid REDUCE statement or expression.
  449. \begin{Examples}
  450. a := 2; & A := 2 \\
  451. if a < 5 then <<b := a + 10; write b>>; & 12 \\
  452. <<d := c/15; f := d + 3; f**2>>;
  453. & \rfrac{C^{2} + 90*C + 202}{225}
  454. \end{Examples}
  455. \begin{Comments}
  456. The value returned from a group statement is the value of the last
  457. individual statement executed inside it. Note that when a semicolon is
  458. placed between the last statement and the closing brackets, 0 or
  459. {\em nil} is returned. Group statements are often used in the
  460. consequence portions of \nameref{if}\ldots\name{then},
  461. \nameref{repeat}\ldots\name{until}, and
  462. \nameref{while}\ldots\name{do}
  463. clauses. They may also be used in interactive
  464. operation to execute several statements at one time. Statements inside
  465. the group statement are separated by semicolons or dollar signs.
  466. \end{Comments}
  467. \end{Command}
  468. \begin{Operator}{AND}
  469. The \name{and} binary logical operator returns {\em true} if both of its
  470. arguments are {\em true}.
  471. \begin{Syntax}
  472. \meta{logical\_expression} \name{and} \meta{logical\_expression}
  473. \end{Syntax}
  474. \meta{logical\_expression} must evaluate to {\em true} or {\em nil}.
  475. \begin{Examples}
  476. a := 12; & A := 12 \\
  477. if numberp a and a < 15 then write a**2 else write "no";
  478. & 144 \\
  479. clear a; \\
  480. if numberp a and a < 15 then write a**2 else write "no";
  481. & no
  482. \end{Examples}
  483. \begin{Comments}
  484. Logical operators can only be used inside conditional statements, such as
  485. \nameref{while}\ldots\name{do} or
  486. \nameref{if}\ldots\name{then}\ldots\name{else}. \name{and} examines each of
  487. its arguments in order, and quits, returning {\em nil}, on finding an
  488. argument that is not {\em true}. An error results if it is used in other
  489. contexts.
  490. \name{and} is left associative: \name{x and y and z} is equivalent to
  491. \name{(x and y) and z}.
  492. \end{Comments}
  493. \end{Operator}
  494. \begin{Command}{BEGIN}
  495. \name{begin} is used to start a \nameref{block} statement, which is closed with
  496. \name{end}.
  497. \begin{Syntax}
  498. \name{begin} \meta{statement}\{\name{;} \meta{statement}\}\optional \ \name{end}
  499. \end{Syntax}
  500. \meta{statement} is any valid REDUCE statement.
  501. \begin{Examples}
  502. begin for i := 1:3 do write i end; &
  503. \begin{multilineoutput}{1cm}
  504. 1
  505. 2
  506. 3
  507. \end{multilineoutput} \\
  508. begin scalar n;n:=1;b:=for i:=1:4 product(x-i);return n end;
  509. & 1 \\
  510. b; &
  511. X^{4} - 10*X^{3} + 35*X^{2} - 50*X + 24
  512. \end{Examples}
  513. \begin{Comments}
  514. A \name{begin}\ldots\name{end} block can do actions (such as \name{write}), but
  515. does not
  516. return a value unless instructed to by a \nameref{return} statement, which must
  517. be the last statement executed in the block. It is unnecessary to insert
  518. a semicolon before the \name{end}.
  519. Local variables, if any, are declared in the first statement immediately
  520. after \name{begin}, and may be defined as \name{scalar, integer,} or
  521. \name{real}. \nameref{array} variables declared
  522. within a \name{begin}\ldots\name{end} block
  523. are global in every case, and \nameref{let} statements have global
  524. effects. A \nameref{let} statement involving a formal parameter affects
  525. the calling parameter that corresponds to it. \nameref{let} statements
  526. involving local variables make global assignments, overwriting outside
  527. variables by the same name or creating them if they do not exist. You
  528. can use this feature to affect global variables from procedures, but be
  529. careful that you do not do it inadvertently.
  530. \end{Comments}
  531. \end{Command}
  532. \begin{Command}{block}
  533. A \name{block} is a sequence of statements enclosed by
  534. commands \nameref{begin} and \nameref{end}.
  535. \begin{Syntax}
  536. \name{begin} \meta{statement}\{\name{;} \meta{statement}\}\optional \ \name{end}
  537. \end{Syntax}
  538. For more details see \nameref{begin}.
  539. \end{Command}
  540. \begin{Command}{COMMENT}
  541. Beginning with the word \name{comment}, all text until the next statement
  542. terminator (\name{;} or \name{\$}) is ignored.
  543. \begin{Examples}
  544. x := a**2 comment--a is the velocity of the particle;;
  545. & X := A^{2}
  546. \end{Examples}
  547. \begin{Comments}
  548. Note that the first semicolon ends the comment and the second one
  549. terminates the original REDUCE statement.
  550. Multiple-line comments are often needed in interactive files. The
  551. \name{comment} command allows a normal-looking text to accompany the
  552. REDUCE statements in the file.
  553. \end{Comments}
  554. \end{Command}
  555. \begin{Operator}{CONS}
  556. The \name{cons} operator adds a new element to the beginning of a
  557. \nameref{list}. Its
  558. operation is identical to the symbol \nameref{dot} (dot). It can be used
  559. infix or prefix.
  560. \begin{Syntax}
  561. \name{cons}\(\meta{item},\meta{list}\) or \meta{item} \name{cons} \meta{list}
  562. \end{Syntax}
  563. \meta{item} can be any REDUCE scalar expression, including a list; \meta{list}
  564. must be a list.
  565. \begin{Examples}
  566. liss := cons(a,{b}); & \{A,B\} \\
  567. liss := c cons liss; & \{C,A,B\} \\
  568. newliss := for each y in liss collect cons(y,list x);
  569. & NEWLISS := \{\{C,X\},\{A,X\},\{B,X\}\} \\
  570. for each y in newliss sum (first y)*(second y);
  571. & X*(A + B + C)
  572. \end{Examples}
  573. \begin{Comments}
  574. If you want to use \name{cons} to put together two elements into a new list,
  575. you must make the second one into a list with curly brackets or the \name{list}
  576. command. You can also start with an empty list created by \name{\{\}}.
  577. The \name{cons} operator is right associative: \name{a cons b cons c} is valid
  578. if \name{c} is a list; \name{b} need not be a list. The list produced is
  579. \name{\{a,b,c\}}.
  580. \end{Comments}
  581. \end{Operator}
  582. \begin{Command}{END}
  583. The command \name{end} has two main uses:
  584. \begin{itemize}
  585. \item[(i)]
  586. as the ending of a \nameref{begin}\ldots\name{end} \nameref{block}; and
  587. \item[(ii)]
  588. to end input from a file.
  589. \end{itemize}
  590. \begin{Comments}
  591. In a \name{begin}\ldots\name{end} \nameref{block}, there need not be a delimiter
  592. (\name{;} or \name{\$}) before the \name{end}, though there must be one
  593. after it, or a right bracket matching an earlier left bracket.
  594. Files to be read into REDUCE should end with \name{end;}, which must be
  595. preceded by a semicolon (usually the last character of the previous line).
  596. The additional semicolon avoids problems with mistakes in the files. If
  597. you have suspended file operation by answering \name{n} to a \name{pause}
  598. command, you are still, technically speaking, ``in" the file. Use
  599. \name{end} to exit the file.
  600. An \name{end} at the top level of a program is ignored.
  601. \end{Comments}
  602. \end{Command}
  603. \begin{Type}{EQUATION}
  604. \index{equation}\index{equal}\index{arithmetic}
  605. An \name{equation} is an expression where two algebraic expressions
  606. are connected by the (infix) operator \nameref{equal} or by \nameindex{=}.
  607. For access to the components of an \name{equation} the operators
  608. \nameref{lhs}, \nameref{rhs} or \nameref{part} can be used. The
  609. evaluation of the left-hand side of an \name{equation} is controlled
  610. by the switch \nameref{evallhseqp}, while the right-hand side is
  611. evaluated unconditionally. When an \name{equation} is part of a
  612. logical expression, e.g. in a \nameref{if} or \nameref{while} statement,
  613. the equation is evaluated by subtracting both sides can comparing
  614. the result with zero.
  615. Equations occur in many contexts, e.g. as arguments of the \nameref{sub}
  616. operator and in the arguments and the results
  617. of the operator \nameref{solve}. An equation can be member of a \nameref{list}
  618. and you may assign an equation to a variable. Elementary arithmetic is supported
  619. for equations: if \nameref{evallhseqp} is on, you may add and subtract
  620. equations, and you can combine an equation with a scalar expression by
  621. addition, subtraction, multiplication, division and raise an equation
  622. to a power.
  623. \begin{Examples}
  624. on evallhseqp;\\
  625. u:=x+y=1$\\
  626. v:=2x-y=0$\\
  627. 2*u-v; & - 3*y=-2\\
  628. ws/3; & y=\rfrac{2}{3}\\
  629. \end{Examples}
  630. Important: the equation must occur in the leftmost term of such an expression.
  631. For other operations, e.g. taking function values of both sides, use the
  632. \nameref{map} operator.
  633. \end{Type}
  634. \begin{Operator}{FIRST}
  635. \index{list}\index{decomposition}
  636. The \name{first} operator returns the first element of a \nameref{list}.
  637. \begin{Syntax}
  638. \name{first}\(\meta{list}\) or \name{first} \meta{list}
  639. \end{Syntax}
  640. \meta{list} must be a non-empty list to avoid an error message.
  641. \begin{Examples}
  642. alist := \{a,b,c,d\}; & ALIST := \{A,B,C,D\} \\
  643. first alist; & A \\
  644. blist := \{x,y,\{ww,aa,qq\},z\}; & BLIST := \{X,Y,\{WW,AA,QQ\},Z\} \\
  645. first third blist; & WW
  646. \end{Examples}
  647. \end{Operator}
  648. \begin{Command}{FOR}
  649. \index{loop}
  650. The \name{for} command is used for iterative loops. There are many
  651. possible forms it can take.
  652. \begin{INFO}
  653. {
  654. \begin{verbatim}
  655. / \
  656. / |STEP <number> UNTIL| \
  657. |<var>:=<number>| |<number>|
  658. FOR| | : | |<action> <exprn>
  659. | \ / |
  660. |EACH <var> IN <list> |
  661. \ /
  662. where <action> ::= DO|PRODUCT|SUM|COLLECT|JOIN.
  663. \end{verbatim}
  664. }
  665. \end{INFO}
  666. \begin{TEX}
  667. \begin{Syntax}
  668. \name{for}
  669. \begin{alternative}
  670. \meta{var} \name{:=} \meta{start} \name{:} \meta{stop}\\
  671. \meta{var} \name{:=} \meta{start} \name{step} \meta{inc}
  672. \name{until} \meta{stop}\\
  673. \name{each} \meta{var} \name{in} \meta{list}
  674. \end{alternative}
  675. \begin{alternative}
  676. \name{collect}\\
  677. \name{do}\\
  678. \name{join}\\
  679. \name{product}\\
  680. \name{sum}
  681. \end{alternative}
  682. \meta{expression}
  683. \end{Syntax}
  684. \end{TEX}
  685. \meta{var} can be any valid REDUCE identifier except \name{t} or
  686. \name{nil}, \meta{inc}, \meta{start} and \meta{stop} can be any expression
  687. that evaluates to a positive or negative integer. \meta{list} must be a
  688. valid \nameref{list} structure.
  689. The action taken must be one of the actions shown
  690. above, each of which is followed by a single REDUCE expression, statement
  691. or a \nameref{group} (\name{<<}\ldots\name{>>}) or \nameref{block}
  692. (\nameref{begin}\ldots\nameref{end}) statement.
  693. \begin{Examples}
  694. for i := 1:10 sum i;
  695. & 55 \\
  696. for a := -2 step 3 until 6 product a;
  697. & -8 \\
  698. a := 3; & A := 3 \\
  699. for iter := 4:a do write iter; \\
  700. m := 0; & M := 0 \\
  701. for s := 10 step -1 until 3 do <<d := 10*s;m := m + d>>; \\
  702. m; & 520 \\
  703. for each x in {q,r,s} sum x**2; & Q^{2} + R^{2} + S^{2} \\
  704. for i := 1:4 collect 1/i;
  705. & \{1,\rfrac{1}{2},\rfrac{1}{3},\rfrac{1}{4}\} \\
  706. for i := 1:3 join list solve(x**2 + i*x + 1,x);
  707. & \begin{multilineoutput}{5cm}
  708. \{\{X= -\rfrac{SQRT(3)*I + 1}{2},
  709. X= -\rfrac{SQRT(3)*I - 1}{2}\}
  710. \{X=-1\},
  711. \{X= - \rfrac{SQRT(5) + 3}{2},X=\rfrac{SQRT(5) - 3}{2}\}\}
  712. \end{multilineoutput}
  713. \end{Examples}
  714. \begin{Comments}
  715. The behavior of each of the five action words follows:
  716. \begin{TEX}
  717. \begin{center}
  718. \begin{tabular}{|l|p{5cm}|p{5cm}|}
  719. \hline
  720. \multicolumn{3}{|c|}{Action Word Behavior}\\
  721. \hline
  722. \multicolumn{1}{|c|}{Keyword} &
  723. \multicolumn{1}{c|}{Argument Type} & \multicolumn{1}{c|}{Action} \\
  724. \hline
  725. do & statement, command, group or block &
  726. Evaluates its argument once for each iteration of the loop, not saving
  727. results \\
  728. collect & expression, statement, command, group, block, list &
  729. Evaluates its argument once for each iteration of the loop, storing the results
  730. in a list which is returned by the \verb|for| statement when done \\
  731. join & list or an operator which produces a list &
  732. Evaluates its argument once for each iteration of the loop, appending the
  733. elements in each individual result list onto the overall result list \\
  734. product & expression, statement, command,
  735. \nameref{group} or \nameref{block} &
  736. Evaluates its argument once for each iteration of the loop, multiplying the
  737. results together and returning the overall product \\
  738. sum & expression, statement, command, group or block &
  739. Evaluates its argument once for each iteration of the loop, adding the results
  740. together and returning the overall sum\\
  741. \hline
  742. \end{tabular}
  743. \end{center}
  744. \end{TEX}
  745. \begin{INFO}
  746. {\begin{verbatim}
  747. Action Word Behavior
  748. Keyword Argument Type Action
  749. do statement, command, group Evaluates its argument once
  750. or block for each iteration of the loop,
  751. not saving results
  752. collect expression, statement, Evaluates its argument once for
  753. command, group, block, list each iteration of the loop,
  754. storing the results in a list
  755. which is returned by the for
  756. statement when done
  757. join list or an operator which Evaluates its argument once for
  758. produces a list each iteration of the loop,
  759. appending the elements in each
  760. individual result list onto the
  761. overall result list
  762. product expression, statement, Evaluates its argument once for
  763. command, group or block each iteration of the loop,
  764. multiplying the results together
  765. and returning the overall product
  766. sum expression, statement, Evaluates its argument once for
  767. command, group or block each iteration of the loop,
  768. adding the results together and
  769. returning the overall sum
  770. \end{verbatim} }
  771. \end{INFO}
  772. For number-driven \name{for} statements, if the ending limit is smaller
  773. than the beginning limit (larger in the case of negative steps) the action
  774. statement is not executed at all. The iterative variable is local to the
  775. \name{for} statement, and does not affect the value of an identifier with
  776. the same name. For list-driven \name{for} statements, if the list is
  777. empty, the action statement is not executed, but no error occurs.
  778. You can use nested \name{for} statements, with the inner \name{for}
  779. statement after the action keyword. You must make sure that your inner
  780. statement returns an expression that the outer statement can handle.
  781. \end{Comments}
  782. \end{Command}
  783. \begin{Command}{FOREACH}
  784. \index{loop}
  785. \name{foreach} is a synonym for the \name{for each} variant of the
  786. \nameref{for} construct. It is designed to iterate down a list, and an
  787. error will occur if a list is not used. The use of \name{for each} is
  788. preferred to \name{foreach}.
  789. \begin{Syntax}
  790. \name{foreach} \meta{variable} in \meta{list} \meta{action} \meta{expression} \\
  791. where \meta{action} ::= \name{do | product | sum | collect | join}
  792. \end{Syntax}
  793. \begin{Examples}
  794. foreach x in {q,r,s} sum x**2; & Q^{2} + R^{2} + S^{2}
  795. \end{Examples}
  796. \end{Command}
  797. \begin{Operator}{GEQ}
  798. The \name{geq} operator is a binary infix or prefix logical operator. It
  799. returns true if its first argument is greater than or equal to its second
  800. argument. As an infix operator it is identical with \name{>=}.
  801. \begin{Syntax}
  802. \name{geq}\(\meta{expression},\meta{expression}\) or \meta{expression}
  803. \name{geq} \meta{expression}
  804. \end{Syntax}
  805. \meta{expression} can be any valid REDUCE expression that evaluates to a
  806. number.
  807. \begin{Examples}
  808. a := 20; & A := 20 \\
  809. if geq(a,25) then write "big" else write "small";
  810. & small \\
  811. if a geq 20 then write "big" else write "small";
  812. & big \\
  813. if (a geq 18) then write "big" else write "small";
  814. & big
  815. \end{Examples}
  816. \begin{Comments}
  817. Logical operators can only be used in conditional statements such as \\
  818. \nameref{if}\ldots\name{then}\ldots\name{else} or
  819. \nameref{repeat}\ldots\name{until}.
  820. \end{Comments}
  821. \end{Operator}
  822. \begin{Command}{GOTO}
  823. Inside a \name{begin}\ldots\name{end} \nameref{block}, \name{goto}, or
  824. preferably, \name{go to}, transfers flow of control to a labeled statement.
  825. \begin{Syntax}
  826. \name{go to} \meta{labeled_statement} or \name{goto} \meta{labeled_statement}
  827. \end{Syntax}
  828. \meta{labeled_statement} is of the form \meta{label} \name{:}\meta{statement}
  829. \begin{Examples}
  830. \begin{multilineinput}
  831. procedure dumb(a);
  832. begin scalar q;
  833. go to lab;
  834. q := df(a**2 - sin(a),a);
  835. write q;
  836. lab: return a
  837. end;
  838. \end{multilineinput} & DUMB \\
  839. dumb(17); & 17
  840. \end{Examples}
  841. \begin{Comments}
  842. \name{go to} can only be used inside a \name{begin}\ldots\name{end}
  843. \nameref{block}, and inside
  844. the block only statements at the top level can be labeled, not ones inside
  845. \name{<<}\ldots\name{>>}, \nameref{while}\ldots\name{do}, etc.
  846. \end{Comments}
  847. \end{Command}
  848. \begin{Operator}{GREATERP}
  849. The \name{greaterp} logical operator returns true if its first argument is
  850. strictly greater than its second argument. As an infix operator it is
  851. identical with \name{>}.
  852. \begin{Syntax}
  853. \name{greaterp}\(\meta{expression},\meta{expression}\) or \meta{expression}
  854. \name{greaterp} \meta{expression}
  855. \end{Syntax}
  856. \meta{expression} can be any valid REDUCE expression that evaluates to a
  857. number.
  858. \begin{Examples}
  859. a := 20; & A := 20 \\
  860. if greaterp(a,25) then write "big" else write "small";
  861. & small \\
  862. if a greaterp 20 then write "big" else write "small";
  863. & small \\
  864. if (a greaterp 18) then write "big" else write "small";
  865. & big
  866. \end{Examples}
  867. \begin{Comments}
  868. Logical operators can only be used in conditional statements such as \\
  869. \nameref{if}\ldots\name{then}\ldots\name{else}
  870. or \nameref{repeat}\ldots\nameref{while}.
  871. \end{Comments}
  872. \end{Operator}
  873. \begin{Command}{IF}
  874. The \name{if} command is a conditional statement that executes a statement
  875. if a condition is true, and optionally another statement if it is not.
  876. \begin{Syntax}
  877. \name{if} \meta{condition} \name{then} \meta{statement}
  878. \ \&option\(\name{else}\ \meta{statement}\)
  879. \end{Syntax}
  880. \meta{condition} must be a logical or comparison operator that evaluates to
  881. a \nameref{boolean value}.
  882. \meta{statement} must be a single REDUCE statement or a
  883. \nameref{group} (\name{<<}\ldots\name{>>}) or
  884. \nameref{block} (\name{begin}\ldots\name{end}) statement.
  885. \begin{Examples}
  886. if x = 5 then a := b+c else a := d+f;
  887. & D + F \\
  888. x := 9; & X := 9 \\
  889. if numberp x and x<20 then y := sqrt(x) else write "illegal";
  890. & 3 \\
  891. clear x; \\
  892. if numberp x and x<20 then y := sqrt(x) else write "illegal";
  893. & illegal \\
  894. x := 12; & X := 12 \\
  895. a := if x < 5 then 100 else 150;
  896. & A := 150 \\
  897. b := u**(if x < 10 then 2);
  898. & B := 1 \\
  899. bb := u**(if x > 10 then 2);
  900. & BB := U^{2}
  901. \end{Examples}
  902. \begin{Comments}
  903. An \name{if} statement may be used inside an assignment statement and sets
  904. its value depending on the conditions, or used anywhere else an
  905. expression would be valid, as shown in the last example. If there is no
  906. \nameindex{else} clause, the value is 0 if a number is expected, and nothing
  907. otherwise.
  908. The \name{else} clause may be left out if no action is to be taken if the
  909. condition is false.
  910. The condition may be a compound conditional statement using \nameref{and} or
  911. \nameref{or}. If a non-conditional statement, such as a constant, is used by
  912. accident, it is assumed to have value {\em true}.
  913. Be sure to use \nameref{group} or \nameref{block} statements after
  914. \nameindex{then} or \name{else}.
  915. The \name{if} operator is right associative. The following constructions are
  916. examples:
  917. \begin{itemize}
  918. \item[(1)]
  919. \begin{Syntax}
  920. \name{if} \meta{condition} \name{then} \name{if} \meta{condition} \name{then}
  921. \meta{action} \name{else} \meta{action}
  922. \end{Syntax}
  923. %\end{itemize}
  924. which is equivalent to
  925. \begin{Syntax}
  926. \name{if} \meta{condition} \name{then} \(\name{if}\ \meta{condition}
  927. \ \name{then}\ \meta{action}\ \name{else}\ \meta{action}\);
  928. \end{Syntax}
  929. %\begin{itemize}
  930. \item[(2)]
  931. \begin{Syntax}
  932. \name{if} \meta{condition} \name{then} \meta{action} \name{else if}
  933. \meta{condition} \name{then} \meta{action} \name{else} \meta{action}
  934. \end{Syntax}
  935. which is equivalent to
  936. \begin{Syntax}
  937. \name{if}\ \meta{condition} \name{then} \meta{action} \name{else} \\
  938. \(\name{if}\ \meta{condition}\ \name{then}\ \meta{action}
  939. \ \name{else}\ \meta{action}\).
  940. \end{Syntax}
  941. \end{itemize}
  942. \end{Comments}
  943. \end{Command}
  944. \begin{Operator}{LIST}
  945. \index{list}
  946. The \name{list} operator constructs a list from its arguments.
  947. \begin{Syntax}
  948. \name{list}\(\meta{item} \{,\meta{item}\}\optional\) or
  949. \name{list}\(\) to construct an empty list.
  950. \end{Syntax}
  951. \meta{item} can be any REDUCE scalar expression, including another list.
  952. Left and right curly brackets can also be used instead of the operator
  953. \name{list} to construct a list.
  954. \begin{Examples}
  955. liss := list(c,b,c,\{xx,yy\},3x**2+7x+3,df(sin(2*x),x));
  956. & LISS := \{C,B,C,\{XX,YY\},3*X^{2} + 7*X + 3,2*COS(2*X)\} \\
  957. length liss; & 6 \\
  958. liss := \{c,b,c,\{xx,yy\},3x**2+7x+3,df(sin(2*x),x)\};
  959. & LISS := \{C,B,C,\{XX,YY\},3*X^{2} + 7*X + 3,2*COS(2*X)\} \\
  960. emptylis := list(); & EMPTYLIS := \{\} \\
  961. a . emptylis; & \{A\}
  962. \end{Examples}
  963. \begin{Comments}
  964. Lists are ordered, hierarchical structures. The elements stay where you
  965. put them, and only change position in the list if you specifically change
  966. them. Lists can have nested sublists to any (reasonable) level. The
  967. \nameref{part} operator can be used to access elements anywhere within a list
  968. hierarchy. The \nameref{length} operator counts the
  969. number of top-level elements
  970. of its list argument; elements that are themselves lists still only
  971. count as one element.
  972. \end{Comments}
  973. \end{Operator}
  974. \begin{Operator}{OR}
  975. The \name{or} binary logical operator returns {\it true} if either one or
  976. both of its arguments is {\it true}.
  977. \begin{Syntax}
  978. \meta{logical expression} \name{or} \meta{logical expression}
  979. \end{Syntax}
  980. \meta{logical expression} must evaluate to {\it true} or {\it nil}.
  981. \begin{Examples}
  982. a := 10; & A := 10 \\
  983. \begin{multilineinput}
  984. if a<0 or a>140 then write "not a valid human age" else
  985. write "age = ",a;
  986. \end{multilineinput} \\
  987. & age = 10 \\
  988. a := 200; & A := 200 \\
  989. if a < 0 or a > 140 then write "not a valid human age";
  990. & not a valid human age
  991. \end{Examples}
  992. \begin{Comments}
  993. The \name{or} operator is left associative: \name{x or y or z} is equivalent to
  994. \name{(x or y)} \name{or z}.
  995. Logical operators can only be used in conditional expressions, such as \\
  996. \nameref{if}\ldots\name{then}\ldots\name{else}
  997. and \nameref{while}\ldots\name{do}.
  998. \name{or} evaluates its arguments in order and quits, returning {\it true},
  999. on finding the first {\it true} statement.
  1000. \end{Comments}
  1001. \end{Operator}
  1002. \begin{Command}{PROCEDURE}
  1003. The \name{procedure} command allows you to define a mathematical operation as a
  1004. function with arguments.
  1005. \begin{Syntax}
  1006. \&\meta{option} \name{procedure} \meta{identifier}
  1007. \(\meta{arg}\{,\meta{arg}\}\repeated\)\name{;}\meta{body}
  1008. \end{Syntax}
  1009. The \meta{option} may be \nameref{algebraic} or \nameref{symbolic},
  1010. indicating the
  1011. mode under which the procedure is executed, or \nameref{real} or
  1012. \nameref{integer}, indicating the type of answer expected. The default is
  1013. algebraic. Real or integer procedures are subtypes of algebraic
  1014. procedures; type-checking is done on the results of integer procedures, but
  1015. not on real procedures (in the current REDUCE release). \meta{identifier}
  1016. may be any valid REDUCE identifier that is not already a procedure name,
  1017. operator, \nameref{array} or \nameref{matrix}.
  1018. \meta{arg} is a formal parameter that may be any
  1019. valid REDUCE identifier. \meta{body} is a single statement (a \nameref{group}
  1020. or \nameref{block} statement may be used) with the desired activities in it.
  1021. \begin{Examples}
  1022. \begin{multilineinput}
  1023. procedure fac(n);
  1024. if not (fixp(n) and n>=0)
  1025. then rederr "Choose nonneg. integer only"
  1026. else for i := 0:n-1 product i+1;
  1027. \end{multilineinput}
  1028. & FAC \\
  1029. fac(0); & 1 \\
  1030. fac(5); & 120 \\
  1031. fac(-5); & ***** choose nonneg. integer only
  1032. \end{Examples}
  1033. \begin{Comments}
  1034. Procedures are automatically declared as operators upon definition. When
  1035. REDUCE has parsed the procedure definition and successfully converted it to
  1036. a form for its own use, it prints the name of the procedure. Procedure
  1037. definitions cannot be nested. Procedures can call other procedures, or can
  1038. recursively call themselves. Procedure identifiers can be cleared as you
  1039. would clear an operator. Unlike \nameref{let} statements, new definitions
  1040. under the same procedure name replace the previous definitions completely.
  1041. Be careful not to use the name of a system operator for your own procedure.
  1042. REDUCE may or may not give you a warning message. If you redefine a system
  1043. operator in your own procedure, the original function of the system operator
  1044. is lost for the remainder of the REDUCE session.
  1045. Procedures may have none, one, or more than one parameter. A REDUCE
  1046. parameter is a formal parameter only; the use of {\it x} as a parameter in
  1047. a \name{procedure} definition has no connection with a value of {\it x} in
  1048. the REDUCE session, and the results of calling a procedure have no effect
  1049. on the value of {\it x}. If a procedure is {\it called} with {\it x} as a
  1050. parameter, the current value of {\it x} is used as specified in the
  1051. computation, but is not changed outside the procedure.
  1052. Making an assignment statement by \name{:=} with a
  1053. formal parameter on the left-hand side only changes the value of the
  1054. calling parameter within the procedure.
  1055. Using a \nameref{let} statement inside a procedure always changes the value
  1056. globally: a \name{let} with a formal parameter makes the change to the calling
  1057. parameter. \name{let} statements cannot be made on local variables inside
  1058. \nameref{begin}\ldots\name{end} \nameref{block}\name{s}.
  1059. When \nameref{clear} statements are used on formal
  1060. parameters, the calling variables associated with them are cleared globally too.
  1061. The use of \name{let} or \name{clear} statements inside procedures
  1062. should be done with extreme caution.
  1063. Arrays and operators may be used as parameters to procedures. The body of the
  1064. procedure can contain statements that appropriately manipulate these
  1065. arguments. Changes are made to values of the calling arrays or operators.
  1066. Simple expressions can also be used as arguments, in the place of scalar
  1067. variables. Matrices may {\it not} be used as arguments to procedures.
  1068. A procedure that has no parameters is called by the procedure name,
  1069. immediately followed by empty parentheses. The empty parentheses may be left
  1070. out when writing a procedure with no parameters, but must appear in a call of
  1071. the procedure. If this is a nuisance to you, use a \nameref{let} statement on
  1072. the name of the procedure (i.e., \name{let noargs = noargs()}) after which
  1073. you can call the procedure by just its name.
  1074. Procedures that have a single argument can leave out the parentheses around
  1075. it both in the definition and procedure call. (You can use the parentheses if
  1076. you wish.) Procedures with more than one argument must use parentheses, with
  1077. the arguments separated by commas.
  1078. Procedures often have a \name{begin}\ldots\name{end} block in them. Inside the
  1079. block, local variables are declared using \name{scalar}, \name{real} or
  1080. \name{integer} declarations.
  1081. The declarations must be made immediately after the word
  1082. \name{begin}, and if more than one type of declaration is made, they are
  1083. separated by semicolons. REDUCE currently does no type checking on local
  1084. variables; \name{real} and \name{integer} are treated just like \name{scalar}.
  1085. Actions take place as specified in the statements inside the block statement.
  1086. Any identifiers that are not formal parameters or local variables are treated
  1087. as global variables, and activities involving these identifiers are global in
  1088. effect.
  1089. If a return value is desired from a procedure call, a specific
  1090. \nameref{return} command must be the last statement executed before exiting
  1091. from the procedure. If no \name{return} is used, a procedure returns a
  1092. zero or no value.
  1093. Procedures are often written in a file using an editor, then the file
  1094. is input using the command \nameref{in}. This method allows easy changes in
  1095. development, and also allows you to load the named procedures whenever
  1096. you like, by loading the files that contain them.
  1097. \end{Comments}
  1098. \end{Command}
  1099. \begin{Command}{REPEAT}
  1100. \index{loop}
  1101. The \nameref{repeat} command causes repeated execution of a statement
  1102. \nameindex{until}
  1103. the given condition is found to be true. The statement is always executed
  1104. at least once.
  1105. \begin{Syntax}
  1106. \name{repeat} \meta{statement} \name{until} \meta{condition}
  1107. \end{Syntax}
  1108. \meta{statement} can be a single statement, \nameref{group} statement, or
  1109. a \name{begin}\ldots\name{end} \nameref{block}. \meta{condition} must be
  1110. a logical operator that evaluates to {\it true} or {\it nil}.
  1111. \begin{Examples}
  1112. <<m := 4; repeat <<write 100*x*m;m := m-1>> until m = 0>>;
  1113. & \begin{multilineoutput}{6cm}
  1114. 400*X
  1115. 300*X
  1116. 200*X
  1117. 100*X
  1118. \end{multilineoutput}\\
  1119. <<m := -1; repeat <<write m; m := m-1>> until m <= 0>>;
  1120. & -1
  1121. \end{Examples}
  1122. \begin{Comments}
  1123. \name{repeat} must always be followed by an \name{until} with a condition.
  1124. Be careful not to generate an infinite loop with a condition that is never
  1125. true. In the second example, if the condition had been \name{m = 0}, it
  1126. would never have been true since \name{m} already had value -2 when the
  1127. condition was first evaluated.
  1128. \end{Comments}
  1129. \end{Command}
  1130. \begin{Operator}{REST}
  1131. \index{list}\index{decomposition}
  1132. The \name{rest} operator returns a \nameref{list} containing all but the first
  1133. element of the list it is given.
  1134. \begin{Syntax}
  1135. \name{rest}\(\meta{list}\) or \name{rest} \meta{list}
  1136. \end{Syntax}
  1137. \meta{list} must be a non-empty list, but need not have more than one element.
  1138. \begin{Examples}
  1139. alist := {a,b,c,d}; & ALIST := \{A,B,C,D\}; \\
  1140. rest alist; & \{B,C,D\} \\
  1141. blist := {x,y,{aa,bb,cc},z}; & BLIST := \{X,Y,\{AA,BB,CC\},Z\} \\
  1142. second rest blist; & \{AA,BB,CC\} \\
  1143. clist := {c}; & CLIST := C \\
  1144. rest clist; & \{\}
  1145. \end{Examples}
  1146. \end{Operator}
  1147. \begin{Command}{RETURN}
  1148. The \name{return} command causes a value to be returned from inside a
  1149. \name{begin}\ldots\name{end} \nameref{block}.
  1150. \begin{TEX}
  1151. \begin{Syntax}
  1152. \name{begin} \meta{statements} \name{return} \meta{\&option(expression)}
  1153. \name{end}
  1154. \end{Syntax}
  1155. \end{TEX}
  1156. \begin{INFO}
  1157. {\begin{Syntax}
  1158. \name{begin} \meta{statements} \name{return} \meta{(expression)}
  1159. \name{end}
  1160. \end{Syntax}
  1161. }\end{INFO}
  1162. \meta{statements} can be any valid REDUCE statements. The value of
  1163. \meta{expression} is returned.
  1164. \begin{Examples}
  1165. begin write "yes"; return a end; &
  1166. \begin{multilineoutput}{5cm}
  1167. yes
  1168. A
  1169. \end{multilineoutput}\\
  1170. \begin{multilineinput}
  1171. procedure dumb(a);
  1172. begin if numberp(a) then return a else return 10 end;
  1173. \end{multilineinput}
  1174. & DUMB \\
  1175. dumb(x); & 10 \\
  1176. dumb(-5); & -5 \\
  1177. \begin{multilineinput}
  1178. procedure dumb2(a);
  1179. begin c := a**2 + 2*a + 1; d := 17; c*d; return end;
  1180. \end{multilineinput} & DUMB2 \\
  1181. dumb2(4); \\
  1182. c; & 25 \\
  1183. d; & 17
  1184. \end{Examples}
  1185. \begin{Comments}
  1186. Note in \name{dumb2} above that the assignments were made as requested, but
  1187. the product \name{c*d} cannot be accessed. Changing the procedure to read
  1188. \name{return c*d} would remedy this problem.
  1189. The \name{return} statement is always the last statement executed before
  1190. leaving the block. If \name{return} has no argument, the block is exited but
  1191. no value is returned. A block statement does not need a \name{return} ;
  1192. the statements inside terminate in their normal fashion without one.
  1193. In that case no value is returned, although the specified actions inside the
  1194. block take place.
  1195. The \name{return} command can be used inside \name{<<}\ldots\name{>>}
  1196. \nameref{group} statements and
  1197. \nameref{if}\ldots\name{then}\ldots\name{else} commands that
  1198. are inside \name{begin}\ldots\name{end} \nameref{block}s.
  1199. It is not valid in these constructions that are not inside
  1200. a \name{begin}\ldots\name{end}
  1201. block. It is not valid inside \nameref{for},
  1202. \nameref{repeat}\ldots\name{until} or \nameref{while}\ldots\name{do}
  1203. loops in any construction. To force early termination from loops, the
  1204. \name{go to}(\nameref{goto}) command must be used.
  1205. When you use nested block statements, a
  1206. \name{return} from an inner block exits returning a value to the next-outermost
  1207. block, rather than all the way to the outside.
  1208. \end{Comments}
  1209. \end{Command}
  1210. \begin{Operator}{REVERSE}
  1211. \index{list}
  1212. The \name{reverse} operator returns a \nameref{list} that is the reverse of the
  1213. list it is given.
  1214. \begin{Syntax}
  1215. \name{reverse}\(\meta{list}\) or \name{reverse} \meta{list}
  1216. \end{Syntax}
  1217. \meta{list} must be a \nameref{list}.
  1218. \begin{Examples}
  1219. aa := \{c,b,a,\{x**2,z**3\},y\}; & AA := \{C,B,A,\{X^{2},Z^{3}\},Y\} \\
  1220. reverse aa; & \{Y,\{X^{2},Z^{3}\},A,B,C\} \\
  1221. reverse(q . reverse aa); & \{C,B,A,\{X^{2},Z^{3}\},Y,Q\}
  1222. \end{Examples}
  1223. \begin{Comments}
  1224. \name{reverse} and \nameref{cons} can be used together to add a new element to
  1225. the end of a list (\name{.} adds its new element to the beginning). The
  1226. \name{reverse} operator uses a noticeable amount of system resources,
  1227. especially if the list is long. If you are doing much heavy-duty list
  1228. manipulation, you should probably design your algorithms to avoid much
  1229. reversing of lists. A moderate amount of list reversing is no problem.
  1230. \end{Comments}
  1231. \end{Operator}
  1232. \begin{Type}{RULE}
  1233. \index{rule}\index{rule list}
  1234. A \name{rule} is an instruction to replace an algebraic expression
  1235. or a part of an expression by another one.
  1236. \begin{Syntax}
  1237. \meta{lhs} => \meta{rhs} or
  1238. \meta{lhs} => \meta{rhs} \name{when} \meta{cond}
  1239. \end{Syntax}
  1240. \meta{lhs} is an algebraic expression used as search pattern and
  1241. \meta{rhs} is an algebraic expression which replaces matches of
  1242. \meta{rhs}. \name{=>} is the operator \nameref{replace}.
  1243. \meta{lhs} can contain \nameref{free variable}s which are
  1244. symbols preceded by a tilde \nameindex{~} in their leftmost position
  1245. in \meta{lhs}.
  1246. A double tilde marks an \nameref{optional free variable}.
  1247. If a rule has a \name{when} \meta{cond}
  1248. part it will fire only if the evaluation of \meta{cond} has a
  1249. result \nameref{true}. \meta{cond} may contain references to
  1250. free variables of \meta{lhs}.
  1251. Rules can be collected in a \nameref{list} which then forms a
  1252. \nameindex{rule list}. \name{Rule lists} can be used to collect
  1253. algebraic knowledge for a specific evaluation context.
  1254. \name{Rules} and \name{rule lists} are globally activated and
  1255. deactivated by \nameref{let}, \nameref{forall}, \nameref{clearrules}.
  1256. For a single evaluation they can be locally activate by \nameref{where}.
  1257. The active rules for an operator can be visualized by \nameref{showrules}.
  1258. \begin{Examples}
  1259. operator f,g,h; \\
  1260. let f(x) => x^2; \\
  1261. f(x); & x^{2}\\
  1262. g_rules:=\{g(~n,~x)=>h(n/2,x) when evenp n,\\
  1263. g(~n,~x)=>h((1-n)/2,x) when not evenp n\}$\\
  1264. let g_rules;\\
  1265. g(3,x); & h(-1,x)
  1266. \end{Examples}
  1267. \end{Type}
  1268. \begin{Type}{Free Variable}
  1269. \index{variable}
  1270. A variable preceded by a tilde is considered as \name{free variable}
  1271. and stands for an arbitrary part in an algebraic form during
  1272. pattern matching. Free variables occur in the left-hand sides
  1273. of \nameref{rule}s, in the side relations for \nameref{compact}
  1274. and in the first arguments of \nameref{map} and \nameref{select}
  1275. calls. See \nameref{rule} for examples.
  1276. In rules also \nameref{optional free variable}s may occur.
  1277. \end{Type}
  1278. \begin{Type}{Optional Free Variable}
  1279. \index{variable}
  1280. A variable preceded by a double tilde is considered as
  1281. \name{optional free variable}
  1282. and stands for an arbitrary part part in an algebraic form during
  1283. pattern matching. In contrast to ordinary \nameref{free variable}s
  1284. an operator pattern with an \name{optional free variable}
  1285. matches also if the operand for the variable is missing. In such
  1286. a case the variable is bound to a neutral value.
  1287. Optional free variables can be used as
  1288. term in a sum: set to 0 if missing,
  1289. factor in a product: set to 1 if missing,
  1290. exponent: set to 1 if missing
  1291. \begin{Examples}
  1292. sin(~~u + ~~n * pi) => sin(u) when evenp u;
  1293. \end{Examples}
  1294. Optional free variables are allowed only in the left-hand sides
  1295. of \nameref{rule}s.
  1296. \end{Type}
  1297. \begin{Operator}{SECOND}
  1298. \index{list}\index{decomposition}
  1299. The \name{second} operator returns the second element of a list.
  1300. \begin{Syntax}
  1301. \name{second}\(\meta{list}\) or \name{second} \meta{list}
  1302. \end{Syntax}
  1303. \meta{list} must be a list with at least two elements, to avoid an error
  1304. message.
  1305. \begin{Examples}
  1306. alist := \{a,b,c,d\}; & ALIST := \{A,B,C,D\} \\
  1307. second alist; & B \\
  1308. blist := \{x,\{aa,bb,cc\},z\}; & BLIST := \{X,\{AA,BB,CC\},Z\} \\
  1309. second second blist; & BB
  1310. \end{Examples}
  1311. \end{Operator}
  1312. \begin{Operator}{SET}
  1313. \index{assign}
  1314. The \name{set} operator is used for assignments when you want both sides of
  1315. the assignment statement to be evaluated.
  1316. %%% INCONSISTENT??? hyphen after restricted
  1317. \begin{Syntax}
  1318. \name{set}\(\meta{restricted\_expression},\meta{expression}\)
  1319. \end{Syntax}
  1320. \meta{expression} can be any REDUCE expression; \meta{restricted\_expression}
  1321. must be an identifier or an expression that evaluates to an identifier.
  1322. \begin{Examples}
  1323. a := y; & A := Y \\
  1324. set(a,sin(x^2)); & SIN(X^{2}) \\
  1325. a; & SIN(X^{2}) \\
  1326. y; & SIN(X^{2}) \\
  1327. a := b + c; & A := B + C \\
  1328. set(a-c,z); & Z \\
  1329. b; & Z
  1330. \end{Examples}
  1331. \begin{Comments}
  1332. Using an \nameref{array} or \nameref{matrix} reference as the first
  1333. argument to \name{set} has
  1334. the result of setting the {\it contents} of the designated element to
  1335. \name{set}'s second argument. You should be careful to avoid unwanted
  1336. side effects when you use this facility.
  1337. \end{Comments}
  1338. \end{Operator}
  1339. \begin{Operator}{SETQ}
  1340. \index{assign}
  1341. The \name{setq} operator is an infix or prefix binary assignment operator.
  1342. It is identical to \name{:=}.
  1343. \begin{Syntax}
  1344. \name{setq}\(\meta{restricted\_expression},\meta{expression}\) or \\
  1345. \meta{restricted\_expression} \name{setq} \meta{expression}
  1346. \end{Syntax}
  1347. \meta{restricted expression} is ordinarily a single identifier, though
  1348. simple expressions may be used (see Comments below). \meta{expression} can
  1349. be any valid REDUCE expression. If \meta{expression} is a \nameref{matrix}
  1350. identifier, then \meta{restricted\_expression} can be a matrix identifier
  1351. (redimensioned if necessary), which has each element set to the
  1352. corresponding elements of the identifier on the right-hand side.
  1353. \begin{Examples}
  1354. setq(b,6); & B := 6 \\
  1355. c setq sin(x); & C := SIN(X) \\
  1356. w + setq(c,x+3) + z; & W + X + Z + 3 \\
  1357. c; & X + 3 \\
  1358. setq(a1 + a2,25); & A1 + A2 := 25 \\
  1359. a1; & - (A2 - 25)
  1360. \end{Examples}
  1361. \begin{Comments}
  1362. Embedding a \name{setq} statement in an expression has the side effect of making
  1363. the assignment, as shown in the third example above.
  1364. Assignments are generally done for identifiers, but may be done for simple
  1365. expressions as well, subject to the following remarks:
  1366. \begin{itemize}
  1367. \item[(i)]
  1368. If the left-hand side is an identifier, an operator, or a power, the rule
  1369. is added to the rule table.
  1370. \item[(ii)]
  1371. If the operators \name{- + /} appear on the left-hand side, all but the first
  1372. term of the expression is moved to the right-hand side.
  1373. \item[(iii)]
  1374. If the operator \name{*} appears on the left-hand side, any constant terms are
  1375. moved to the right-hand side, but the symbolic factors remain.
  1376. \end{itemize}
  1377. Be careful not to make a recursive \name{setq} assignment that is not
  1378. controlled inside a loop statement. The process of resubstitution
  1379. continues until you get a stack overflow message. \name{setq} can be used
  1380. to attach functionality to operators, as the \name{:=} does.
  1381. \end{Comments}
  1382. \end{Operator}
  1383. \begin{Operator}{THIRD}
  1384. \index{list}\index{decomposition}
  1385. The \name{third} operator returns the third item of a \nameref{list}.
  1386. \begin{Syntax}
  1387. \name{third}\(\meta{list}\) or \name{third} \meta{list}
  1388. \end{Syntax}
  1389. \meta{list} must be a list containing at least three items to avoid an error
  1390. message.
  1391. \begin{Examples}
  1392. alist := \{a,b,c,d\}; & ALIST := \{A,B,C,D\} \\
  1393. third alist; & C \\
  1394. blist := \{x,\{aa,bb,cc\},y,z\}; & BLIST := \{X,\{AA,BB,CC\},Y,Z\}; \\
  1395. third second blist; & CC \\
  1396. third blist; & Y
  1397. \end{Examples}
  1398. \end{Operator}
  1399. \begin{Operator}{WHEN}
  1400. \index{rule}
  1401. The \name{when} operator is used inside a \name{rule} to make the
  1402. execution of the rule depend on a boolean condition which is
  1403. evaluated at execution time. For the use see \nameref{rule}.
  1404. \end{Operator}