/usr/share/doc/yacas-doc/html/refchapter23.html is in yacas-doc 1.3.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 | <html>
<head>
<title>Control flow functions</title>
<link rel="stylesheet" href="yacas.css" TYPE="text/css" MEDIA="screen">
</head>
<body>
<a name="c23">
</a>
(directly go to documentation on : <a href="refchapter23.html#MaxEvalDepth" target='Chapters' title="set the maximum evaluation depth">MaxEvalDepth</a>, <a href="refchapter23.html#Hold" target='Chapters' title="keep expression unevaluated">Hold</a>, <a href="refchapter23.html#Eval" target='Chapters' title="force evaluation of expression">Eval</a>, <a href="refchapter23.html#While" target='Chapters' title="loop while a condition is met">While</a>, <a href="refchapter23.html#Until" target='Chapters' title="loop until a condition is met">Until</a>, <a href="refchapter23.html#If" target='Chapters' title="branch point">If</a>, <a href="refchapter23.html#SystemCall" target='Chapters' title="pass a command to the shell">SystemCall</a>, <a href="refchapter23.html#Function" target='Chapters' title="declare or define a function">Function</a>, <a href="refchapter23.html#Macro" target='Chapters' title="declare or define a macro">Macro</a>, <a href="refchapter23.html#Use" target='Chapters' title="load a file, but not twice">Use</a>, <a href="refchapter23.html#For" target='Chapters' title="C-style <b><tt>for</tt></b> loop">For</a>, <a href="refchapter23.html#ForEach" target='Chapters' title="loop over all entries in list">ForEach</a>, <a href="refchapter23.html#Apply" target='Chapters' title="apply a function to arguments">Apply</a>, <a href="refchapter23.html#MapArgs" target='Chapters' title="apply a function to all top-level arguments">MapArgs</a>, <a href="refchapter23.html#Subst" target='Chapters' title="perform a substitution">Subst</a>, <a href="refchapter23.html#WithValue" target='Chapters' title="temporary assignment during an evaluation">WithValue</a>, <a href="refchapter23.html#/:" target='Chapters' title="local simplification rules">/:</a>, <a href="refchapter23.html#/::" target='Chapters' title="local simplification rules">/::</a>, <a href="refchapter23.html#TraceStack" target='Chapters' title="show calling stack after an error occurs">TraceStack</a>, <a href="refchapter23.html#TraceExp" target='Chapters' title="evaluate with tracing enabled">TraceExp</a>, <a href="refchapter23.html#TraceRule" target='Chapters' title="turn on tracing for a particular function">TraceRule</a>, <a href="refchapter23.html#Time" target='Chapters' title="measure the time taken by a function">Time</a>.
)<h1>
23. Control flow functions
</h1>
<p> </p>
<center><table>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#MaxEvalDepth" target='Chapters' title="set the maximum evaluation depth">MaxEvalDepth</a></td>
<td>set the maximum evaluation depth</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Hold" target='Chapters' title="keep expression unevaluated">Hold</a></td>
<td>keep expression unevaluated</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Eval" target='Chapters' title="force evaluation of expression">Eval</a></td>
<td>force evaluation of expression</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#While" target='Chapters' title="loop while a condition is met">While</a></td>
<td>loop while a condition is met</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Until" target='Chapters' title="loop until a condition is met">Until</a></td>
<td>loop until a condition is met</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#If" target='Chapters' title="branch point">If</a></td>
<td>branch point</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#SystemCall" target='Chapters' title="pass a command to the shell">SystemCall</a></td>
<td>pass a command to the shell</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Function" target='Chapters' title="declare or define a function">Function</a></td>
<td>declare or define a function</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Macro" target='Chapters' title="declare or define a macro">Macro</a></td>
<td>declare or define a macro</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Use" target='Chapters' title="load a file, but not twice">Use</a></td>
<td>load a file, but not twice</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#For" target='Chapters' title="C-style <b><tt>for</tt></b> loop">For</a></td>
<td>C-style <b><tt>for</tt></b> loop</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#ForEach" target='Chapters' title="loop over all entries in list">ForEach</a></td>
<td>loop over all entries in list</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Apply" target='Chapters' title="apply a function to arguments">Apply</a></td>
<td>apply a function to arguments</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#MapArgs" target='Chapters' title="apply a function to all top-level arguments">MapArgs</a></td>
<td>apply a function to all top-level arguments</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Subst" target='Chapters' title="perform a substitution">Subst</a></td>
<td>perform a substitution</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#WithValue" target='Chapters' title="temporary assignment during an evaluation">WithValue</a></td>
<td>temporary assignment during an evaluation</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#/:" target='Chapters' title="local simplification rules">/:</a></td>
<td>local simplification rules</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#/::" target='Chapters' title="local simplification rules">/::</a></td>
<td>local simplification rules</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#TraceStack" target='Chapters' title="show calling stack after an error occurs">TraceStack</a></td>
<td>show calling stack after an error occurs</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#TraceExp" target='Chapters' title="evaluate with tracing enabled">TraceExp</a></td>
<td>evaluate with tracing enabled</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#TraceRule" target='Chapters' title="turn on tracing for a particular function">TraceRule</a></td>
<td>turn on tracing for a particular function</td>
</tr>
<tr BGCOLOR=#E0E0E0>
<td><a href="refchapter23.html#Time" target='Chapters' title="measure the time taken by a function">Time</a></td>
<td>measure the time taken by a function</td>
</tr>
</table></center>
<a name="MaxEvalDepth">
</a>
<a name="maxevaldepth">
</a>
<h3>
<hr>MaxEvalDepth -- set the maximum evaluation depth
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
MaxEvalDepth(n)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>n</tt></b> -- new maximum evaluation depth
<p>
<h5>
Description:
</h5>
Use this command to set the maximum evaluation depth to the integer
"n". The default value is 1000. The function <b><tt>MaxEvalDepth</tt></b> returns <b><tt>True</tt></b>.
<p>
The point of having a maximum evaluation depth is to catch any
infinite recursion. For example, after the definition <b><tt>f(x) := f(x)</tt></b>, evaluating the expression <b><tt>f(x)</tt></b> would call <b><tt>f(x)</tt></b>, which would
call <b><tt>f(x)</tt></b>, etc. The interpreter will halt if
the maximum evaluation depth is reached. Also indirect recursion, e.g.
the pair of definitions <b><tt>f(x) := g(x)</tt></b> and <b><tt>g(x) := f(x)</tt></b>, will be caught.
<p>
<h5>
Examples:
</h5>
An example of an infinite recursion, caught because the maximum
evaluation depth is reached.
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(x) := f(x)
Out> True;
In> f(x)
Error on line 1 in file [CommandLine]
Max evaluation stack depth reached.
Please use MaxEvalDepth to increase the stack
size as needed.
</pre></tr>
</table>
<p>
However, a long calculation may cause the maximum evaluation depth to
be reached without the presence of infinite recursion. The function <b><tt>MaxEvalDepth</tt></b> is meant for these cases.
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> 10 # g(0) <-- 1;
Out> True;
In> 20 # g(n_IsPositiveInteger) <-- \
2 * g(n-1);
Out> True;
In> g(1001);
Error on line 1 in file [CommandLine]
Max evaluation stack depth reached.
Please use MaxEvalDepth to increase the stack
size as needed.
In> MaxEvalDepth(10000);
Out> True;
In> g(1001);
Out> 21430172143725346418968500981200036211228096234
1106721488750077674070210224987224498639675763139171
6255189345835106293650374290571384628087196915514939
7149607869135549648461970842149210124742283755908364
3060929499671638825347975351183310878921541258291423
92955373084335320859663305248773674411336138752;
</pre></tr>
</table>
<p>
<a name="Hold">
</a>
<a name="hold">
</a>
<h3>
<hr>Hold -- keep expression unevaluated
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Hold(expr)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>expr</tt></b> -- expression to keep unevaluated
<p>
<h5>
Description:
</h5>
The expression "expr" is returned unevaluated. This is useful to
prevent the evaluation of a certain expression in a context in which
evaluation normally takes place.
<p>
The function <b><tt>UnList()</tt></b> also leaves its result unevaluated. Both functions stop the process of evaluation (no more rules will be applied).
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Echo({ Hold(1+1), "=", 1+1 });
1+1 = 2
Out> True;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Eval" target="Chapters">
Eval
</a>
, <a href="ref.html?HoldArg" target="Chapters">
HoldArg
</a>
, <a href="ref.html?UnList" target="Chapters">
UnList
</a>
.<a name="Eval">
</a>
<a name="eval">
</a>
<h3>
<hr>Eval -- force evaluation of expression
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Eval(expr)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>expr</tt></b> -- expression to evaluate
<p>
<h5>
Description:
</h5>
This function explicitly requests an evaluation of the expression
"expr", and returns the result of this evaluation.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> a := x;
Out> x;
In> x := 5;
Out> 5;
In> a;
Out> x;
In> Eval(a);
Out> 5;
</pre></tr>
</table>
<p>
The variable <b><tt>a</tt></b> is bound to <b><tt>x</tt></b>,
and <b><tt>x</tt></b> is bound to 5. Hence evaluating <b><tt>a</tt></b> will give <b><tt>x</tt></b>. Only when an extra
evaluation of <b><tt>a</tt></b> is requested, the value 5 is
returned.
<p>
Note that the behavior would be different if we had exchanged the
assignments. If the assignment <b><tt>a := x</tt></b> were given
while <b><tt>x</tt></b> had the value 5, the variable <b><tt>a</tt></b> would also get the value 5 because the assignment
operator <b><tt>:=</tt></b> evaluates the right-hand side.
<p>
<h5>
See also:
</h5>
<a href="ref.html?Hold" target="Chapters">
Hold
</a>
, <a href="ref.html?HoldArg" target="Chapters">
HoldArg
</a>
, <a href="ref.html?:=" target="Chapters">
:=
</a>
.<a name="While">
</a>
<a name="while">
</a>
<h3>
<hr>While -- loop while a condition is met
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
While(pred) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>pred</tt></b> -- predicate deciding whether to keep on looping
<p>
<b><tt>body</tt></b> -- expression to loop over
<p>
<h5>
Description:
</h5>
Keep on evaluating "body" while "pred" evaluates to <b><tt>True</tt></b>. More precisely, <b><tt>While</tt></b>
evaluates the predicate "pred", which should evaluate to either <b><tt>True</tt></b> or <b><tt>False</tt></b>. If the result is <b><tt>True</tt></b>, the expression "body" is evaluated and then
the predicate "pred" is again evaluated. If it is still <b><tt>True</tt></b>, the expressions "body" and "pred" are again
evaluated and so on until "pred" evaluates to <b><tt>False</tt></b>. At that point, the loop terminates and <b><tt>While</tt></b>
returns <b><tt>True</tt></b>.
<p>
In particular, if "pred" immediately evaluates to <b><tt>False</tt></b>, the body is never executed. <b><tt>While</tt></b> is the fundamental looping construct on which
all other loop commands are based. It is equivalent to the <b><tt>while</tt></b> command in the programming language C.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x := 0;
Out> 0;
In> While (x! < 10^6) \
[ Echo({x, x!}); x++; ];
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
Out> True;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Until" target="Chapters">
Until
</a>
, <a href="ref.html?For" target="Chapters">
For
</a>
.<a name="Until">
</a>
<a name="until">
</a>
<h3>
<hr>Until -- loop until a condition is met
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Until(pred) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>pred</tt></b> -- predicate deciding whether to stop
<p>
<b><tt>body</tt></b> -- expression to loop over
<p>
<h5>
Description:
</h5>
Keep on evaluating "body" until "pred" becomes <b><tt>True</tt></b>. More precisely, <b><tt>Until</tt></b> first
evaluates the expression "body". Then the predicate "pred" is
evaluated, which should yield either <b><tt>True</tt></b> or <b><tt>False</tt></b>. In the latter case, the expressions "body"
and "pred" are again evaluated and this continues as long as
"pred" is <b><tt>False</tt></b>. As soon as "pred" yields <b><tt>True</tt></b>, the loop terminates and <b><tt>Until</tt></b> returns <b><tt>True</tt></b>.
<p>
The main difference with <b><tt>While</tt></b> is that <b><tt>Until</tt></b> always evaluates the body at least once, but <b><tt>While</tt></b> may not evaluate the body at all. Besides, the
meaning of the predicate is reversed: <b><tt>While</tt></b> stops
if "pred" is <b><tt>False</tt></b> while <b><tt>Until</tt></b> stops if "pred" is <b><tt>True</tt></b>.
The command
<b><tt>Until(pred) body;</tt></b> is equivalent to <b><tt>pred; While(Not pred) body;</tt></b>. In fact, the
implementation of <b><tt>Until</tt></b> is based on the internal
command <b><tt>While</tt></b>. The <b><tt>Until</tt></b>
command can be compared to the <b><tt>do ... while</tt></b>
construct in the programming language C.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x := 0;
Out> 0;
In> Until (x! > 10^6) \
[ Echo({x, x!}); x++; ];
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
Out> True;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?While" target="Chapters">
While
</a>
, <a href="ref.html?For" target="Chapters">
For
</a>
.<a name="If">
</a>
<a name="if">
</a>
<h3>
<hr>If -- branch point
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
If(pred, then)
If(pred, then, else)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>pred</tt></b> -- predicate to test
<p>
<b><tt>then</tt></b> -- expression to evaluate if "pred" is <b><tt>True</tt></b>
<p>
<b><tt>else</tt></b> -- expression to evaluate if "pred" is <b><tt>False</tt></b>
<p>
<h5>
Description:
</h5>
This command implements a branch point. The predicate "pred" is
evaluated, which should result in either <b><tt>True</tt></b> or <b><tt>False</tt></b>. In the first case, the expression "then" is
evaluated and returned. If the predicate yields <b><tt>False</tt></b>, the expression "else" (if present) is evaluated and
returned. If there is no "else" branch (i.e. if the first calling
sequence is used), the <b><tt>If</tt></b> expression returns <b><tt>False</tt></b>.
<p>
<h5>
Examples:
</h5>
The sign function is defined to be 1 if its argument is positive and
-1 if its argument is negative. A possible implementation is
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> mysign(x) := If (IsPositiveReal(x), 1, -1);
Out> True;
In> mysign(Pi);
Out> 1;
In> mysign(-2.5);
Out> -1;
</pre></tr>
</table>
Note that this will give incorrect results, if "x" cannot be
numerically approximated.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> mysign(a);
Out> -1;
</pre></tr>
</table>
Hence a better implementation would be
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> mysign(_x)_IsNumber(N(x)) <-- If \
(IsPositiveReal(x), 1, -1);
Out> True;
</pre></tr>
</table>
<p>
<a name="SystemCall">
</a>
<a name="systemcall">
</a>
<h3>
<hr>SystemCall -- pass a command to the shell
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
SystemCall(str)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>str</tt></b> -- string containing the command to call
<p>
<h5>
Description:
</h5>
The command contained in the string "str" is executed by the
underlying operating system (OS).
The return value of <b><tt>SystemCall</tt></b> is <b><tt>True</tt></b> or <b><tt>False</tt></b> according to the exit code of the command.
<p>
The <b><tt>SystemCall</tt></b> function is not allowed in the body of the <b><tt>Secure</tt></b> command and will lead to an error.
<p>
<h5>
Examples:
</h5>
In a UNIX environment, the command <b><tt>SystemCall("ls")</tt></b> would print the contents of the current directory.
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> SystemCall("ls")
AUTHORS
COPYING
ChangeLog
</pre></tr>
</table>
... (truncated to save space)
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Out> True;
</pre></tr>
</table>
<p>
The standard UNIX command <b><tt>test</tt></b> returns success or failure depending on conditions.
For example, the following command will check if a directory exists:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> SystemCall("test -d scripts/")
Out> True;
</pre></tr>
</table>
<p>
Check that a file exists:
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> SystemCall("test -f COPYING")
Out> True;
In> SystemCall("test -f nosuchfile.txt")
Out> False;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Secure" target="Chapters">
Secure
</a>
.<a name="Function">
</a>
<a name="function">
</a>
<h3>
<hr>Function -- declare or define a function
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Function() func(arglist)
Function() func(arglist, ...)
Function("op", {arglist}) body
Function("op", {arglist, ...}) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>func(args)</tt></b> -- function declaration, e.g. <b><tt>f(x,y)</tt></b>
<p>
<b><tt>"op"</tt></b> -- string, name of the function
<p>
<b><tt>{arglist}</tt></b> -- list of atoms, formal arguments to the function
<p>
<b><tt>...</tt></b> -- literal ellipsis symbol "<b><tt>...</tt></b>" used to denote a variable number of arguments
<p>
<b><tt>body</tt></b> -- expression comprising the body of the function
<p>
<h5>
Description:
</h5>
This command can be used to define a new function with named arguments.
<p>
The number of arguments of the new function and their names are determined by the list <b><tt>arglist</tt></b>. If the ellipsis "<b><tt>...</tt></b>" follows the last atom in <b><tt>arglist</tt></b>, a function with a variable number of arguments is declared (using <b><tt>RuleBaseListed</tt></b>). Note that the ellipsis cannot be the only element of <b><tt>arglist</tt></b> and <i>must</i> be preceded by an atom.
<p>
A function with variable number of arguments can take more arguments than elements in <b><tt>arglist</tt></b>; in this case, it obtains its last argument as a list containing all extra arguments.
<p>
The short form of the <b><tt>Function</tt></b> call merely declares a <b><tt>RuleBase</tt></b> for the new function but does not define any function body. This is a convenient shorthand for <b><tt>RuleBase</tt></b> and <b><tt>RuleBaseListed</tt></b>, when definitions of the function are to be supplied by rules. If the new function has been already declared with the same number of arguments (with or without variable arguments), <b><tt>Function</tt></b> returns false and does nothing.
<p>
The second, longer form of the <b><tt>Function</tt></b> call declares a function and also defines a function body. It is equivalent to a
single rule such as <b><tt>op(_arg1, _arg2) <-- body</tt></b>. The rule will be declared at
precedence 1025. Any previous rules associated with <b><tt>"op"</tt></b> (with the same
arity) will be discarded. More complicated functions (with more than one body)
can be defined by adding more rules.
<p>
<h5>
Examples:
</h5>
This will declare a new function with two or more arguments, but define no rules for it. This is equivalent to <b><tt>RuleBase ("f1", {x, y, ...})</tt></b>.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Function() f1(x,y,...);
Out> True;
In> Function() f1(x,y);
Out> False;
</pre></tr>
</table>
<p>
This defines a function <b><tt>FirstOf</tt></b> which returns the
first element of a list. Equivalent definitions would be
<b><tt>FirstOf(_list) <-- list[1]</tt></b> or <b><tt>FirstOf(list) := list[1]</tt></b>.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Function("FirstOf", {list}) list[1];
Out> True;
In> FirstOf({a,b,c});
Out> a;
</pre></tr>
</table>
<p>
The following function will print all arguments to a string:
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Function("PrintAll",{x, ...}) If(IsList(x),
PrintList(x), ToString()Write(x));
Out> True;
In> PrintAll(1):
Out> " 1";
In> PrintAll(1,2,3);
Out> " 1 2 3";
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?TemplateFunction" target="Chapters">
TemplateFunction
</a>
, <a href="ref.html?Rule" target="Chapters">
Rule
</a>
, <a href="ref.html?RuleBase" target="Chapters">
RuleBase
</a>
, <a href="ref.html?RuleBaseListed" target="Chapters">
RuleBaseListed
</a>
, <a href="ref.html?:=" target="Chapters">
:=
</a>
, <a href="ref.html?Retract" target="Chapters">
Retract
</a>
.<a name="Macro">
</a>
<a name="macro">
</a>
<h3>
<hr>Macro -- declare or define a macro
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Macro() func(arglist)
Macro() func(arglist, ...)
Macro("op", {arglist}) body
Macro("op", {arglist, ...}) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>func(args)</tt></b> -- function declaration, e.g. <b><tt>f(x,y)</tt></b>
<p>
<b><tt>"op"</tt></b> -- string, name of the function
<p>
<b><tt>{arglist}</tt></b> -- list of atoms, formal arguments to the function
<p>
<b><tt>...</tt></b> -- literal ellipsis symbol "<b><tt>...</tt></b>" used to denote a variable number of arguments
<p>
<b><tt>body</tt></b> -- expression comprising the body of the function
<p>
<h5>
Description:
</h5>
This does the same as <b><tt>Function</tt></b>, but for macros. One can define a macro
easily with this function, in stead of having to use <b><tt>DefMacroRuleBase</tt></b>.
<p>
<h5>
Examples:
</h5>
the following example defines a looping function.
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Macro("myfor",{init,pred,inc,body}) [@init;While(@pred)[@body;@inc;];True;];
Out> True;
In> a:=10
Out> 10;
</pre></tr>
</table>
<p>
Here this new macro <b><tt>myfor</tt></b> is used to loop, using a variable <b><tt>a</tt></b> from the
calling environment.
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> myfor(i:=1,i<10,i++,Echo(a*i))
10
20
30
40
50
60
70
80
90
Out> True;
In> i
Out> 10;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Function" target="Chapters">
Function
</a>
, <a href="ref.html?DefMacroRuleBase" target="Chapters">
DefMacroRuleBase
</a>
.<a name="Use">
</a>
<a name="use">
</a>
<h3>
<hr>Use -- load a file, but not twice
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Use(name)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>name</tt></b> -- name of the file to load
<p>
<h5>
Description:
</h5>
If the file "name" has been loaded before, either by an earlier call
to <b><tt>Use</tt></b> or via the <b><tt>DefLoad</tt></b>
mechanism, nothing happens. Otherwise all expressions in the file are
read and evaluated. <b><tt>Use</tt></b> always returns <b><tt>True</tt></b>.
<p>
The purpose of this function is to make sure that the file will at
least have been loaded, but is not loaded twice.
<p>
<h5>
See also:
</h5>
<a href="ref.html?Load" target="Chapters">
Load
</a>
, <a href="ref.html?DefLoad" target="Chapters">
DefLoad
</a>
, <a href="ref.html?DefaultDirectory" target="Chapters">
DefaultDirectory
</a>
.<a name="For">
</a>
<a name="for">
</a>
<h3>
<hr>For -- C-style <b><tt>for</tt></b> loop
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
For(init, pred, incr) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>init</tt></b> -- expression for performing the initialization
<p>
<b><tt>pred</tt></b> -- predicate deciding whether to continue the loop
<p>
<b><tt>incr</tt></b> -- expression to increment the counter
<p>
<b><tt>body</tt></b> -- expression to loop over
<p>
<h5>
Description:
</h5>
This commands implements a C style <b><tt>for</tt></b> loop. First
of all, the expression "init" is evaluated. Then the predicate
"pred" is evaluated, which should return <b><tt>True</tt></b> or
<b><tt>False</tt></b>. Next the loop is executed as long as the
predicate yields <b><tt>True</tt></b>. One traversal of the loop
consists of the subsequent evaluations of "body", "incr", and
"pred". Finally, the value <b><tt>True</tt></b> is returned.
<p>
This command is most often used in a form such as <b><tt>For(i=1, i<=10, i++) body</tt></b>, which evaluates <b><tt>body</tt></b> with
<b><tt>i</tt></b> subsequently set to 1, 2, 3, 4, 5, 6, 7, 8, 9,
and 10.
<p>
The expression <b><tt>For(init, pred, incr) body</tt></b> is
equivalent to <b><tt>init; While(pred) [body; incr;]</tt></b>.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> For (i:=1, i<=10, i++) Echo({i, i!});
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800
Out> True;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?While" target="Chapters">
While
</a>
, <a href="ref.html?Until" target="Chapters">
Until
</a>
, <a href="ref.html?ForEach" target="Chapters">
ForEach
</a>
.<a name="ForEach">
</a>
<a name="foreach">
</a>
<h3>
<hr>ForEach -- loop over all entries in list
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
ForEach(var, list) body
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>var</tt></b> -- looping variable
<p>
<b><tt>list</tt></b> -- list of values to assign to "var"
<p>
<b><tt>body</tt></b> -- expression to evaluate with different values of "var"
<p>
<h5>
Description:
</h5>
The expression "body" is evaluated multiple times. The first time,
"var" has the value of the first element of "list", then it gets
the value of the second element and so on. <b><tt>ForEach</tt></b>
returns <b><tt>True</tt></b>.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> ForEach(i,{2,3,5,7,11}) Echo({i, i!});
2 2
3 6
5 120
7 5040
11 39916800
Out> True;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?For" target="Chapters">
For
</a>
.<a name="Apply">
</a>
<a name="apply">
</a>
<h3>
<hr>Apply -- apply a function to arguments
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Apply(fn, arglist)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>fn</tt></b> -- function to apply
<p>
<b><tt>arglist</tt></b> -- list of arguments
<p>
<h5>
Description:
</h5>
This function applies the function "fn" to the arguments in
"arglist" and returns the result. The first parameter "fn" can
either be a string containing the name of a function or a pure
function. Pure functions, modeled after lambda-expressions, have the
form "<b><tt>varlist,body</tt></b>", where "varlist" is the list of formal
parameters. Upon application, the formal parameters are assigned the
values in "arglist" (the second parameter of <b><tt>Apply</tt></b>) and the "body" is evaluated.
<p>
Another way to define a pure function is with the Lambda construct.
Here, in stead of passing in "<b><tt>varlist,body</tt></b>", one can pass in
"Lambda(varlist,body)". Lambda has the advantage that its arguments
are not evaluated (using lists can have undesirable effects because
lists are evaluated). Lambda can be used everywhere a pure function
is expected, in principle, because the function Apply is the only function
dealing with pure functions. So all places where a pure function can
be passed in will also accept Lambda.
<p>
An shorthand for <b><tt>Apply</tt></b> is provided by the <b><tt>@</tt></b> operator.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Apply("+", {5,9});
Out> 14;
In> Apply({{x,y}, x-y^2}, {Cos(a), Sin(a)});
Out> Cos(a)-Sin(a)^2;
In> Apply(Lambda({x,y}, x-y^2), {Cos(a), Sin(a)});
Out> Cos(a)-Sin(a)^2
In> Lambda({x,y}, x-y^2) @ {Cos(a), Sin(a)}
Out> Cos(a)-Sin(a)^2
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Map" target="Chapters">
Map
</a>
, <a href="ref.html?MapSingle" target="Chapters">
MapSingle
</a>
, <a href="ref.html?@" target="Chapters">
@
</a>
.<a name="MapArgs">
</a>
<a name="mapargs">
</a>
<h3>
<hr>MapArgs -- apply a function to all top-level arguments
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
MapArgs(expr, fn)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>expr</tt></b> -- an expression to work on
<p>
<b><tt>fn</tt></b> -- an operation to perform on each argument
<p>
<h5>
Description:
</h5>
Every top-level argument in "expr" is substituted by the result of
applying "fn" to this argument. Here "fn" can be either the name
of a function or a pure function (see Apply for more information on
pure functions).
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> MapArgs(f(x,y,z),"Sin");
Out> f(Sin(x),Sin(y),Sin(z));
In> MapArgs({3,4,5,6}, {{x},x^2});
Out> {9,16,25,36};
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?MapSingle" target="Chapters">
MapSingle
</a>
, <a href="ref.html?Map" target="Chapters">
Map
</a>
, <a href="ref.html?Apply" target="Chapters">
Apply
</a>
.<a name="Subst">
</a>
<a name="subst">
</a>
<h3>
<hr>Subst -- perform a substitution
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Subst(from, to) expr
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>from</tt></b> -- expression to be substituted
<p>
<b><tt>to</tt></b> -- expression to substitute for "from"
<p>
<b><tt>expr</tt></b> -- expression in which the substitution takes place
<p>
<h5>
Description:
</h5>
This function substitutes every occurrence of "from" in "expr" by
"to". This is a syntactical substitution: only places where "from"
occurs as a subexpression are affected.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Subst(x, Sin(y)) x^2+x+1;
Out> Sin(y)^2+Sin(y)+1;
In> Subst(a+b, x) a+b+c;
Out> x+c;
In> Subst(b+c, x) a+b+c;
Out> a+b+c;
</pre></tr>
</table>
<p>
The explanation for the last result is that the expression <b><tt>a+b+c</tt></b> is internally stored as <b><tt>(a+b)+c</tt></b>. Hence <b><tt>a+b</tt></b> is a subexpression, but <b><tt>b+c</tt></b> is not.
<p>
<h5>
See also:
</h5>
<a href="ref.html?WithValue" target="Chapters">
WithValue
</a>
, <a href="ref.html?/:" target="Chapters">
/:
</a>
.<a name="WithValue">
</a>
<a name="withvalue">
</a>
<h3>
<hr>WithValue -- temporary assignment during an evaluation
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
WithValue(var, val, expr)
WithValue({var,...}, {val,...}, expr)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>var</tt></b> -- variable to assign to
<p>
<b><tt>val</tt></b> -- value to be assigned to "var"
<p>
<b><tt>expr</tt></b> -- expression to evaluate with "var" equal to "val"
<p>
<h5>
Description:
</h5>
First, the expression "val" is assigned to the variable
"var". Then, the expression "expr" is evaluated and
returned. Finally, the assignment is reversed so that the variable
"var" has the same value as it had before <b><tt>WithValue</tt></b> was evaluated.
<p>
The second calling sequence assigns the first element in the list of
values to the first element in the list of variables, the second value
to the second variable, etc.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> WithValue(x, 3, x^2+y^2+1);
Out> y^2+10;
In> WithValue({x,y}, {3,2}, x^2+y^2+1);
Out> 14;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Subst" target="Chapters">
Subst
</a>
, <a href="ref.html?/:" target="Chapters">
/:
</a>
.<a name="/:">
</a>
<a name="/:">
</a>
<h3>
<hr>/: -- local simplification rules
</h3>
<a name="/::">
</a>
<a name="/::">
</a>
<h3>
<hr>/:: -- local simplification rules
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
expression /: patterns
expressions /:: patterns
</pre></tr>
</table>
Precedence:
20000
<p>
<h5>
Parameters:
</h5>
<b><tt>expression</tt></b> -- an expression
<p>
<b><tt>patterns</tt></b> -- a list of patterns
<p>
<h5>
Description:
</h5>
Sometimes you have an expression, and you want to use specific
simplification rules on it that are not done by default. This
can be done with the <b><tt>/:</tt></b> and the <b><tt>/::</tt></b> operators. Suppose we have the
expression containing things such as <b><tt>Ln(a*b)</tt></b>, and we want
to change these into <b><tt>Ln(a)+Ln(b)</tt></b>, the easiest way
to do this is using the <b><tt>/:</tt></b> operator, as follows:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Sin(x)*Ln(a*b)
Out> Sin(x)*Ln(a*b);
In> % /: { Ln(_x*_y) <- Ln(x)+Ln(y) }
Out> Sin(x)*(Ln(a)+Ln(b));
</pre></tr>
</table>
<p>
A whole list of simplification rules can be built up in the list,
and they will be applied to the expression on the left hand side
of <b><tt>/:</tt></b> .
<p>
The forms the patterns can have are one of:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
pattern <- replacement
{pattern,replacement}
{pattern,postpredicate,replacement}
</pre></tr>
</table>
<p>
Note that for these local rules, <b><tt><-</tt></b> should be used instead of
<b><tt><--</tt></b> which would be used in a global rule.
<p>
The <b><tt>/:</tt></b> operator traverses an expression much as <b><tt>Subst</tt></b> does, that is, top
down, trying to apply the rules from the beginning of the list of
rules to the end of the list of rules. If the rules cannot be applied
to an expression, it will try subexpressions of that
expression and so on.
<p>
It might be necessary sometimes to use the <b><tt>/::</tt></b> operator, which
repeatedly applies the <b><tt>/:</tt></b> operator until the result doesn't change
any more. Caution is required, since rules can contradict each other,
which could result in an infinite loop. To detect this situation,
just use /: repeatedly on the expression. The repetitive nature
should become apparent.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Sin(u)*Ln(a*b) /: {Ln(_x*_y) <- Ln(x)+Ln(y)}
Out> Sin(u)*(Ln(a)+Ln(b));
In> Sin(u)*Ln(a*b) /:: { a <- 2, b <- 3 }
Out> Sin(u)*Ln(6);
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?Subst" target="Chapters">
Subst
</a>
.<a name="TraceStack">
</a>
<a name="tracestack">
</a>
<h3>
<hr>TraceStack -- show calling stack after an error occurs
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
TraceStack(expression)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>expression</tt></b> -- an expression to evaluate
<p>
<h5>
Description:
</h5>
TraceStack shows the calling stack after an error occurred.
It shows the last few items on the stack, not to flood the screen.
These are usually the only items of interest on the stack.
This is probably by far the most useful debugging function in
Yacas. It shows the last few things it did just after an error
was generated somewhere.
<p>
For each stack frame, it shows if the function evaluated was a
built-in function or a user-defined function, and for the user-defined
function, the number of the rule it is trying whether it was evaluating
the pattern matcher of the rule, or the body code of the rule.
<p>
This functionality is not offered by default because it slows
down the evaluation code.
<p>
<h5>
Examples:
</h5>
Here is an example of a function calling itself recursively,
causing Yacas to flood its stack:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(x):=f(Sin(x))
Out> True;
In> TraceStack(f(2))
Debug> 982 : f (Rule # 0 in body)
Debug> 983 : f (Rule # 0 in body)
Debug> 984 : f (Rule # 0 in body)
Debug> 985 : f (Rule # 0 in body)
Debug> 986 : f (Rule # 0 in body)
Debug> 987 : f (Rule # 0 in body)
Debug> 988 : f (Rule # 0 in body)
Debug> 989 : f (Rule # 0 in body)
Debug> 990 : f (Rule # 0 in body)
Debug> 991 : f (Rule # 0 in body)
Debug> 992 : f (Rule # 0 in body)
Debug> 993 : f (Rule # 0 in body)
Debug> 994 : f (Rule # 0 in body)
Debug> 995 : f (User function)
Debug> 996 : Sin (Rule # 0 in pattern)
Debug> 997 : IsList (Internal function)
Error on line 1 in file [CommandLine]
Max evaluation stack depth reached.
Please use MaxEvalDepth to increase the stack
size as needed.
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?TraceExp" target="Chapters">
TraceExp
</a>
, <a href="ref.html?TraceRule" target="Chapters">
TraceRule
</a>
.<a name="TraceExp">
</a>
<a name="traceexp">
</a>
<h3>
<hr>TraceExp -- evaluate with tracing enabled
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
TraceExp(expr)
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>expr</tt></b> -- expression to trace
<p>
<h5>
Description:
</h5>
The expression "expr" is evaluated with the tracing facility turned
on. This means that every subexpression, which is evaluated, is shown
before and after evaluation. Before evaluation, it is shown in the
form <b><tt>TrEnter(x)</tt></b>, where <b><tt>x</tt></b>
denotes the subexpression being evaluated. After the evaluation the
line <b><tt>TrLeave(x,y)</tt></b> is printed, where <b><tt>y</tt></b> is the result of the evaluation. The indentation
shows the nesting level.
<p>
Note that this command usually generates huge amounts of output. A
more specific form of tracing (eg. <b><tt>TraceRule</tt></b>) is
probably more useful for all but very simple expressions.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> TraceExp(2+3);
TrEnter(2+3);
TrEnter(2);
TrLeave(2, 2);
TrEnter(3);
TrLeave(3, 3);
TrEnter(IsNumber(x));
TrEnter(x);
TrLeave(x, 2);
TrLeave(IsNumber(x),True);
TrEnter(IsNumber(y));
TrEnter(y);
TrLeave(y, 3);
TrLeave(IsNumber(y),True);
TrEnter(True);
TrLeave(True, True);
TrEnter(MathAdd(x,y));
TrEnter(x);
TrLeave(x, 2);
TrEnter(y);
TrLeave(y, 3);
TrLeave(MathAdd(x,y),5);
TrLeave(2+3, 5);
Out> 5;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?TraceStack" target="Chapters">
TraceStack
</a>
, <a href="ref.html?TraceRule" target="Chapters">
TraceRule
</a>
.<a name="TraceRule">
</a>
<a name="tracerule">
</a>
<h3>
<hr>TraceRule -- turn on tracing for a particular function
</h3>
<h5 align=right>Internal function</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
TraceRule(template) expr
</pre></tr>
</table>
<p>
<h5>
Parameters:
</h5>
<b><tt>template</tt></b> -- template showing the operator to trace
<p>
<b><tt>expr</tt></b> -- expression to evaluate with tracing on
<p>
<h5>
Description:
</h5>
The tracing facility is turned on for subexpressions of the form
"template", and the expression "expr" is evaluated. The template
"template" is an example of the function to trace on. Specifically, all
subexpressions with the same top-level operator and arity as "template"
are shown. The subexpressions are displayed before (indicated with <b><tt>TrEnter</tt></b>) and after (<b><tt>TrLeave</tt></b>)
evaluation. In between, the arguments are shown before and after
evaluation (<b><tt>TrArg</tt></b>). Only functions defined in
scripts can be traced.
<p>
This is useful for tracing a function that is called from within
another function. This way you can see how your function behaves
in the environment it is used in.
<p>
<h5>
Examples:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> TraceRule(x+y) 2+3*5+4;
TrEnter(2+3*5+4);
TrEnter(2+3*5);
TrArg(2, 2);
TrArg(3*5, 15);
TrLeave(2+3*5, 17);
TrArg(2+3*5, 17);
TrArg(4, 4);
TrLeave(2+3*5+4, 21);
Out> 21;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?TraceStack" target="Chapters">
TraceStack
</a>
, <a href="ref.html?TraceExp" target="Chapters">
TraceExp
</a>
.<a name="Time">
</a>
<a name="time">
</a>
<h3>
<hr>Time -- measure the time taken by a function
</h3>
<h5 align=right>Standard library</h5><h5>
Calling format:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
Time(expr)
</pre></tr>
</table>
<h5>
Parameters:
</h5>
<b><tt>expr</tt></b> -- any expression
<h5>
Description:
</h5>
The function <b><tt>Time(expr)</tt></b> evaluates the expression <b><tt>expr</tt></b> and prints the time in seconds needed for the evaluation.
The time is printed to the current output stream.
The built-in function <b><tt>GetTime</tt></b> is used for timing.
<p>
The result is the "user time" as reported by the OS, not the real ("wall clock") time.
Therefore, any CPU-intensive processes running alongside Yacas will not significantly affect the result of <b><tt>Time</tt></b>.
<p>
<h5>
Example:
</h5>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Time(N(MathLog(1000),40))
0.34 seconds taken
Out> 6.9077552789821370520539743640530926228033;
</pre></tr>
</table>
<p>
<h5>
See also:
</h5>
<a href="ref.html?GetTime" target="Chapters">
GetTime
</a>
.
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2425144-1";
urchinTracker();
</script>
</body>
</html>
|