/usr/share/doc/scheme48/html/manual-Z-H-6.html is in scheme48-doc 1.9-5.
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 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 | <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!--
Generated from manual.tex by tex2page, v 20100828
(running on MzScheme 4.2.4, :unix),
(c) Dorai Sitaram,
http://evalwhen.com/tex2page/index.html
-->
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
The Incomplete Scheme 48 Reference Manual for release 1.9
</title>
<link rel="stylesheet" type="text/css" href="manual-Z-S.css" title=default>
<meta name=robots content="index,follow">
</head>
<body>
<div id=slidecontent>
<div align=right class=navigation>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-5.html">previous</a></span><span>, <a href="manual-Z-H-7.html">next</a></span> page<span>; </span><span><a href="manual-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="manual-Z-H-11.html#node_index_start">index</a></span>]</div>
<p></p>
<a name="node_chap_5"></a>
<h1 class=chapter>
<div class=chapterheading><a href="manual-Z-H-1.html#node_toc_node_chap_5">Chapter 5</a></div><br>
<a href="manual-Z-H-1.html#node_toc_node_chap_5">Libraries</a></h1>
<p>Use the
<tt>,open</tt> command (section <a href="manual-Z-H-4.html#node_sec_3.4">3.4</a>)
or
the module language (chapter <a href="manual-Z-H-3.html#node_sec_2.6">2.6</a>)
to open the structures described below.</p>
<p>
</p>
<a name="node_sec_5.1"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.1">5.1 General utilities</a></h2>
<p></p>
<p>
</p>
<p>
These are in the <tt>big-util</tt> structure.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(atom?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_14"></a></p>
</ul><p>
<tt>(atom? <i>x</i>)</tt> is the same as <tt>(not (pair? <i>x</i>))</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(null-list?<i> list</i>) –> <i>boolean</i></tt><a name="node_idx_16"></a></p>
</ul><p>
Returns true for the empty list, false for a pair, and signals an
error otherwise.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(neq?<i> value value</i>) –> <i>boolean</i></tt><a name="node_idx_18"></a></p>
</ul><p>
<tt>(neq? <i>x</i> <i>y</i>)</tt> is the same as <tt>(not (eq? <i>x</i>
<i>y</i>))</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(n=<i> number number</i>) –> <i>boolean</i></tt><a name="node_idx_20"></a></p>
</ul><p>
<tt>(n= <i>x</i> <i>y</i>)</tt> is the same as <tt>(not (= <i>x</i>
<i>y</i>))</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(identity<i> value</i>) –> <i>value</i></tt><a name="node_idx_22"></a></p>
<li><p></p>
<p class=noindent><tt>(no-op<i> value</i>) –> <i>value</i></tt><a name="node_idx_24"></a></p>
</ul><p>
These both just return their argument. <tt>No-op</tt> is guaranteed not to
be compiled in-line, <tt>identity</tt> may be.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(memq?<i> value list</i>) –> <i>boolean</i></tt><a name="node_idx_26"></a></p>
</ul><p>
Returns true if <i>value</i> is in <i>list</i>, false otherwise.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(any?<i> predicate list</i>) –> <i>boolean</i></tt><a name="node_idx_28"></a></p>
</ul><p>
Returns true if <i>predicate</i> is true for any element of <i>list</i>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(every?<i> predicate list</i>) –> <i>boolean</i></tt><a name="node_idx_30"></a></p>
</ul><p>
Returns true if <i>predicate</i> is true for every element of <i>list</i>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(any<i> predicate list</i>) –> <i>value</i></tt><a name="node_idx_32"></a></p>
<li><p></p>
<p class=noindent><tt>(first<i> predicate list</i>) –> <i>value</i></tt><a name="node_idx_34"></a></p>
</ul><p>
<tt>Any</tt> returns some element of <i>list</i> for which <i>predicate</i> is true, or
false if there are none. <tt>First</tt> does the same except that it returns
the first element for which <i>predicate</i> is true.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(filter<i> predicate list</i>) –> <i>list</i></tt><a name="node_idx_36"></a></p>
<li><p></p>
<p class=noindent><tt>(filter!<i> predicate list</i>) –> <i>list</i></tt><a name="node_idx_38"></a></p>
</ul><p>
Returns a list containing all of the elements of <i>list</i> for which
<i>predicate</i> is true. The order of the elements is preserved.
<tt>Filter!</tt> may reuse the storage of <i>list</i>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(filter-map<i> procedure list</i>) –> <i>list</i></tt><a name="node_idx_40"></a></p>
</ul><p>
The same as <tt>filter</tt> except the returned list contains the results of
applying <i>procedure</i> instead of elements of <i>list</i>. <tt>(filter-map <i>p</i>
<i>l</i>)</tt> is the same as <tt>(filter identity (map <i>p</i> <i>l</i>))</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(partition-list<i> predicate list</i>) –> <i>list list</i></tt><a name="node_idx_42"></a></p>
<li><p></p>
<p class=noindent><tt>(partition-list!<i> predicate list</i>) –> <i>list list</i></tt><a name="node_idx_44"></a></p>
</ul><p>
The first return value contains those elements <i>list</i> for which
<i>predicate</i> is true, the second contains the remaining elements.
The order of the elements is preserved. <tt>Partition-list!</tt> may
reuse the storage of the <i>list</i>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(remove-duplicates<i> list</i>) –> <i>list</i></tt><a name="node_idx_46"></a></p>
</ul><p>
Returns its argument with all duplicate elements removed. The first
instance of each element is preserved.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(delq<i> value list</i>) –> <i>list</i></tt><a name="node_idx_48"></a></p>
<li><p></p>
<p class=noindent><tt>(delq!<i> value list</i>) –> <i>list</i></tt><a name="node_idx_50"></a></p>
<li><p></p>
<p class=noindent><tt>(delete<i> predicate list</i>) –> <i>list</i></tt><a name="node_idx_52"></a></p>
</ul><p>
All three of these return <i>list</i> with some elements removed.
<tt>Delq</tt> removes all elements <tt>eq?</tt> to <i>value</i>. <tt>Delq!</tt>
does the same and may modify the list argument. <tt>Delete</tt> removes
all elements for which <i>predicate</i> is true. Both <tt>delq</tt> and
<tt>delete</tt> may reuse some of the storage in the list argument, but
won't modify it.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(reverse!<i> list</i>) –> <i>list</i></tt><a name="node_idx_54"></a></p>
</ul><p>
Destructively reverses <i>list</i>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(concatenate-symbol<i> value <tt>...</tt></i>) –> <i>symbol</i></tt><a name="node_idx_56"></a></p>
</ul><p>
Returns the symbol whose name is produced by concatenating the
<tt>display</tt>ed
representations of <i>value</i> <tt>...</tt>.</p>
<p>
</p>
<pre class=verbatim>(concatenate-symbol 'abc "-" 4) ===⇒ 'abc-4
</pre><p></p>
<p>
</p>
<a name="node_sec_5.2"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.2">5.2 Pretty-printing</a></h2>
<p>These are in the <tt>pp</tt> structure.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(p<i> value</i>)</tt><a name="node_idx_58"></a></p>
<li><p></p>
<p class=noindent><tt>(p<i> value output-port</i>)</tt><a name="node_idx_60"></a></p>
<li><p></p>
<p class=noindent><tt>(pretty-print<i> value output-port position</i>)</tt><a name="node_idx_62"></a></p>
</ul><p>
Pretty-print <i>value</i> The current output port is used if no port is
specified. <i>Position</i> is the starting offset. <i>Value</i> will be
pretty-printed to the right of this column.</p>
<p>
</p>
<a name="node_sec_5.3"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.3">5.3 Bitwise integer operations</a></h2>
<p>These functions use the two's-complement representation for integers.
There is no limit to the number of bits in an integer.
They are in the structures <tt>bitwise</tt> and <tt>big-scheme</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(bitwise-and<i> integer integer <tt>...</tt></i>) –> <i>integer</i></tt><a name="node_idx_64"></a></p>
<li><p></p>
<p class=noindent><tt>(bitwise-ior<i> integer integer <tt>...</tt></i>) –> <i>integer</i></tt><a name="node_idx_66"></a></p>
<li><p></p>
<p class=noindent><tt>(bitwise-xor<i> integer integer <tt>...</tt></i>) –> <i>integer</i></tt><a name="node_idx_68"></a></p>
<li><p></p>
<p class=noindent><tt>(bitwise-not<i> integer</i>) –> <i>integer</i></tt><a name="node_idx_70"></a></p>
</ul><p>
</p>
<p class=noindent>These perform various logical operations on integers on a bit-by-bit
basis. `<tt>ior</tt>' is inclusive OR and `<tt>xor</tt>' is exclusive OR.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(arithmetic-shift<i> integer bit-count</i>) –> <i>integer</i></tt><a name="node_idx_72"></a></p>
</ul><p>
</p>
<p class=noindent>Shifts the integer by the given bit count, which must be an integer,
shifting left for positive counts and right for negative ones.
Shifting preserves the integer's sign.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(bit-count<i> integer</i>) –> <i>integer</i></tt><a name="node_idx_74"></a></p>
</ul><p>
</p>
<p class=noindent>Counts the number of bits set in the integer.
If the argument is negative a bitwise NOT operation is performed
before counting.</p>
<p>
</p>
<a name="node_sec_5.4"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.4">5.4 Byte vectors</a></h2>
<p>These are homogeneous vectors of small integers (0 ≤ <em>i</em> ≤ 255).
The functions that operate on them are analogous to those for vectors.
They are in the structure <tt>byte-vectors</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(byte-vector?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_76"></a></p>
<li><p></p>
<p class=noindent><tt>(make-byte-vector<i> k fill</i>) –> <i>byte-vector</i></tt><a name="node_idx_78"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector<i> b <tt>...</tt></i>) –> <i>byte-vector</i></tt><a name="node_idx_80"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector-length<i> byte-vector</i>) –> <i>integer</i></tt><a name="node_idx_82"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector-ref<i> byte-vector k</i>) –> <i>integer</i></tt><a name="node_idx_84"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector-set!<i> byte-vector k b</i>)</tt><a name="node_idx_86"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector=?<i> byte-vector byte-vector</i>) –> <i>boolean</i></tt><a name="node_idx_88"></a></p>
</ul><p></p>
<p>
</p>
<a name="node_sec_5.5"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.5">5.5 Sparse vectors</a></h2>
<p>These are vectors that grow as large as they need to. That is, they
can be indexed by arbitrarily large nonnegative integers. The
implementation allows for arbitrarily large gaps by arranging the
entries in a tree. They are in the structure <tt>sparse-vectors</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-sparse-vector<i></i>) –> <i>sparse-vector</i></tt><a name="node_idx_90"></a></p>
<li><p></p>
<p class=noindent><tt>(sparse-vector-ref<i> sparse-vector k</i>) –> <i>value</i></tt><a name="node_idx_92"></a></p>
<li><p></p>
<p class=noindent><tt>(sparse-vector-set!<i> sparse-vector k value</i>)</tt><a name="node_idx_94"></a></p>
<li><p></p>
<p class=noindent><tt>(sparse-vector->list<i> sparse-vector</i>) –> <i>list</i></tt><a name="node_idx_96"></a></p>
</ul><p>
<tt>Make-sparse-vector</tt>, <tt>sparse-vector-ref</tt>, and
<tt>sparse-vector-set!</tt> are analogous to <tt>make-vector</tt>,
<tt>vector-ref</tt>, and <tt>vector-set!</tt>, except that the indices
passed to <tt>sparse-vector-ref</tt> and <tt>sparse-vector-set!</tt> can
be arbitrarily large. For indices whose elements have not been set in
a sparse vector, <tt>sparse-vector-ref</tt> returns <tt>#f</tt>.</p>
<p>
<tt>Sparse-vector->list</tt> is for debugging: It returns a list of the
consecutive elements in a sparse vector from 0 to the highest element
that has been set. Note that the list will also include all the
<tt>#f</tt> elements for the unset elements.</p>
<p>
</p>
<a name="node_sec_5.6"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.6">5.6 Cells</a></h2>
<p></p>
<p>
These hold a single value and are useful when a simple indirection is
required.
The system uses these to hold the values of lexical variables that
may be <tt>set!</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(cell?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_98"></a></p>
<li><p></p>
<p class=noindent><tt>(make-cell<i> value</i>) –> <i>cell</i></tt><a name="node_idx_100"></a></p>
<li><p></p>
<p class=noindent><tt>(cell-ref<i> cell</i>) –> <i>value</i></tt><a name="node_idx_102"></a></p>
<li><p></p>
<p class=noindent><tt>(cell-set!<i> cell value</i>)</tt><a name="node_idx_104"></a></p>
</ul><p></p>
<p>
</p>
<a name="node_sec_5.7"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.7">5.7 Queues</a></h2>
<p>These are ordinary first-in, first-out queues.
The procedures are in structure <tt>queues</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-queue<i></i>) –> <i>queue</i></tt><a name="node_idx_106"></a></p>
<li><p></p>
<p class=noindent><tt>(queue?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_108"></a></p>
<li><p></p>
<p class=noindent><tt>(queue-empty?<i> queue</i>) –> <i>boolean</i></tt><a name="node_idx_110"></a></p>
<li><p></p>
<p class=noindent><tt>(list->queue<i> values</i>) –> <i>queue</i></tt><a name="node_idx_112"></a></p>
<li><p></p>
<p class=noindent><tt>(enqueue!<i> queue value</i>)</tt><a name="node_idx_114"></a></p>
<li><p></p>
<p class=noindent><tt>(enqueue-many!<i> queue list</i>)</tt><a name="node_idx_116"></a></p>
<li><p></p>
<p class=noindent><tt>(queue-head-or-value<i> queue value</i>) –> <i>value</i></tt><a name="node_idx_118"></a></p>
<li><p></p>
<p class=noindent><tt>(queue-head-or-thunk<i> queue thunk</i>) –> <i>value</i></tt><a name="node_idx_120"></a></p>
<li><p></p>
<p class=noindent><tt>(queue-head<i> queue</i>) –> <i>value</i></tt><a name="node_idx_122"></a></p>
<li><p></p>
<p class=noindent><tt>(maybe-queue-head<i> queue</i>) –> <i>value</i></tt><a name="node_idx_124"></a></p>
<li><p></p>
<p class=noindent><tt>(dequeue-or-value!<i> queue value</i>) –> <i>value</i></tt><a name="node_idx_126"></a></p>
<li><p></p>
<p class=noindent><tt>(dequeue-or-thunk!<i> queue thunk</i>) –> <i>value</i></tt><a name="node_idx_128"></a></p>
<li><p></p>
<p class=noindent><tt>(dequeue!<i> queue</i>) –> <i>value</i></tt><a name="node_idx_130"></a></p>
<li><p></p>
<p class=noindent><tt>(maybe-dequeue!<i> queue</i>) –> <i>value</i></tt><a name="node_idx_132"></a></p>
<li><p></p>
<p class=noindent><tt>(empty-queue!<i> queue</i>)</tt><a name="node_idx_134"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-queue</tt> creates an empty queue, <tt>queue?</tt> is a predicate for
identifying queues, and <tt>queue-empty?</tt> tells you if a queue is empty.
<tt>List->queue</tt> returns a queue containing <i>values</i>, preserving
their order.
<tt>Enqueue!</tt> adds one value to the queue; <tt>enqueue-many!</tt> adds a
list of values to the queue.
<tt>Queue-head-or-value</tt>, <tt>queue-head-or-thunk</tt>, <tt>queue-head</tt>,
and <tt>maybe-queue-head</tt> return the first value in <i>queue</i> if it is
not empty; if the queue is empty, <tt>queue-head-or-value</tt> returns
<i>value</i>, <tt>queue-head-or-thunk</tt> tail-calls <i>thunk</i>,
<tt>queue-head</tt> raises an error, and <tt>maybe-queue-head</tt> returns
<tt>#f</tt>.
<tt>Dequeue-or-value!</tt>, <tt>dequeue-or-thunk!</tt>, <tt>dequeue!</tt>, and
<tt>maybe-dequeue!</tt> remove a value from the queue if one is available; if
the queue is empty, <tt>dequeue-or-value!</tt> returns <i>value</i>,
<tt>dequeue-or-thunk!</tt> tail-calls <i>thunk</i>, <tt>dequeue!</tt> raises an
error, and <tt>maybe-dequeue!</tt> returns <tt>#f</tt>.
<tt>Empty-queue!</tt> removes all values from <i>queue</i>.</p>
<p>
<tt>(Dequeue-or-value! q value)</tt> is more efficient than, but otherwise
equivalent to:
</p>
<pre class=verbatim>(ensure-atomicity
(if (queue-empty? q)
value
(dequeue! q)))
</pre><p></p>
<p>
Because <tt>queue-head</tt> and <tt>dequeue!</tt> raise exceptions if they are
called on an empty queue, they <em>must not</em> be called with a proposal
already active unless <tt>queue-empty?</tt> has returned <tt>#f</tt> with the
same proposal active.</p>
<p>
The following procedures are not used in the Scheme 48 system, and are <em>very</em> slow. These operations may be removed from the <tt>queues</tt>
structure in a future revision.
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(queue-length<i> queue</i>) –> <i>integer</i></tt><a name="node_idx_136"></a></p>
<li><p></p>
<p class=noindent><tt>(queue->list<i> queue</i>) –> <i>values</i></tt><a name="node_idx_138"></a></p>
<li><p></p>
<p class=noindent><tt>(delete-from-queue!<i> queue value</i>) –> <i>boolean</i></tt><a name="node_idx_140"></a></p>
<li><p></p>
<p class=noindent><tt>(on-queue?<i> queue value</i>) –> <i>boolean</i></tt><a name="node_idx_142"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Queue-length</tt> returns the number of values in <i>queue</i>.
<tt>Queue->list</tt> returns the values in <i>queue</i> as a list, in the
order in which the values were added.
<tt>Delete-from-queue!</tt> removes the first instance of <i>value</i> from
<i>queue</i>, using <tt>eqv?</tt> for comparisons.
<tt>Delete-from-queue!</tt> returns <tt>#t</tt> if it removes an element and
<tt>#f</tt> if it does not.
<tt>On-queue?</tt> returns <tt>#t</tt> if <i>value</i> is in the <i>queue</i>
(using <tt>eqv?</tt> for comparisons) and <tt>#f</tt> if it is not.</p>
<p>
</p>
<a name="node_sec_5.8"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.8">5.8 Arrays</a></h2>
<p>These provide N-dimensional, zero-based arrays and
are in the structure <tt>arrays</tt>.
The array interface is derived from one invented by Alan Bawden.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-array<i> value dimension<sub>0</sub> <tt>...</tt></i>) –> <i>array</i></tt><a name="node_idx_144"></a></p>
<li><p></p>
<p class=noindent><tt>(array<i> dimensions element<sub>0</sub> <tt>...</tt></i>) –> <i>array</i></tt><a name="node_idx_146"></a></p>
<li><p></p>
<p class=noindent><tt>(copy-array<i> array</i>) –> <i>array</i></tt><a name="node_idx_148"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-array</tt> makes a new array with the given dimensions, each of which
must be a non-negative integer.
Every element is initially set to <i>value</i>.
<tt>Array</tt> Returns a new array with the given dimensions and elements.
<i>Dimensions</i> must be a list of positive integers,
The number of elements should be the equal to the product of the
dimensions.
The elements are stored in row-major order.
</p>
<pre class=verbatim>(make-array 'a 2 3) <code class=verbatim>=> </code>{Array 2 3}
(array '(2 3) 'a 'b 'c 'd 'e 'f)
<code class=verbatim>=> </code>{Array 2 3}
</pre><p></p>
<p>
<tt>Copy-array</tt> returns a copy of <i>array</i>.
The copy is identical to the <i>array</i> but does not share storage with it.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(array?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_150"></a></p>
</ul><p>
</p>
<p class=noindent>Returns <tt>#t</tt> if <i>value</i> is an array.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(array-ref<i> array index<sub>0</sub> <tt>...</tt></i>) –> <i>value</i></tt><a name="node_idx_152"></a></p>
<li><p></p>
<p class=noindent><tt>(array-set!<i> array value index<sub>0</sub> <tt>...</tt></i>)</tt><a name="node_idx_154"></a></p>
<li><p></p>
<p class=noindent><tt>(array->vector<i> array</i>) –> <i>vector</i></tt><a name="node_idx_156"></a></p>
<li><p></p>
<p class=noindent><tt>(array-shape<i> array</i>) –> <i>list</i></tt><a name="node_idx_158"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Array-ref</tt> returns the specified array element and <tt>array-set!</tt>
replaces the element with <i>value</i>.
</p>
<pre class=verbatim>(let ((a (array '(2 3) 'a 'b 'c 'd 'e 'f)))
(let ((x (array-ref a 0 1)))
(array-set! a 'g 0 1)
(list x (array-ref a 0 1))))
<code class=verbatim>=> </code>'(b g)
</pre><p></p>
<p>
<tt>Array->vector</tt> returns a vector containing the elements of <i>array</i>
in row-major order.
<tt>Array-shape</tt> returns the dimensions of
the array as a list.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-shared-array<i> array linear-map dimension<sub>0</sub> <tt>...</tt></i>) –> <i>array</i></tt><a name="node_idx_160"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-shared-array</tt> makes a new array that shares storage with <i>array</i>
and uses <i>linear-map</i> to map indexes to elements.
<i>Linear-map</i> must accept as many arguments as the number of
<i>dimension</i>s given and must return a list of non-negative integers
that are valid indexes into <i>array</i>.
</p>
<pre class=verbatim>(array-ref (make-shared-array a f i0 i1 ...)
j0 j1 ...)
</pre><p>
is equivalent to
</p>
<pre class=verbatim>(apply array-ref a (f j0 j1 ...))
</pre><p></p>
<p>
As an example, the following function makes the transpose of a two-dimensional
array:
</p>
<pre class=verbatim>(define (transpose array)
(let ((shape (array-shape array)))
(make-shared-array array
(lambda (x y)
(list y x))
(cadr shape)
(car shape))))
(array->vector
(transpose
(array '(2 3) 'a 'b 'c 'd 'e 'f)))
<code class=verbatim>=> </code>'(a d b e c f)
</pre><p></p>
<p>
</p>
<a name="node_sec_5.9"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.9">5.9 Records</a></h2>
<p></p>
<p>
New types can be constructed using the <tt>define-record-type</tt> macro
from the <tt>define-record-types</tt> structure
The general syntax is:
</p>
<pre class=verbatim>(define-record-type [<i>tag</i>] <i>type-name</i>
(<i>constructor-name</i> <i>field-tag</i> <tt>...</tt>)
<i>predicate-name</i>
(<i>field-tag</i> <i>accessor-name</i> [<i>modifier-name</i>])
<tt>...</tt>)
</pre><p>
This makes the following definitions:
</p>
<ul>
<li><p></p>
<p class=noindent><tt><i>type-name</i></tt> (type)
</p>
<li><p></p>
<p class=noindent><tt>(<i>constructor-name</i><i> field-init <tt>...</tt></i>) –> <i>type-name</i></tt>
</p>
<li><p></p>
<p class=noindent><tt>(<i>predicate-name</i><i> value</i>) –> <i>boolean</i></tt>
</p>
<li><p></p>
<p class=noindent><tt>(<i>accessor-name</i><i> type-name</i>) –> <i>value</i></tt>
</p>
<li><p></p>
<p class=noindent><tt>(<i>modifier-name</i><i> type-name value</i>)</tt>
</p>
</ul><p>
</p>
<p class=noindent><i>Type-name</i> is the record type itself, and can be used to
specify a print method (see below).
<i>Constructor-name</i> is a constructor that accepts values
for the fields whose tags are specified.
<i>Predicate-name</i> is a predicate that returns <tt>#t</tt> for
elements of the type and <tt>#f</tt> for everything else.
The <i>accessor-name</i>s retrieve the values of fields,
and the <i>modifier-name</i>'s update them.
<i>Tag</i> is used in printing instances of the record type and
the <i>field-tag</i>s are used in the inspector and to match
constructor arguments with fields. If <i>tag</i> is not specified,
<i>type-name</i> is used instead.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(define-record-discloser<i> type discloser</i>)</tt><a name="node_idx_162"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Define-record-discloser</tt> determines how
records of type <i>type</i> are printed.
<i>Discloser</i> should be procedure which takes a single
record of type <i>type</i> and returns a list whose car is
a symbol.
The record will be printed as the value returned by <i>discloser</i>
with curly braces used instead of the usual parenthesis.</p>
<p>
For example
</p>
<pre class=verbatim>(define-record-type pare :pare
(kons x y)
pare?
(x kar set-kar!)
(y kdr))
</pre><p>
defines <tt>kons</tt> to be a constructor, <tt>kar</tt> and <tt>kdr</tt> to be
accessors, <tt>set-kar!</tt> to be a modifier, and <tt>pare?</tt> to be a predicate
for a new type of object.
The type itself is named <tt>:pare</tt>.
<tt>Pare</tt> is a tag used in printing the new objects.</p>
<p>
By default, the new objects print as <tt>#{Pare}</tt>.
The print method can be modified using <tt>define-record-discloser</tt>:
</p>
<pre class=verbatim>(define-record-discloser :pare
(lambda (p) `(pare ,(kar p) ,(kdr p))))
</pre><p>
will cause the result of <tt>(kons 1 2)</tt> to print as
<tt>#{Pare 1 2}</tt>.</p>
<p>
<tt>Define-record-resumer</tt> (section <a href="manual-Z-H-9.html#node_sec_8.7.6">8.7.6</a>)
can be used to control how records are stored in heap images.</p>
<p>
</p>
<a name="node_sec_5.9.1"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.9.1">5.9.1 Low-level access to records</a></h3>
<p>Records are implemented using primitive objects exactly analogous
to vectors.
Every record has a record type (which is another record) in the first slot.
Note that use of these procedures, especially <tt>record-set!</tt>, breaks
the record abstraction described above; caution is advised.</p>
<p>
These procedures are in the structure <tt>records</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-record<i> n value</i>) –> <i>record</i></tt><a name="node_idx_164"></a></p>
<li><p></p>
<p class=noindent><tt>(record<i> value <tt>...</tt></i>) –> <i>record-vector</i></tt><a name="node_idx_166"></a></p>
<li><p></p>
<p class=noindent><tt>(record?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_168"></a></p>
<li><p></p>
<p class=noindent><tt>(record-length<i> record</i>) –> <i>integer</i></tt><a name="node_idx_170"></a></p>
<li><p></p>
<p class=noindent><tt>(record-type<i> record</i>) –> <i>value</i></tt><a name="node_idx_172"></a></p>
<li><p></p>
<p class=noindent><tt>(record-ref<i> record i</i>) –> <i>value</i></tt><a name="node_idx_174"></a></p>
<li><p></p>
<p class=noindent><tt>(record-set!<i> record i value</i>)</tt><a name="node_idx_176"></a></p>
</ul><p>
</p>
<p class=noindent>These the same as the standard <tt>vector-</tt> procedures except that they
operate on records.
The value returned by <tt>record-length</tt> includes the slot holding the
record's type.
<tt>(record-type <i>x</i>)</tt> is equivalent to <tt>(record-ref <i>x</i> 0)</tt>.</p>
<p>
</p>
<a name="node_sec_5.9.2"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.9.2">5.9.2 Record types</a></h3>
<p>Record types are themselves records of a particular type (the first slot
of <tt>:record-type</tt> points to itself).
A record type contains four values: the name of the record type, a list of
the names its fields, and procedures for disclosing and resuming records
of that type.
Procedures for manipulating them are in the structure <tt>record-types</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-record-type<i> name field-names</i>) –> <i>record-type</i></tt><a name="node_idx_178"></a></p>
<li><p></p>
<p class=noindent><tt>(record-type?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_180"></a></p>
<li><p></p>
<p class=noindent><tt>(record-type-name<i> record-type</i>) –> <i>symbol</i></tt><a name="node_idx_182"></a></p>
<li><p></p>
<p class=noindent><tt>(record-type-field-names<i> record-type</i>) –> <i>symbols</i></tt><a name="node_idx_184"></a></p>
</ul><p>
</p>
<p class=noindent></p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(record-constructor<i> record-type field-names</i>) –> <i>procedure</i></tt><a name="node_idx_186"></a></p>
<li><p></p>
<p class=noindent><tt>(record-predicate<i> record-type</i>) –> <i>procedure</i></tt><a name="node_idx_188"></a></p>
<li><p></p>
<p class=noindent><tt>(record-accessor<i> record-type field-name</i>) –> <i>procedure</i></tt><a name="node_idx_190"></a></p>
<li><p></p>
<p class=noindent><tt>(record-modifier<i> record-type field-name</i>) –> <i>procedure</i></tt><a name="node_idx_192"></a></p>
</ul><p>
</p>
<p class=noindent>These procedures construct the usual record-manipulating procedures.
<tt>Record-constructor</tt> returns a constructor that is passed the initial
values for the fields specified and returns a new record.
<tt>Record-predicate</tt> returns a predicate that return true when passed
a record of type <i>record-type</i> and false otherwise.
<tt>Record-accessor</tt> and <tt>record-modifier</tt> return procedures that
reference and set the given field in records of the appropriate type.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(define-record-discloser<i> record-type discloser</i>)</tt><a name="node_idx_194"></a></p>
<li><p></p>
<p class=noindent><tt>(define-record-resumer<i> record-type resumer</i>)</tt><a name="node_idx_196"></a></p>
</ul><p>
</p>
<p class=noindent></p>
<p class=noindent><tt>Record-types</tt> is the initial exporter of
<tt>define-record-discloser</tt>
(re-exported by <tt>define-record-types</tt> described above)
and
<tt>define-record-resumer</tt>
(re-exported by
<tt>external-calls</tt> (section <a href="manual-Z-H-9.html#node_sec_8.7.6">8.7.6</a>)).</p>
<p>
The procedures described in this section can be used to define new
record-type-defining macros.
</p>
<pre class=verbatim>(define-record-type pare :pare
(kons x y)
pare?
(x kar set-kar!)
(y kdr))
</pre><p>
is (semantically) equivalent to
</p>
<pre class=verbatim>(define :pare (make-record-type 'pare '(x y)))
(define kons (record-constructor :pare '(x y)))
(define kar (record-accessor :pare 'x))
(define set-kar! (record-modifier :pare 'x))
(define kdr (record-accessor :pare 'y))
</pre><p></p>
<p>
The “(semantically)” above is because <tt>define-record-type</tt> adds
declarations, which allows the type checker to detect some misuses of records,
and uses more efficient definitions for the constructor, accessors, and
modifiers.
Ignoring the declarations, which will have to wait for another edition of
the manual, what the above example actually expands into is:
</p>
<pre class=verbatim>(define :pare (make-record-type 'pare '(x y)))
(define (kons x y) (record :pare x y))
(define (kar r) (checked-record-ref r :pare 1))
(define (set-kar! r new)
(checked-record-set! r :pare 1 new))
(define (kdr r) (checked-record-ref r :pare 2))
</pre><p>
<tt>Checked-record-ref</tt> and <tt>Checked-record-set!</tt> are
low-level procedures that check the type of the
record and access or modify it using a single VM instruction.</p>
<p>
</p>
<a name="node_sec_5.10"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.10">5.10 Finite record types</a></h2>
<p></p>
<p>
The structure <tt>finite-types</tt> has
two macros for defining `finite' record types.
These are record types for which there are a fixed number of instances,
all of which are created at the same time as the record type itself.
The syntax for defining an enumerated type is:
</p>
<pre class=verbatim>(define-enumerated-type <i>tag</i> <i>type-name</i>
<i>predicate-name</i>
<i>vector-of-instances-name</i>
<i>name-accessor</i>
<i>index-accessor</i>
(<i>instance-name</i> <tt>...</tt>))
</pre><p>
This defines a new record type, bound to <i>type-name</i>, with as many
instances as there are <i>instance-name</i>'s.
<i>Vector-of-instances-name</i> is bound to a vector containing the instances
of the type in the same order as the <i>instance-name</i> list.
<i>Tag</i> is bound to a macro that when given an <i>instance-name</i> expands
into an expression that returns corresponding instance.
The name lookup is done at macro expansion time.
<i>Predicate-name</i> is a predicate for the new type.
<i>Name-accessor</i> and <i>index-accessor</i> are accessors for the
name and index (in <i>vector-of-instances</i>) of instances of the type.</p>
<p>
</p>
<pre class=verbatim>(define-enumerated-type color :color
color?
colors
color-name
color-index
(black white purple maroon))
(color-name (vector-ref colors 0)) <code class=verbatim>=> </code>black
(color-name (color white)) <code class=verbatim>=> </code>white
(color-index (color purple)) <code class=verbatim>=> </code>2
</pre><p></p>
<p>
Finite types are enumerations that allow the user to add additional
fields in the type.
The syntax for defining a finite type is:
</p>
<pre class=verbatim>(define-finite-type <i>tag</i> <i>type-name</i>
(<i>field-tag</i> <tt>...</tt>)
<i>predicate-name</i>
<i>vector-of-instances-name</i>
<i>name-accessor</i>
<i>index-accessor</i>
(<i>field-tag</i> <i>accessor-name</i> [<i>modifier-name</i>])
<tt>...</tt>((<i>instance-name</i> <i>field-value</i> <tt>...</tt>)
<tt>...</tt>))
</pre><p>
The additional fields are specified exactly as with <tt>define-record-type</tt>.
The field arguments to the constructor are listed after the <i>type-name</i>;
these do not include the name and index fields.
The form ends with the names and the initial field values for
the instances of the type.
The instances are constructed by applying the (unnamed) constructor to
these initial field values.
The name must be first and
the remaining values must match the <i>field-tag</i>s in the constructor's
argument list.</p>
<p>
</p>
<p>
</p>
<pre class=verbatim>(define-finite-type color :color
(red green blue)
color?
colors
color-name
color-index
(red color-red)
(green color-green)
(blue color-blue)
((black 0 0 0)
(white 255 255 255)
(purple 160 32 240)
(maroon 176 48 96)))
(color-name (color black)) <code class=verbatim>=> </code>black
(color-name (vector-ref colors 1)) <code class=verbatim>=> </code>white
(color-index (color purple)) <code class=verbatim>=> </code>2
(color-red (color maroon)) <code class=verbatim>=> </code>176
</pre><p></p>
<p>
</p>
<a name="node_sec_5.11"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.11">5.11 Sets over finite types</a></h2>
<p></p>
<p>
The structure <tt>enum-sets</tt> has a macro for defining types for sets
of elements of finite types. These work naturally with the finite
types defined by the <tt>finite-types</tt> structure, but are not tied
to them. The syntax for defining such a type is:</p>
<p>
</p>
<pre class=verbatim>(define-enum-set-type <i>id</i> <i>type-name</i> <i>predicate</i> <i>constructor</i>
<i>element-syntax</i> <i>element-predicate</i> <i>all-elements</i> <i>element-index-ref</i>)
</pre><p>
This defines <i>id</i> to be syntax for constructing sets,
<i>type-name</i> to be a value representing the type,
<i>predicate</i> to be a predicate for those sets, and
<i>constructor</i> a procedure for constructing one from a list.</p>
<p>
<i>Element-syntax</i> must be the name of a macro for constructing set
elements from names (akin to the <i>tag</i> argument to
<tt>define-enumerated-type</tt>). <i>Element-predicate</i> must be a
predicate for the element type, <i>all-elements</i> a vector of all
values of the element type, and <i>element-index-ref</i> must return
the index of an element within the <i>all-elements</i> vector.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(enum-set->list<i> enum-set</i>) –> <i>list</i></tt><a name="node_idx_198"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-member?<i> enum-set enumerand</i>) –> <i>boolean</i></tt><a name="node_idx_200"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set=?<i> enum-set enum-set</i>) –> <i>boolean</i></tt><a name="node_idx_202"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-subset?<i> enum-set enum-set</i>) –> <i>boolean</i></tt><a name="node_idx_204"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-union<i> enum-set enum-set</i>) –> <i>enum-set</i></tt><a name="node_idx_206"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-intersection<i> enum-set enum-set</i>) –> <i> enum-set</i></tt><a name="node_idx_208"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-difference<i> enum-set enum-set</i>) –> <i> enum-set</i></tt><a name="node_idx_210"></a></p>
<li><p></p>
<p class=noindent><tt>(enum-set-negation<i> enum-set</i>) –> <i>enum-set</i></tt><a name="node_idx_212"></a></p>
</ul><p>
<tt>Enum-set->list</tt> converts a set into a list of its elements.
<tt>Enum-set-member?</tt> tests for membership. <tt>Enum-set=?</tt> tests
two sets of equal type for equality. (If its arguments are not of the
same type, <tt>enum-set=?</tt> raises an exception.)
<tt>Enum-set-subset?</tt> tests, for two sets of equal type,
if the first set is a subset of the second one.
<tt>Enum-set-union</tt> computes the union of two sets of equal type,
<tt>enum-set-intersection</tt> computes the intersection,
<tt>enum-set-difference</tt> computes the intersection,
and
<tt>enum-set-negation</tt> computes the complement of a set.</p>
<p>
Here is an example. Given an enumerated type:</p>
<p>
</p>
<pre class=verbatim>(define-enumerated-type color :color
color?
colors
color-name
color-index
(red blue green))
</pre><p></p>
<p>
we can define sets of colors:</p>
<p>
</p>
<pre class=verbatim>(define-enum-set-type color-set :color-set
color-set?
make-color-set
color color? colors color-index)
</pre><p></p>
<p>
</p>
<pre class=verbatim>> (enum-set->list (color-set red blue))
(#{Color red} #{Color blue})
> (enum-set->list (enum-set-negation (color-set red blue)))
(#{Color green})
> (enum-set-member? (color-set red blue) (color blue))
#t
</pre><p></p>
<p>
</p>
<a name="node_sec_5.12"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.12">5.12 Hash tables</a></h2>
<p>These are generic hash tables, and are in the structure <tt>tables</tt>.
Strictly speaking they are more maps than tables, as every table has a
value for every possible key (for that type of table).
All but a finite number of those values are <tt>#f</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-table<i></i>) –> <i>table</i></tt><a name="node_idx_214"></a></p>
<li><p></p>
<p class=noindent><tt>(make-symbol-table<i></i>) –> <i>symbol-table</i></tt><a name="node_idx_216"></a></p>
<li><p></p>
<p class=noindent><tt>(make-string-table<i></i>) –> <i>string-table</i></tt><a name="node_idx_218"></a></p>
<li><p></p>
<p class=noindent><tt>(make-integer-table<i></i>) –> <i>integer-table</i></tt><a name="node_idx_220"></a></p>
<li><p></p>
<p class=noindent><tt>(make-table-maker<i> compare-proc hash-proc</i>) –> <i>procedure</i></tt><a name="node_idx_222"></a></p>
<li><p></p>
<p class=noindent><tt>(make-table-immutable!<i> table</i>)</tt><a name="node_idx_224"></a></p>
</ul><p>
</p>
<p class=noindent>The first four functions listed make various kinds of tables.
<tt>Make-table</tt> returns a table whose keys may be symbols, integer,
characters, booleans, or the empty list (these are also the values
that may be used in <tt>case</tt> expressions).
As with <tt>case</tt>, comparison is done using <tt>eqv?</tt>.
The comparison procedures used in symbol, string, and integer tables are
<tt>eq?</tt>, <tt>string=?</tt>, and <tt>=</tt>.</p>
<p>
<tt>Make-table-maker</tt> takes two procedures as arguments and returns
a nullary table-making procedure.
<i>Compare-proc</i> should be a two-argument equality predicate.
<i>Hash-proc</i> should be a one argument procedure that takes a key
and returns a non-negative integer hash value.
If <tt>(<i>compare-proc</i> <i>x</i> <i>y</i>)</tt> returns true,
then <tt>(= (<i>hash-proc</i> <i>x</i>) (<i>hash-proc</i> <i>y</i>))</tt>
must also return true.
For example, <tt>make-integer-table</tt> could be defined
as <tt>(make-table-maker = abs)</tt>.</p>
<p>
<tt>Make-table-immutable!</tt> prohibits future modification to its argument.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(table?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_226"></a></p>
<li><p></p>
<p class=noindent><tt>(table-ref<i> table key</i>) –> <i>value or <tt>#f</tt></i></tt><a name="node_idx_228"></a></p>
<li><p></p>
<p class=noindent><tt>(table-set!<i> table key value</i>)</tt><a name="node_idx_230"></a></p>
<li><p></p>
<p class=noindent><tt>(table-walk<i> procedure table</i>)</tt><a name="node_idx_232"></a></p>
<li><p></p>
<p class=noindent><tt>(table->entry-list<i> table</i>) –> <i>list of pairs</i></tt><a name="node_idx_234"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Table?</tt> is the predicate for tables.
<tt>Table-ref</tt> and <tt>table-set!</tt> access and modify the value of <i>key</i>
in <i>table</i>.
<tt>Table-walk</tt> applies <i>procedure</i>, which must accept two arguments,
to every associated key and non-<tt>#f</tt> value in <tt>table</tt>.
<tt>Table->entry-list</tt> returns a list with the values of
the table.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(default-hash-function<i> value</i>) –> <i>integer</i></tt><a name="node_idx_236"></a></p>
<li><p></p>
<p class=noindent><tt>(string-hash<i> string</i>) –> <i>integer</i></tt><a name="node_idx_238"></a></p>
<li><p></p>
<p class=noindent><tt>(symbol-hash<i> symbol</i>) –> <i>integer</i></tt><a name="node_idx_240"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Default-hash-function</tt> is the hash function used in the tables
returned by <tt>make-table</tt>, <tt>string-hash</tt> is the one used
by <tt>make-string-table</tt>, and <tt>symbol-hash</tt> is the one used
by <tt>make-symbol-table</tt>.</p>
<p>
</p>
<a name="node_sec_5.13"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.13">5.13 Port extensions</a></h2>
<p>These procedures are in structure <tt>extended-ports</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-string-input-port<i> string</i>) –> <i>input-port</i></tt><a name="node_idx_242"></a></p>
<li><p></p>
<p class=noindent><tt>(make-string-output-port<i></i>) –> <i>output-port</i></tt><a name="node_idx_244"></a></p>
<li><p></p>
<p class=noindent><tt>(string-output-port-output<i> string-output-port</i>) –> <i>string</i></tt><a name="node_idx_246"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-string-input-port</tt> returns an input port that
that reads characters from the supplied string. An end-of-file
object is returned if the user reads past the end of the string.
<tt>Make-string-output-port</tt> returns an output port that saves
the characters written to it.
These are then returned as a string by <tt>string-output-port-output</tt>.</p>
<p>
</p>
<pre class=verbatim>(read (make-string-input-port "(a b)"))
<code class=verbatim>=> </code>'(a b)
(let ((p (make-string-output-port)))
(write '(a b) p)
(let ((s (string-output-port-output p)))
(display "c" p)
(list s (string-output-port-output p))))
<code class=verbatim>=> </code>'("(a b)" "(a b)c")
</pre><p></p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(limit-output<i> output-port n procedure</i>)</tt><a name="node_idx_248"></a></p>
</ul><p>
</p>
<p class=noindent><i>Procedure</i> is called on an output port.
Output written to that port is copied to <i>output-port</i> until <i>n</i>
characters have been written, at which point <tt>limit-output</tt> returns.
If <i>procedure</i> returns before writing <i>n</i> characters, then
<tt>limit-output</tt> also returns at that time, regardless of how many
characters have been written.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-tracking-input-port<i> input-port</i>) –> <i>input-port</i></tt><a name="node_idx_250"></a></p>
<li><p></p>
<p class=noindent><tt>(make-tracking-output-port<i> output-port</i>) –> <i>output-port</i></tt><a name="node_idx_252"></a></p>
<li><p></p>
<p class=noindent><tt>(current-row<i> port</i>) –> <i>integer or <tt>#f</tt></i></tt><a name="node_idx_254"></a></p>
<li><p></p>
<p class=noindent><tt>(current-column<i> port</i>) –> <i>integer or <tt>#f</tt></i></tt><a name="node_idx_256"></a></p>
<li><p></p>
<p class=noindent><tt>(fresh-line<i> output-port</i>)</tt><a name="node_idx_258"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-tracking-input-port</tt> and <tt>make-tracking-output-port</tt>
return ports that keep track of the current row and column and
are otherwise identical to their arguments.
Closing a tracking port does not close the underlying port.
<tt>Current-row</tt> and <tt>current-column</tt> return
<i>port</i>'s current read or write location.
They return <tt>#f</tt> if <i>port</i> does not keep track of its location.
<tt>Fresh-line</tt> writes a newline character to <i>output-port</i> if
<tt>(current-row <i>port</i>)</tt> is not 0.</p>
<p>
</p>
<pre class=verbatim>(define p (make-tracking-output-port (open-output-file "/tmp/temp")))
(list (current-row p) (current-column p))
<code class=verbatim>=> </code>'(0 0)
(display "012" p)
(list (current-row p) (current-column p))
<code class=verbatim>=> </code>'(0 3)
(fresh-line p)
(list (current-row p) (current-column p))
<code class=verbatim>=> </code>'(1 0)
(fresh-line p)
(list (current-row p) (current-column p))
<code class=verbatim>=> </code>'(1 0)
</pre><p></p>
<p>
</p>
<a name="node_sec_5.14"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.14">5.14 Fluid bindings</a></h2>
<p>These procedures implement dynamic binding and are in structure <tt>fluids</tt>.
A <i>fluid</i> is a cell whose value can be bound dynamically.
Each fluid has a top-level value that is used when the fluid
is unbound in the current dynamic environment.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-fluid<i> value</i>) –> <i>fluid</i></tt><a name="node_idx_260"></a></p>
<li><p></p>
<p class=noindent><tt>(fluid<i> fluid</i>) –> <i>value</i></tt><a name="node_idx_262"></a></p>
<li><p></p>
<p class=noindent><tt>(let-fluid<i> fluid value thunk</i>) –> <i>value(s)</i></tt><a name="node_idx_264"></a></p>
<li><p></p>
<p class=noindent><tt>(let-fluids<i> fluid<sub>0</sub> value<sub>0</sub> fluid<sub>1</sub> value<sub>1</sub> <tt>...</tt>thunk</i>) –> <i>value(s)</i></tt><a name="node_idx_266"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Make-fluid</tt> returns a new fluid with <i>value</i> as its initial
top-level value.
<tt>Fluid</tt> returns <tt>fluid</tt>'s current value.
<tt>Let-fluid</tt> calls <tt>thunk</tt>, with <i>fluid</i> bound to <i>value</i>
until <tt>thunk</tt> returns.
Using a continuation to throw out of the call to <tt>thunk</tt> causes
<i>fluid</i> to revert to its original value, while throwing back
in causes <i>fluid</i> to be rebound to <i>value</i>.
<tt>Let-fluid</tt> returns the value(s) returned by <i>thunk</i>.
<tt>Let-fluids</tt> is identical to <tt>let-fluid</tt> except that it binds
an arbitrary number of fluids to new values.</p>
<p>
</p>
<pre class=verbatim>(let* ((f (make-fluid 'a))
(v0 (fluid f))
(v1 (let-fluid f 'b
(lambda ()
(fluid f))))
(v2 (fluid f)))
(list v0 v1 v2))
<code class=verbatim>=> </code>'(a b a)
</pre><p></p>
<p>
</p>
<pre class=verbatim>(let ((f (make-fluid 'a))
(path '())
(c #f))
(let ((add (lambda ()
(set! path (cons (fluid f) path)))))
(add)
(let-fluid f 'b
(lambda ()
(call-with-current-continuation
(lambda (c0)
(set! c c0)))
(add)))
(add)
(if (< (length path) 5)
(c)
(reverse path))))
<code class=verbatim>=> </code>'(a b a b a)
</pre><p></p>
<p>
</p>
<a name="node_sec_5.15"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.15">5.15 OS strings</a></h2>
<p></p>
<p>
<a name="node_idx_268"></a>On common operating systems such as Unix and Windows, various
parameters to OS functionality—such as file names, user names,
command-line arguments etc.—appear as text in most contexts, but are
really byte sequences: On Unix, the byte sequence may be interpreted
as text through some locale-determined encoding. On Windows, such
parameters are typically represented as sequences of UTF-16 code
units. In both cases, not every such byte sequence has a string
equivalent: On Unix, a byte sequence encoding a file name using
Latin-1 often cannot be decoded using UTF-8. On Windows, unpaired
UTF-16 surrogates are admissible in encodings, and no lossless text
decoding for them exists.</p>
<p>
For representing such string-like parameters, Scheme 48 uses an
abstraction called <i>OS strings</i>. An OS string is created from
either a string or a NUL-terminated byte sequence stored in a byte
vector, and has an associated text codec (see
section <a href="manual-Z-H-7.html#node_sec_6.6.1">6.6.1</a>) that is able to convert from one
representation to the other. The exact meaning of a NUL-terminated
byte sequence is dependent on this text codec. However, only codecs
for encodings that are a conservative extension of ASCII (such as
ASCII itself, Latin-1, or UTF-8) should be used here, to allow a
minimal set of portable file names. (The Windows port uses a special
synthetic encoding called UTF-8of16 compatible with UTF-8 but capable
of encoding even invalid UTF-16 internally, but uses the UTF-8 codec
at the Scheme level.)</p>
<p>
Most procedures accepting OS strings also accept strings or byte
vectors, which are then used to construct a OS string. In the headers
of the specifications of these procedures, such arguments occur as
<i>os-string-thing</i>.<a name="node_idx_270"></a>The standard Scheme procedures such as <tt>open-input-file</tt> that
take file names all accept <i>os-string-thing</i> arguments. OS
strings are in the <tt>os-strings</tt> structure.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(os-string?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_272"></a></p>
<li><p></p>
<p class=noindent><tt>(make-os-string<i> text-codec string/byte-vector</i>) –> <i>os-string</i></tt><a name="node_idx_274"></a></p>
</ul><p>
The <tt>os-string?</tt> predicate returns <tt>#t</tt> if its argument is
an OS string, <tt>#f</tt> otherwise.</p>
<p>
The <tt>make-os-string</tt> procedure creates an OS string from a text
codec and a byte vector or string that defines its contents. If the
argument is a byte vector, it does not matter if it is NUL-terminated
or not.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(string->os-string<i> string</i>) –> <i>os-string</i></tt><a name="node_idx_276"></a></p>
<li><p></p>
<p class=noindent><tt>(byte-vector->os-string<i> byte-vector</i>) –> <i>os-string</i></tt><a name="node_idx_278"></a></p>
<li><p></p>
<p class=noindent><tt>(x->os-string<i> os-string-thing</i>) –> <i>os-string</i></tt><a name="node_idx_280"></a></p>
</ul><p>
</p>
<p class=noindent>These procedures create an OS string from a string, a byte-vector
(whose last value should be 0), and an <i>os-string-thing</i> argument,
respectively, always using the standard OS-string text codec (see
below).</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(os-string->byte-vector<i> os-string</i>) –> <i>byte-vector</i></tt><a name="node_idx_282"></a></p>
<li><p></p>
<p class=noindent><tt>(os-string->string<i> os-string</i>) –> <i>string</i></tt><a name="node_idx_284"></a></p>
</ul><p>
</p>
<p class=noindent>These procedures yield the contents of an OS string. For an OS string
created from a string, <tt>os-string->string</tt> will return a string
with the same contents; for an OS string created from a byte vector,
<tt>os-string->byte-vector</tt> will return a byte vector with the same
contents. For the other cases, data loss as determined by the text
codec is possible.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(os-string-text-codec<i> os-string</i>) –> <i>text-codec</i></tt><a name="node_idx_286"></a></p>
</ul><p>
This procedure returns the text codec of the OS string.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(os-string=?<i> os-string os-string</i>) –> <i>boolean</i></tt><a name="node_idx_288"></a></p>
</ul><p>
This procedure returns <tt>#t</tt> if its arguments denote the same
byte sequence, <tt>#f</tt> otherwise.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(x->os-byte-vector<i> os-string-thing</i>) –> <i>byte-vector</i></tt><a name="node_idx_290"></a></p>
<li><p></p>
<p class=noindent><tt>(string->os-byte-vector<i> string</i>) –> <i>byte-vector</i></tt><a name="node_idx_292"></a></p>
</ul><p>
These are convenience procedures: The first is the composition of
<tt>x->os-string</tt> and <tt>os-string->byte-vector</tt>, and the second
is the composition of <tt>string->os-string</tt> and
<tt>os-string->byte-vector</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(current-os-string-text-codec<i></i>) –> <i>text-codec</i></tt><a name="node_idx_294"></a></p>
<li><p></p>
<p class=noindent><tt>(call-with-os-string-text-codec<i> text-codec thunk</i>) –> <i> value(s)</i></tt><a name="node_idx_296"></a></p>
</ul><p>
</p>
<p class=noindent>The <tt>current-os-string-text-codec</tt> returns the current text codec
used for creating new OS strings. The initial default is determined
by the operating system. (On Unix, this is the text codec determined
by the locale. On Windows, this is UTF-8.) The
<tt>call-with-os-string-text-codec</tt> procedure dynamically binds the
current text codec to <i>text-codec</i> during the invocation of
<i>thunk</i>.</p>
<p>
</p>
<a name="node_sec_5.16"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.16">5.16 Shell commands</a></h2>
<p>Structure <tt>c-system-function</tt> provides access to the C <tt>system()</tt>
function.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(have-system?<i></i>) –> <i>boolean</i></tt><a name="node_idx_298"></a></p>
<li><p></p>
<p class=noindent><tt>(system<i> os-string-thing</i>) –> <i>integer</i></tt><a name="node_idx_300"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Have-system?</tt> returns true if the underlying C implementation
has a command processor.
<tt>(System <i>string</i>)</tt> passes <i>string</i> to the C
<tt>system()</tt> function and returns the result.</p>
<p>
</p>
<pre class=verbatim>(begin
(system "echo foo > test-file")
(call-with-input-file "test-file" read))
<code class=verbatim>=> </code>'foo
</pre><p></p>
<p>
</p>
<a name="node_sec_5.17"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.17">5.17 Sockets</a></h2>
<p></p>
<p>
Structure <tt>sockets</tt> provides access to TCP/IP sockets for interprocess
and network communication.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(open-socket<i></i>) –> <i>socket</i></tt><a name="node_idx_302"></a></p>
<li><p></p>
<p class=noindent><tt>(open-socket<i> port-number</i>) –> <i>socket</i></tt><a name="node_idx_304"></a></p>
<li><p></p>
<p class=noindent><tt>(socket-port-number<i> socket</i>) –> <i>integer</i></tt><a name="node_idx_306"></a></p>
<li><p></p>
<p class=noindent><tt>(close-socket<i> socket</i>)</tt><a name="node_idx_308"></a></p>
<li><p></p>
<p class=noindent><tt>(socket-accept<i> socket</i>) –> <i>input-port output-port</i></tt><a name="node_idx_310"></a></p>
<li><p></p>
<p class=noindent><tt>(get-host-name<i></i>) –> <i>string</i></tt><a name="node_idx_312"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Open-socket</tt> creates a new socket.
If no <i>port-number</i> is supplied the system picks one at random.
<tt>Socket-port-number</tt> returns a socket's port number.
<tt>Close-socket</tt> closes a socket, preventing any further connections.
<tt>Socket-accept</tt> accepts a single connection on <i>socket</i>, returning
an input port and an output port for communicating with the client.
If no client is waiting <tt>socket-accept</tt> blocks until one appears.
<tt>Get-host-name</tt> returns the network name of the machine.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(socket-client<i> host-name port-number</i>) –> <i>input-port output-port</i></tt><a name="node_idx_314"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Socket-client</tt> connects to the server at <i>port-number</i> on
the machine named <i>host-name</i>.
<tt>Socket-client</tt> blocks until the server accepts the connection.</p>
<p>
The following simple example shows a server and client for a centralized UID
service.
</p>
<pre class=verbatim>(define (id-server)
(let ((socket (open-socket)))
(display "Waiting on port ")
(display (socket-port-number socket))
(newline)
(let loop ((next-id 0))
(call-with-values
(lambda ()
(socket-accept socket))
(lambda (in out)
(display next-id out)
(close-input-port in)
(close-output-port out)
(loop (+ next-id 1)))))))
(define (get-id machine port-number)
(call-with-values
(lambda ()
(socket-client machine port-number))
(lambda (in out)
(let ((id (read in)))
(close-input-port in)
(close-output-port out)
id))))
</pre><p></p>
<p>
</p>
<a name="node_sec_5.18"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.18">5.18 Profiling</a></h2>
<p>The profiler can be used programmatically from within the code and
its results can be processed dynamically.</p>
<p>
The structure <tt>profiler</tt> offers basic data structures and functions.
All functions need a <tt>profile-data</tt> record argument that can be created with
<tt>make-empty-profile-data</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(make-empty-profile-data<i></i>) –> <i>profile-data</i></tt><a name="node_idx_316"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-thunk<i> profile-data thunk [interrupt-time [with-non-instr?]]</i>) –> <i>value</i></tt><a name="node_idx_318"></a></p>
</ul><p></p>
<p>
<tt>profile-thunk</tt> executes a thunk under the profiler. The data is stored in
the <i>profile-data</i> record passed as argument. Optionally the timeout for the profiler interrupt
can be passed (in milliseconds). The return value of <tt>profile-thunk</tt> is the return value
of <tt>thunk</tt>. By default, non-instrumented code will be profiled by the sampling process.
By passing <tt>with-non-instr? = #f</tt> only instrumented functions will be profiled.</p>
<p>
After profiling data, can be retrieved with the following accessors:</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(profile-data-starttime<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_320"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-endtime<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_322"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-runtime<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_324"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-memoryuse<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_326"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-gcruns<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_328"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-samples<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_330"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-data-interrupttime<i> profile-data</i>) –> <i>number</i></tt><a name="node_idx_332"></a></p>
</ul><p></p>
<p>
Times are in milliseconds, memory usage in bytes.</p>
<p>
The following functions produce the same output as the <tt>,profile</tt> command
(see section <a href="manual-Z-H-4.html#node_sec_3.6">3.6</a>).
They all take an optional <i>port</i> argument. Default is the <tt>current-output-port</tt>.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(profile-display<i> profile-data [port]</i>)</tt><a name="node_idx_334"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-display-overview<i> profile-data [port]</i>)</tt><a name="node_idx_336"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-display-flat<i> profile-data [port]</i>)</tt><a name="node_idx_338"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-display-tree<i> profile-data [port]</i>)</tt><a name="node_idx_340"></a></p>
</ul><p></p>
<p>
<tt>profile-display</tt> prints the full output of the profiler.
The other <tt>profile-display-<tt>...</tt></tt> functions only display the respective part of the output.</p>
<p>
The single fields in the flat profile can be retrieved with the following accessors:</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(profile-function-calls<i> profile-data names</i>)</tt><a name="node_idx_342"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-reccalls<i> profile-data names</i>)</tt><a name="node_idx_344"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-nonreccalls<i> profile-data names</i>)</tt><a name="node_idx_346"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-occurs<i> profile-data names</i>)</tt><a name="node_idx_348"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-hist<i> profile-data names</i>)</tt><a name="node_idx_350"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-memoryuse<i> profile-data names</i>)</tt><a name="node_idx_352"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-timeshare<i> profile-data names</i>)</tt><a name="node_idx_354"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-time-cumulative<i> profile-data names</i>)</tt><a name="node_idx_356"></a></p>
<li><p></p>
<p class=noindent><tt>(profile-function-time-self<i> profile-data names</i>)</tt><a name="node_idx_358"></a></p>
</ul><p></p>
<p>
Here <i>names</i> is the list of names specifying the function, optionally
with it's module. For example, <tt>names = ("dynamic-wind", "wind")</tt>
would specify the <tt>dynamic-wind</tt> function in the module <tt>wind</tt>, if
it was seen while profiling. If two or more functions match, the first one
is used. If no function matches, the functions return <tt>#{Unspecific}</tt>.</p>
<p>
The argument <i>names</i> can also be a plain string, as in
<tt>(profile-display-function-flat prof-data "module")</tt>. This will display
all flat function profiles that have “module” either as name or module.</p>
<p>
The meanings of the fields that the functions return are as follows:
</p>
<ul>
<li><p><tt>calls</tt>:
total number of calls (recursive and non-recursive) to the function
</p>
<li><p><tt>reccalls</tt>:
total number of recursive calls to the function
</p>
<li><p><tt>nonreccalls</tt>:
total number of non-recursive calls to the function
</p>
<li><p><tt>occurs</tt>:
number of times the function was seen on stack while profiling
</p>
<li><p><tt>hist</tt>:
number of times the function was seen running while profiling
</p>
<li><p><tt>memoryuse</tt>:
bytes of memory used by the function
</p>
<li><p><tt>timeshare</tt>:
percentage of time used by the function itself (number from 0 to 1)
</p>
<li><p><tt>time-cumulative</tt>:
total time in ms the function was on call-stack
</p>
<li><p><tt>time-self</tt>:
total time in ms the function actively running
</p>
</ul><p></p>
<p>
The following shows a short example of the usage of the profiler interface, where <tt>main</tt>
is the function to be profiled:
</p>
<pre class=verbatim>(define prof-data (make-empty-profile-data))
(profile-thunk prof-data (lambda () (main 22)))
(display "Samples: ")
(display (profile-data-samples prof-data))
(newline)
(profile-display-overview prof-data)
(profile-display-flat prof-data (current-output-port))
(profile-display-tree prof-data)
; print only function "a"
(profile-display-function-flat prof-data '("a"))
; print only function "a" in file "x.scm"
(profile-display-function-flat prof-data '("a" "x.scm"))
; print all profiled functions in file "x.scm"
(profile-display-function-flat prof-data "x.scm")
; print percentage of time "a" was running
(display (* (profile-function-timeshare prof-data '("a")) 100))
</pre><p></p>
<p>
</p>
<p>
</p>
<a name="node_sec_5.19"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19">5.19 Macros for writing loops</a></h2>
<p></p>
<p>
<tt>Iterate</tt> and <tt>reduce</tt> are extensions of named-<tt>let</tt> for
writing loops that walk down one or more sequences,
such as the elements of a list or vector, the
characters read from a port, or an arithmetic series.
Additional sequences can be defined by the user.
<tt>Iterate</tt> and <tt>reduce</tt> are in structure <tt>reduce</tt>.</p>
<p>
</p>
<a name="node_sec_5.19.1"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.1">5.19.1 <tt>Iterate</tt></a></h3>
<p>The syntax of <tt>iterate</tt> is:
</p>
<pre class=verbatim> (iterate <i>loop-name</i>
((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
<tt>...</tt>)
((<i>state-variable</i> <i>initial-value</i>)
<tt>...</tt>)
<i>body-expression</i>
[<i>final-expression</i>])
</pre><p></p>
<p>
<tt>Iterate</tt> steps the <i>element-variable</i>s in parallel through the
sequences, while each <i>state-variable</i> has the corresponding
<i>initial-value</i> for the first iteration and have later values
supplied by <i>body-expression</i>.
If any sequence has reached its limit the value of the <tt>iterate</tt>
expression is
the value of <i>final-expression</i>, if present, or the current values of
the <i>state-variable</i>s, returned as multiple values.
If no sequence has reached
its limit, <i>body-expression</i> is evaluated and either calls <i>loop-name</i> with
new values for the <i>state-variable</i>s, or returns some other value(s).</p>
<p>
The <i>loop-name</i> and the <i>state-variable</i>s and <i>initial-value</i>s behave
exactly as in named-<tt>let</tt>. The named-<tt>let</tt> expression
</p>
<pre class=verbatim> (let loop-name ((state-variable initial-value) ...)
body ...)
</pre><p>
is equivalent to an <tt>iterate</tt> expression with no sequences
(and with an explicit
<tt>let</tt> wrapped around the body expressions to take care of any
internal <tt>define</tt>s):
</p>
<pre class=verbatim> (iterate loop-name
()
((state-variable initial-value) ...)
(let () body ...))
</pre><p></p>
<p>
The <i>sequence-type</i>s are keywords (they are actually macros of a particular
form; it is easy to add additional types of sequences).
Examples are <tt>list*</tt> which walks down the elements of a list and
<tt>vector*</tt> which does the same for vectors.
For each iteration, each <i>element-variable</i> is bound to the next
element of the sequence.
The <i>sequence-data</i> gives the actual list or vector or whatever.</p>
<p>
If there is a <i>final-expression</i>, it is evaluated when the end of one or more
sequences is reached.
If the <i>body-expression</i> does not call <i>loop-name</i> the
<i>final-expression</i> is not evaluated.
The <i>state-variable</i>s are visible in
<i>final-expression</i> but the <i>sequence-variable</i>s are not. </p>
<p>
The <i>body-expression</i> and the <i>final-expression</i> are in tail-position within
the <tt>iterate</tt>.
Unlike named-<tt>let</tt>, the behavior of a non-tail-recursive call to
<i>loop-name</i> is unspecified (because iterating down a sequence may involve side
effects, such as reading characters from a port).</p>
<p>
</p>
<a name="node_sec_5.19.2"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.2">5.19.2 <tt>Reduce</tt></a></h3>
<p>If an <tt>iterate</tt> expression is not meant to terminate before a sequence
has reached its end,
<i>body-expression</i> will always end with a tail call to <i>loop-name</i>.
<tt>Reduce</tt> is a macro that makes this common case explicit.
The syntax of <tt>reduce</tt> is
the same as that of <tt>iterate</tt>, except that there is no <i>loop-name</i>.
The <i>body-expression</i> returns new values of the <i>state-variable</i>s
instead of passing them to <i>loop-name</i>.
Thus <i>body-expression</i> must return as many values as there are state
variables.
By special dispensation, if there are
no state variables then <i>body-expression</i> may return any number of values,
all of which are ignored.</p>
<p>
The syntax of <tt>reduce</tt> is:
</p>
<pre class=verbatim> (reduce ((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
<tt>...</tt>)
((<i>state-variable</i> <i>initial-value</i>)
<tt>...</tt>)
<i>body-expression</i>
[<i>final-expression</i>])
</pre><p></p>
<p>
The value(s) returned by an instance of <tt>reduce</tt> is the value(s) returned
by the <i>final-expression</i>, if present, or the current value(s) of the state
variables when the end of one or more sequences is reached.</p>
<p>
A <tt>reduce</tt> expression can be rewritten as an equivalent <tt>iterate</tt>
expression by adding a <i>loop-var</i> and a wrapper for the
<i>body-expression</i> that calls the <i>loop-var</i>.
</p>
<pre class=verbatim>(iterate loop
((<i>sequence-type</i> <i>element-variable</i> <i>sequence-data</i> <tt>...</tt>)
<tt>...</tt>)
((<i>state-variable</i> <i>initial-value</i>)
<tt>...</tt>)
(call-with-values (lambda ()
<i>body-expression</i>)
loop)
[<i>final-expression</i>])
</pre><p></p>
<p>
</p>
<a name="node_sec_5.19.3"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.3">5.19.3 Sequence types</a></h3>
<p>The predefined sequence types are:
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(list* <i>elt-var</i> <i>list</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(list-spine* <i>elt-var</i> <i>list</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(list-spine-cycle-safe* <i>elt-var</i> <i>list</i> <i>on-cycle-thunk</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(vector* <i>elt-var</i> <i>vector</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(string* <i>elt-var</i> <i>string</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(count* <i>elt-var</i> <i>start</i> [<i>end</i> [<i>step</i>]])</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(bits* <i>elt-var</i> <i>i</i> [<i>size</i>])</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(input* <i>elt-var</i> <i>input-port</i> <i>read-procedure</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(stream* <i>elt-var</i> <i>procedure</i> <i>initial-data</i>)</tt> (syntax)
</p>
</ul><p></p>
<p>
For lists, vectors, and strings the element variable is bound to the
successive elements of the list or vector, or the characters in the
string.</p>
<p>
For <tt>list-spine*</tt> the element variable is bound to the successive
pairs in the spine of the list.
<tt>List-spine-cycle-safe*</tt> is similar, but calls <i>on-cycle-thunk</i>
with no arguments and with the continuation of the loop macro at an
unspecified time if <i>list</i> contains a cycle.</p>
<p>
For <tt>count*</tt> the element variable is bound to the elements of the sequence
</p>
<pre class=verbatim> <i>start</i>, <i>start</i> + <i>step</i>, <i>start</i> + 2<i>step</i>, <tt>...</tt>, <i>end</i>
</pre><p>
inclusive of <i>start</i> and exclusive of <i>end</i>.
The default <i>step</i> is 1.
The sequence does not terminate if no <i>end</i> is given or if there
is no <em>N</em> > 0 such that <i>end</i> = <i>start</i> + N<i>step</i>
(<tt>=</tt> is used to test for termination).
For example, <tt>(count* i 0 -1)</tt> doesn't terminate
because it begins past the <i>end</i> value and <tt>(count* i 0 1 2)</tt> doesn't
terminate because it skips over the <i>end</i> value.</p>
<p>
For <tt>bits*</tt>, the element variable is bound to a sequence of
representations of successive bit-fields of <i>i</i>, from least to most
significant.
If <i>size</i> is present, it must be a positive exact integer, and the
element variable is bound to a sequence of <i>size</i>-bit integers.
If <i>size</i> is omitted, <tt>bits*</tt> iterates through single bits, and the
element variable is bound to a sequence of booleans. <tt>#t</tt> represents 1,
and <tt>#f</tt> represents 0.</p>
<p>
For <tt>input*</tt> the elements are the results of successive applications
of <i>read-procedure</i> to <i>input-port</i>.
The sequence ends when <i>read-procedure</i> returns an end-of-file object.</p>
<p>
For a stream, the <i>procedure</i> takes the current data value as an argument
and returns two values, the next value of the sequence and a new data value.
If the new data is <tt>#f</tt> then the previous element was the last
one. For example,
</p>
<pre class=verbatim> (list* elt my-list)
</pre><p>
is the same as
</p>
<pre class=verbatim> (stream* elt list->stream my-list)
</pre><p>
where <tt>list->stream</tt> is
</p>
<pre class=verbatim> (lambda (list)
(if (null? list)
(values 'ignored #f)
(values (car list) (cdr list))))
</pre><p></p>
<p>
</p>
<a name="node_sec_5.19.4"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.4">5.19.4 Synchronous sequences</a></h3>
<p>When using the sequence types described above, a loop terminates when any of
its sequences reaches its end. To help detect bugs it is useful to have
sequence types that check to see if two or more sequences end on the same
iteration. For this purpose there is second set of sequence types called
synchronous sequences. These are identical to the ones listed above except
that they cause an error to be signalled if a loop is terminated by a
synchronous sequence and some other synchronous sequence did not reach its
end on the same iteration.</p>
<p>
Sequences are checked for termination in order, from left to right, and
if a loop is terminated by a non-synchronous sequence no further checking
is done.</p>
<p>
The synchronous sequences are:</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(list% <i>elt-var</i> <i>list</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(list-spine% <i>elt-var</i> <i>list</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(list-spine-cycle-safe% <i>elt-var</i> <i>list</i> <i>on-cycle-thunk</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(vector% <i>elt-var</i> <i>vector</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(string% <i>elt-var</i> <i>string</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(count% <i>elt-var</i> <i>start</i> <i>end</i> [<i>step</i>])</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(input% <i>elt-var</i> <i>input-port</i> <i>read-procedure</i>)</tt> (syntax)
</p>
<li><p></p>
<p class=noindent><tt>(stream% <i>elt-var</i> <i>procedure</i> <i>initial-data</i>)</tt> (syntax)
</p>
</ul><p></p>
<p>
Note that the synchronous <tt>count%</tt> must have an <i>end</i>, unlike the
nonsynchronous <tt>count*</tt>.</p>
<p>
</p>
<a name="node_sec_5.19.5"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.5">5.19.5 Examples</a></h3>
<p></p>
<p class=noindent>Gathering the indexes of list elements that answer true to some
predicate.
</p>
<pre class=verbatim>(lambda (my-list predicate)
(reduce ((list* elt my-list)
(count* i 0))
((hits '()))
(if (predicate elt)
(cons i hits)
hits)
(reverse hits))
</pre><p></p>
<p>
</p>
<p class=noindent>Looking for the index of an element of a list.
</p>
<pre class=verbatim>(lambda (my-list predicate)
(iterate loop
((list* elt my-list)
(count* i 0))
() ; no state
(if (predicate elt)
i
(loop))))
</pre><p></p>
<p>
</p>
<p class=noindent>Reading one line.
</p>
<pre class=verbatim>(define (read-line port)
(iterate loop
((input* c port read-char))
((chars '()))
(if (char=? c #<code class=verbatim>\</code>newline)
(list->string (reverse chars))
(loop (cons c chars)))
(if (null? chars)
(eof-object)
; no newline at end of file
(list->string (reverse chars)))))
</pre><p></p>
<p>
</p>
<p class=noindent>Counting the lines in a file. We can't use <tt>count*</tt> because we
need the value of the count after the loop has finished.
</p>
<pre class=verbatim>(define (line-count name)
(call-with-input-file name
(lambda (in)
(reduce ((input* l in read-line))
((i 0))
(+ i 1)))))
</pre><p></p>
<p>
</p>
<a name="node_sec_5.19.6"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.6">5.19.6 Defining sequence types</a></h3>
<p>The sequence types are object-oriented macros similar to enumerations.
A non-synchronous sequence macro needs to supply three values:
<tt>#f</tt> to indicate that it isn't synchronous, a list of state variables
and their initializers, and the code for one iteration.
The first
two methods are CPS'ed: they take another macro and argument to
which to pass their result.
The <tt>sync</tt> method gets no additional arguments.
The <tt>state-vars</tt> method is passed a list of names which
will be bound to the arguments to the sequence.
The final method, for the step, is passed the list of names bound to
the arguments and the list of state variables.
In addition there is
a variable to be bound to the next element of the sequence, the
body expression for the loop, and an expression for terminating the
loop.</p>
<p>
The definition of <tt>list*</tt> is
</p>
<pre class=verbatim>(define-syntax list*
(syntax-rules (sync state-vars step)
((list* sync (next more))
(next #f more))
((list* state-vars (start-list) (next more))
(next ((list-var start-list)) more))
((list* step (start-list) (list-var)
value-var loop-body final-exp)
(if (null? list-var)
final-exp
(let ((value-var (car list-var))
(list-var (cdr list-var)))
loop-body)))))
</pre><p></p>
<p>
Synchronized sequences are the same, except that they need to
provide a termination test to be used when some other synchronized
method terminates the loop.
</p>
<pre class=verbatim>(define-syntax list%
(syntax-rules (sync done)
((list% sync (next more))
(next #t more))
((list% done (start-list) (list-var))
(null? list-var))
((list% stuff ...)
(list* stuff ...))))
</pre><p></p>
<p>
</p>
<a name="node_sec_5.19.7"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.19.7">5.19.7 Expanded code</a></h3>
<p>The expansion of
</p>
<pre class=verbatim> (reduce ((list* x '(1 2 3)))
((r '()))
(cons x r))
</pre><p>
is
</p>
<pre class=verbatim> (let ((final (lambda (r) (values r)))
(list '(1 2 3))
(r '()))
(let loop ((list list) (r r))
(if (null? list)
(final r)
(let ((x (car list))
(list (cdr list)))
(let ((continue (lambda (r)
(loop list r))))
(continue (cons x r)))))))
</pre><p></p>
<p>
The only inefficiencies in this code are the <tt>final</tt> and <tt>continue</tt>
procedures, both of which could be substituted in-line.
The macro expander could do the substitution for <tt>continue</tt> when there
is no explicit proceed variable, as in this case, but not in general.</p>
<p>
</p>
<a name="node_sec_5.20"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20">5.20 Sorting lists and vectors</a></h2>
<p></p>
<p>
(This section, as the libraries it describes, was written mostly by
Olin Shivers for the draft of SRFI 32.)</p>
<p>
The sort libraries in Scheme 48 include
</p>
<ul>
<li><p>vector insert sort (stable)
</p>
<li><p>vector heap sort
</p>
<li><p>vector quick sort (with regular comparisons and with median-of-3 pivot picking)
</p>
<li><p>vector merge sort (stable)
</p>
<li><p>pure and destructive list merge sort (stable)
</p>
<li><p>stable vector and list merge
</p>
<li><p>miscellaneous sort-related procedures: vector and list merging,
sorted predicates, vector binary search, vector and list
delete-equal-neighbor procedures.
</p>
<li><p>a general, non-algorithmic set of procedure names for general sorting
and merging
</p>
</ul><p></p>
<p>
</p>
<a name="node_sec_5.20.1"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20.1">5.20.1 Design rules</a></h3>
<p></p>
<a name="node_sec_Temp_5"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_5">What vs. how</a></h5>
<p>There are two different interfaces: “what” (simple) and “how” (detailed).</p>
<p>
</p>
<dl><dt></dt><dd>
</dd><dt><b>Simple</b></dt><dd> you specify semantics: datatype (list or vector),
mutability, and stability.<p>
</p>
</dd><dt><b>Detailed</b></dt><dd> you specify the actual algorithm (quick, heap,
insert, merge). Different algorithms have different properties,
both semantic and pragmatic, so these exports are necessary.<p>
It is necessarily the case that the specifications of these procedures
make statements about execution “pragmatics.” For example, the sole
distinction between heap sort and quick sort—both of which are
provided by this library—-is one of execution time, which is not a
“semantic” distinction. Similar resource-use statements are made about
“iterative” procedures, meaning that they can execute on input of
arbitrary size in a constant number of stack frames.
</p>
</dd></dl><p></p>
<p>
</p>
<a name="node_sec_Temp_6"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_6">Consistency across procedure signatures</a></h5>
<p>The two interfaces share common procedure signatures wherever
possible, to facilitate switching a given call from one procedure
to another.</p>
<p>
</p>
<a name="node_sec_Temp_7"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_7">Less-than parameter first, data parameter after</a></h5>
<p>These procedures uniformly observe the following parameter order:
the data to be sorted comes after the comparison procedure.
That is, we write</p>
<p>
</p>
<pre class=verbatim> (sort < <i>list</i>)
</pre><p></p>
<p>
not</p>
<p>
</p>
<pre class=verbatim> (sort <i>list</i> <)
</pre><p>
</p>
<p>
</p>
<a name="node_sec_Temp_8"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_8">Ordering, comparison procedures and stability</a></h5>
<p>These routines take a < comparison procedure, not a ≤ comparison
procedure, and they sort into increasing order. The difference between
a < spec and a ≤ spec comes up in two places: </p>
<p>
</p>
<ul>
<li><p>the definition of an ordered or sorted data set, and
</p>
<li><p>the definition of a stable sorting algorithm.
</p>
</ul><p>
</p>
<p>
We say that a data set (a list or vector) is <i>sorted</i> or
<i>ordered</i> if it contains no adjacent pair of values <tt>...</tt> <em>x</em>,
<em>y</em> <tt>...</tt> such that <em>y</em> < <em>x</em>.</p>
<p>
In other words, scanning across the data never takes a “downwards” step.</p>
<p>
If you use a ≤ procedure where these algorithms expect a <
procedure, you may not get the answers you expect. For example,
the <tt>list-sorted?</tt> procedure will return false if you pass it a ≤ comparison
procedure and an ordered list containing adjacent equal elements.</p>
<p>
A “stable” sort is one that preserves the pre-existing order of equal
elements. Suppose, for example, that we sort a list of numbers by
comparing their absolute values, i.e., using comparison procedure
</p>
<pre class=verbatim>(lambda (x y) (< (abs x) (abs y)))
</pre><p>
If we sort a list that contains both 3 and -3: </p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center><tt>...</tt> 3, <tt>...</tt>, −3 <tt>...</tt></td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
then a stable sort is an algorithm that will not swap the order
of these two elements, that is, the answer is guaranteed to to look like
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center><tt>...</tt> 3, −3 <tt>...</tt></td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
not
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center><tt>...</tt> −3, 3 <tt>...</tt></td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
Choosing < for the comparison procedure instead of ≤ affects
how stability is coded. Given an adjacent pair <em>x</em>, <em>y</em>, <tt>(<
<em>y</em> <em>x</em>)</tt> means “<em>x</em> should be moved in front of <em>x</em>”—otherwise,
leave things as they are. So using a ≤ procedure where a <
procedure is expected will <em>invert</em> stability.</p>
<p>
This is due to the definition of equality, given a < comparator:
</p>
<pre class=verbatim> (and (not (< x y))
(not (< y x)))
</pre><p>
The definition is rather different, given a ≤ comparator:
</p>
<pre class=verbatim> (and (<= x y)
(<= y x))
</pre><p>
A “stable” merge is one that reliably favors one of its data sets
when equal items appear in both data sets. <em>All merge operations in
this library are stable</em>, breaking ties between data sets in favor
of the first data set—elements of the first list come before equal
elements in the second list.</p>
<p>
So, if we are merging two lists of numbers ordered by absolute value,
the stable merge operation <tt>list-merge</tt>
</p>
<pre class=verbatim> (list-merge (lambda (x y) (< (abs x) (abs y)))
'(0 -2 4 8 -10) '(-1 3 -4 7))
</pre><p>
reliably places the 4 of the first list before the equal-comparing -4
of the second list:
</p>
<pre class=verbatim> (0 -1 -2 4 -4 7 8 -10)
</pre><p>
Some sort algorithms <em>will not work correctly</em> if given a ≤
when they expect a < comparison (or vice-versa).</p>
<p>
</p>
<p>
In short, if your comparison procedure <em>f</em> answers true to <tt>(<em>f</em> x x)</tt>, then
</p>
<ul>
<li><p>using a stable sorting or merging algorithm will not give you a
stable sort or merge,
</p>
<li><p><tt>list-sorted?</tt> may surprise you.
</p>
</ul><p>
Note that you can synthesize a < procedure from a ≤ procedure with
</p>
<pre class=verbatim> (lambda (x y) (not (<= y x)))
</pre><p>
if need be. </p>
<p>
Precise definitions give sharp edges to tools, but require care in use.
“Measure twice, cut once.”</p>
<p>
</p>
<p>
</p>
<a name="node_sec_Temp_9"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_9">All vector operations accept optional subrange parameters</a></h5>
<p>The vector operations specified below all take optional
<tt>start</tt>/<tt>end</tt> arguments indicating a selected subrange
of a vector's elements. If a <tt>start</tt> parameter or
<tt>start</tt>/<tt>end</tt> parameter pair is given to such a
procedure, they must be exact, non-negative integers, such that
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center>
0 ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ <tt></td><td><table><tr><td align=center></td><td><table><tr><td align=center>(vector-length </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>vector</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>)</td></tr></table></td><td></td></tr></table></td><td></tt>
</td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
where <i>vector</i> is the related vector parameter. If not specified,
they default to 0 and the length of the vector, respectively. They are
interpreted to select the range [<i>start</i>,<i>end</i>), that
is, all elements from index <i>start</i> (inclusive) up to, but not
including, index <i>end</i>.</p>
<p>
</p>
<a name="node_sec_Temp_10"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_10">Required vs. allowed side-effects</a></h5>
<p><tt>List-sort!</tt> and <tt>List-stable-sort!</tt> are allowed, but
not required, to alter their arguments' cons cells to construct the
result list. This is consistent with the what-not-how character of the
group of procedures to which they belong (the <tt>sorting</tt> structure).</p>
<p>
The <tt>list-delete-neighbor-dups!</tt>, <tt>list-merge!</tt> and
<tt>list-merge-sort!</tt> procedures, on the other hand, provide
specific algorithms, and, as such, explicitly commit to the use of
side-effects on their input lists in order to guarantee their key
algorithmic properties (e.g., linear-time operation).</p>
<p>
</p>
<a name="node_sec_5.20.2"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20.2">5.20.2 Procedure specification</a></h3>
<p></p>
<div align=center><table><tr><td>
<table border=0><tr><td valign=top >Structure name </td><td valign=top >Functionality</td></tr>
<tr><td valign=top ><tt>sorting</tt> </td><td valign=top >General sorting for lists and vectors</td></tr>
<tr><td valign=top ><tt>sorted</tt> </td><td valign=top >Sorted predicates for lists and vectors</td></tr>
<tr><td valign=top ><tt>list-merge-sort</tt></td><td valign=top >List merge sort</td></tr>
<tr><td valign=top ><tt>vector-merge-sort</tt> </td><td valign=top >Vector merge sort</td></tr>
<tr><td valign=top ><tt>vector-heap-sort</tt> </td><td valign=top >Vector heap sort</td></tr>
<tr><td valign=top ><tt>vector-quick-sort</tt> </td><td valign=top >Vector quick sort</td></tr>
<tr><td valign=top ><tt>vector-quick-sort3</tt> </td><td valign=top >Vector quick sort with 3-way comparisons</td></tr>
<tr><td valign=top ><tt>vector-insert-sort</tt> </td><td valign=top >Vector insertion sort</td></tr>
<tr><td valign=top ><tt>delete-neighbor-duplicates</tt> </td><td valign=top >List and vector delete neighbor duplicates</td></tr>
<tr><td valign=top ><tt>binary-searches</tt> </td><td valign=top >Vector binary search
</td></tr></table>
</td></tr></table></div>
Note that there is no “list insert sort” package, as you might as well always
use list merge sort. The reference implementation's destructive list merge
sort will do fewer <tt>set-cdr!</tt>s than a destructive insert sort.<p>
</p>
<a name="node_sec_Temp_11"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_11">Procedure naming and functionality</a></h5>
<p>Almost all of the procedures described below are variants of two basic
operations: sorting and merging. These procedures are consistently named
by composing a set of basic lexemes to indicate what they do.
</p>
<div align=center><table><tr><td>
</td></tr><tr><td>
<p>
</p>
<table border=0><tr><td valign=top >Lexeme </td><td valign=top >Meaning</td></tr>
<tr><td valign=top ><tt>sort</tt></td><td valign=top >The procedure sorts its input data set by some < comparison procedure.
</td></tr>
<tr><td valign=top ><tt>merge</tt></td><td valign=top >The procedure merges two ordered data sets into a single ordered
result.
</td></tr>
<tr><td valign=top ><tt>stable</tt> </td><td valign=top >This lexeme indicates that the sort is a stable one.
</td></tr>
<tr><td valign=top ><tt>vector</tt></td><td valign=top >The procedure operates upon vectors.
</td></tr>
<tr><td valign=top ><tt>list</tt> </td><td valign=top >The procedure operates upon lists.
</td></tr>
<tr><td valign=top ><tt>!</tt> </td><td valign=top >Procedures that end in <tt>!</tt> are allowed, and sometimes required,
to reuse their input storage to construct their answer.
</td></tr></table>
</td></tr></table></div>
<p>
</p>
<a name="node_sec_Temp_12"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_12">Types of parameters and return values</a></h5>
<p>In the procedures specified below,
</p>
<ul>
<li><p>A <tt><</tt> or <tt>=</tt> parameter is a procedure accepting
two arguments taken from the specified procedure's data set(s), and
returning a boolean;
</p>
<li><p><tt>Start</tt> and <tt>end</tt> parameters are exact, non-negative integers that
serve as vector indices selecting a subrange of some associated vector.
When specified, they must satisfy the relation
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center>
0 ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> ≤ <tt></td><td><table><tr><td align=center></td><td><table><tr><td align=center>(vector-length </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>vector</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>)</td></tr></table></td><td></td></tr></table></td><td></tt>
</td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
where <i>vector</i> is the associated vector.
</p>
</ul><p>
Passing values to procedures with these parameters that do not satisfy
these types is an error.</p>
<p>
If a procedure is said to return “unspecified,” this means that
nothing at all is said about what the procedure returns, not even the
number of return values. Such a procedure is not even required to be
consistent from call to call in the nature or number of its return
values. It is simply required to return a value (or values) that may
be passed to a command continuation, e.g. as the value of an
expression appearing as a non-terminal subform of a <tt>begin</tt>
expression. Note that in R<sup>5</sup>RS, this restricts such a procedure to
returning a single value; non-R<sup>5</sup>RS systems may not even provide this
restriction.</p>
<p>
</p>
<a name="node_sec_5.20.2.1"></a>
<h4 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20.2.1">5.20.2.1 <tt>sorting</tt>—general sorting package</a></h4>
<p>This library provides basic sorting and merging functionality suitable for
general programming. The procedures are named by their semantic properties,
i.e., what they do to the data (sort, stable sort, merge, and so forth).</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(list-sorted?<i> < list</i>) –> <i>boolean</i></tt><a name="node_idx_360"></a></p>
<li><p></p>
<p class=noindent><tt>(list-merge<i> < list<sub>1</sub> list<sub>2</sub></i>) –> <i>list</i></tt><a name="node_idx_362"></a></p>
<li><p></p>
<p class=noindent><tt>(list-merge!<i> < list<sub>1</sub> list<sub>2</sub></i>) –> <i>list</i></tt><a name="node_idx_364"></a></p>
<li><p></p>
<p class=noindent><tt>(list-sort<i> < lis</i>) –> <i>list</i></tt><a name="node_idx_366"></a></p>
<li><p></p>
<p class=noindent><tt>(list-sort!<i> < lis</i>) –> <i>list</i></tt><a name="node_idx_368"></a></p>
<li><p></p>
<p class=noindent><tt>(list-stable-sort<i> < list</i>) –> <i>list</i></tt><a name="node_idx_370"></a></p>
<li><p></p>
<p class=noindent><tt>(list-stable-sort!<i> < list</i>) –> <i>list</i></tt><a name="node_idx_372"></a></p>
<li><p></p>
<p class=noindent><tt>(list-delete-neighbor-dups<i> = list</i>) –> <i>list</i></tt><a name="node_idx_374"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sorted?<i> < v [start [end]]</i>) –> <i>boolean</i></tt><a name="node_idx_376"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-merge<i> < v<sub>1</sub> v<sub>2</sub> [start1 [end1 [start2 [end2]]]]</i>) –> <i>vector</i></tt><a name="node_idx_378"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-merge!<i> < v v<sub>1</sub> v<sub>2</sub> [start [start1 [end1 [start2 [end2]]]]]</i>)</tt><a name="node_idx_380"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sort<i> < v [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_382"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sort!<i> < v [start [end]]</i>)</tt><a name="node_idx_384"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-stable-sort<i> < v [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_386"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-stable-sort!<i> < v [start [end]]</i>)</tt><a name="node_idx_388"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-delete-neighbor-dups<i> = v [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_390"></a></p>
</ul><p></p>
<p>
</p>
<div align=center><table><tr><td>
<table border=0><tr><td valign=top >Procedure </td><td valign=top >Suggested algorithm
</td></tr>
<tr><td valign=top ><tt>list-sort</tt> </td><td valign=top >vector heap or quick</td></tr>
<tr><td valign=top ><tt>list-sort!</tt> </td><td valign=top >list merge sort</td></tr>
<tr><td valign=top ><tt>list-stable-sort</tt> </td><td valign=top >vector merge sort</td></tr>
<tr><td valign=top ><tt>list-stable-sort!</tt> </td><td valign=top >list merge sort</td></tr>
<tr><td valign=top ><tt>vector-sort</tt> </td><td valign=top >heap or quick sort</td></tr>
<tr><td valign=top ><tt>vector-sort!</tt> </td><td valign=top >heap or quick sort</td></tr>
<tr><td valign=top ><tt>vector-stable-sort</tt> </td><td valign=top >vector merge sort</td></tr>
<tr><td valign=top ><tt>vector-stable-sort!</tt> merge sort
</td></tr></table>
</td></tr></table></div>
<tt>List-Sorted?</tt> and <tt>vector-sorted?</tt> return true if their
input list or vector is in sorted order, as determined by their <i><</i>
comparison parameter.<p>
All four merge operations are stable: an element of the initial list
<i>list<sub>1</sub></i> or vector <i>vector<sub>1</sub></i> will come before an
equal-comparing element in the second list <i>list<sub>2</sub></i> or vector
<i>vector<sub>2</sub></i> in the result.</p>
<p>
The procedures
</p>
<ul>
<li><p><tt>list-merge</tt>
</p>
<li><p><tt>list-sort</tt>
</p>
<li><p><tt>list-stable-sort</tt>
</p>
<li><p><tt>list-delete-neighbor-dups</tt>
</p>
</ul><p>
do not alter their inputs and are allowed to return a value that shares
a common tail with a list argument.</p>
<p>
The procedure
</p>
<ul>
<li><p><tt>list-sort!</tt>
</p>
<li><p><tt>list-stable-sort!</tt>
</p>
</ul><p>
are “linear update” operators—they are allowed, but not required, to
alter the cons cells of their arguments to produce their results. </p>
<p>
On the other hand, the <tt>list-merge!</tt> procedure
make only a single, iterative, linear-time pass over its argument
list, using <tt>set-cdr!</tt>s to rearrange the cells of the list
into the final result —it works “in place.” Hence, any cons cell
appearing in the result must have originally appeared in an input. The
intent of this iterative-algorithm commitment is to allow the
programmer to be sure that if, for example, <tt>list-merge!</tt> is asked to
merge two ten-million-element lists, the operation will complete
without performing some extremely (possibly twenty-million) deep
recursion.</p>
<p>
The vector procedures
</p>
<ul>
<li><p><tt>vector-sort</tt>
</p>
<li><p><tt>vector-stable-sort</tt>
</p>
<li><p><tt>vector-delete-neighbor-dups</tt>
</p>
</ul><p>
do not alter their inputs, but allocate a fresh vector for their result,
of length <i>end</i> − <i>start</i>. </p>
<p>
The vector procedures
</p>
<ul>
<li><p><tt>vector-sort!</tt>
</p>
<li><p><tt>vector-stable-sort!</tt>
</p>
</ul><p>
sort their data in-place. (But note that <tt>vector-stable-sort!</tt>
may allocate temporary storage proportional to the size of the
input
.)</p>
<p>
<tt>Vector-merge</tt> returns a vector of length (<i>end<sub>1</sub></i>−<i>start<sub>1</sub></i> + (<i>end<sub>2</sub></i>−<i>start<sub>2</sub></i>).</p>
<p>
<tt>Vector-merge!</tt> writes its result into vector <i>v</i>,
beginning at index <i>start</i>, for indices less than <i>end</i> =
<i>start</i> + (<i>end<sub>1</sub></i>−<i>start<sub>1</sub></i>) +
(<i>end<sub>2</sub></i>−<i>start<sub>2</sub></i>). The target subvector
<i>v</i>[<i>start</i>,<i>end</i>) may not overlap either source
subvector <i>vector<sub>1</sub></i>[<i>start<sub>1</sub></i>,<i>end<sub>1</sub></i>) <i>vector<sub>2</sub></i>[<i>start<sub>2</sub></i>,<i>end<sub>2</sub></i>).</p>
<p>
The <tt><tt>...</tt>-delete-neighbor-dups-<tt>...</tt></tt> procedures:
These procedures delete adjacent duplicate elements from a list or a
vector, using a given element-equality procedure. The first/leftmost
element of a run of equal elements is the one that survives. The list or
vector is not otherwise disordered.</p>
<p>
These procedures are linear time—much faster than the <em>O</em>(<em>n</em><sup>2</sup>) general
duplicate-element deletors that do not assume any “bunching” of elements
(such as the ones provided by SRFI 1). If you want to delete duplicate
elements from a large list or vector, you can sort the elements to bring
equal items together, then use one of these procedures, for a total time
of <em>O</em>(<em>n</em>log(<em>n</em>)).</p>
<p>
The comparison procedure = passed to these procedures is always
applied
<tt>( = <em>x</em> <em>y</em>)</tt>
where <em>x</em> comes before <em>y</em> in the containing list or vector.</p>
<p>
</p>
<ul>
<li><p><tt>List-delete-neighbor-dups</tt> does not alter its input list; its answer
may share storage with the input list.
</p>
<li><p><tt>Vector-delete-neighbor-dups</tt> does not alter its input vector, but
rather allocates a fresh vector to hold the result.
</p>
</ul><p>
Examples:</p>
<p>
</p>
<pre class=verbatim>(list-delete-neighbor-dups = '(1 1 2 7 7 7 0 -2 -2))
===⇒ (1 2 7 0 -2)
(vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2))
===⇒ #(1 2 7 0 -2)
(vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2) 3 7)
===⇒ #(7 0 -2)
</pre><p></p>
<p>
</p>
<a name="node_sec_5.20.2.2"></a>
<h4 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20.2.2">5.20.2.2 Algorithm-specific sorting packages</a></h4>
<p>These packages provide more specific sorting functionality, that is,
specific commitment to particular algorithms that have particular
pragmatic consequences (such as memory locality, asymptotic running time)
beyond their semantic behaviour (sorting, stable sorting, merging, etc.).
Programmers that need a particular algorithm can use one of these packages.</p>
<p>
</p>
<a name="node_sec_Temp_13"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_13"><tt>sorted</tt>—sorted predicates</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(list-sorted?<i> < list</i>) –> <i>boolean</i></tt><a name="node_idx_392"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sorted?<i> < vector</i>) –> <i>boolean</i></tt><a name="node_idx_394"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sorted?<i> < vector start</i>) –> <i>boolean</i></tt><a name="node_idx_396"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-sorted?<i> < vector start end</i>) –> <i>boolean</i></tt><a name="node_idx_398"></a></p>
</ul><p></p>
<p>
Return <tt>#f</tt> iff there is an adjacent pair <tt>...</tt> <em>x</em>, <em>y</em> <tt>...</tt> in the input
list or vector such that <em>y</em> < <em>x</em>. The optional <i>start</i>/<i>end</i> range
arguments restrict <tt>vector-sorted?</tt> to the indicated subvector.</p>
<p>
</p>
<a name="node_sec_Temp_14"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_14"><tt>list-merge-sort</tt>—list merge sort</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(list-merge-sort<i> < list</i>) –> <i>list</i></tt><a name="node_idx_400"></a></p>
<li><p></p>
<p class=noindent><tt>(list-merge-sort!<i> < list</i>) –> <i>list</i></tt><a name="node_idx_402"></a></p>
<li><p></p>
<p class=noindent><tt>(list-merge<i> list<sub>1</sub> < list<sub>2</sub></i>) –> <i>list</i></tt><a name="node_idx_404"></a></p>
<li><p></p>
<p class=noindent><tt>(list-merge!<i> list<sub>1</sub> < list<sub>2</sub></i>) –> <i>list</i></tt><a name="node_idx_406"></a></p>
</ul><p>
The sort procedures sort their data using a list merge sort, which is
stable. (The reference implementation is, additionally, a “natural” sort.
See below for the properties of this algorithm.)</p>
<p>
The <tt>!</tt> procedures are destructive—they use <tt>set-cdr!</tt>s to
rearrange the cells of the lists into the proper order. As such, they
do not allocate any extra cons cells—they are “in place” sorts.
</p>
<p>
The merge operations are stable: an element of <i>list<sub>1</sub></i> will
come before an equal-comparing element in <i>list<sub>2</sub></i> in the result
list.</p>
<p>
</p>
<a name="node_sec_Temp_15"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_15"><tt>vector-merge-sort</tt>—vector merge sort</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-merge-sort<i> < vector [start [end [temp]]]</i>) –> <i>vector</i></tt><a name="node_idx_408"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-merge-sort!<i> < vector [start [end [temp]]]</i>)</tt><a name="node_idx_410"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-merge<i> < vector<sub>1</sub> vector<sub>2</sub> [start<sub>1</sub> [end<sub>1</sub> [start<sub>2</sub> [end<sub>2</sub>]]]]</i>) –> <i>vector</i></tt><a name="node_idx_412"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-merge!<i> < vector vector<sub>1</sub> vector<sub>2</sub> [start [start<sub>1</sub> [end<sub>1</sub> [start<sub>2</sub> [end<sub>2</sub>]]]]]</i>)</tt><a name="node_idx_414"></a></p>
</ul><p>
The sort procedures sort their data using vector merge sort, which is
stable. (The reference implementation is, additionally, a “natural” sort.
See below for the properties of this algorithm.)</p>
<p>
The optional <i>start</i>/<i>end</i> arguments provide for sorting of subranges, and
default to 0 and the length of the corresponding vector.</p>
<p>
Merge-sorting a vector requires the allocation of a temporary
“scratch” work vector for the duration of the sort. This scratch
vector can be passed in by the client as the optional <i>temp</i>
argument; if so, the supplied vector must be of size ≤ <i>end</i>,
and will not be altered outside the range [start,end). If not
supplied, the sort routines allocate one themselves.</p>
<p>
The merge operations are stable: an element of <i>vector<sub>1</sub></i> will
come before an equal-comparing element in <i>vector<sub>2</sub></i> in the
result vector.</p>
<p>
</p>
<ul>
<li><p><tt>Vector-merge-sort!</tt> leaves its result in
<i>vector</i>[<i>start</i>,<i>end</i>).
</p>
<li><p><tt>Vector-merge-sort</tt> returns a vector of length
<i>end</i>−<i>start</i>.
</p>
<li><p><tt>Vector-merge</tt> returns a vector of length
(<i>end<sub>1</sub></i>−<i>start<sub>1</sub></i>) + (<i>end<sub>2</sub></i>−<i>start<sub>2</sub></i>).
</p>
<li><p><tt>Vector-merge!</tt> writes its result into <i>vector</i>, beginning
at index <i>start</i>,
for indices less than <i>end</i> = <i>start</i> +
(<i>end<sub>1</sub></i>−<i>start<sub>1</sub></i>) + (<i>end<sub>2</sub></i>−<i>start<sub>2</sub></i>).
The target subvector
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center></td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>vector</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>[</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>,</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>)</td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
may not overlap either source subvector
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center></td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>vector<sub>1</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>[</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start<sub>1</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>,</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end<sub>1</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>), </td><td><table><tr><td align=center></td><td><table><tr><td align=center> or </td></tr></table></td><td></td></tr></table></td><td>
</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>vector<sub>2</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>[</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>start<sub>2</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>,</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>end<sub>2</sub></td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>).</td></tr></table></td><td></td></tr></table></div>
<p class=noindent>
</p>
</ul><p></p>
<p>
</p>
<a name="node_sec_Temp_16"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_16"><tt>vector-heap-sort</tt>—vector heap sort</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-heap-sort<i> < vector [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_416"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-heap-sort!<i> < vector [start [end]]</i>)</tt><a name="node_idx_418"></a></p>
</ul><p>
These procedures sort their data using heap sort,
which is not a stable sorting algorithm.</p>
<p>
<tt>Vector-heap-sort</tt> returns a vector of length <i>end</i>−<i>start</i>.
<tt>Vector-heap-sort!</tt> is in-place, leaving its result in
<i>vector</i>[<i>start</i>,<i>end</i>).</p>
<p>
</p>
<a name="node_sec_Temp_17"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_17"><tt>vector-quick-sort</tt>—vector quick sort</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-quick-sort<i> < vector [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_420"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-quick-sort!<i> < vector [start [end]]</i>)</tt><a name="node_idx_422"></a></p>
</ul><p>
These procedures sort their data using quick sort,
which is not a stable sorting algorithm.</p>
<p>
<tt>Vector-quick-sort</tt> returns a vector of length <i>end</i>−<i>start</i>.
<tt>Vector-quick-sort!</tt> is in-place, leaving its result in
<i>vector</i>[<i>start</i>,<i>end</i>).</p>
<p>
</p>
<a name="node_sec_Temp_18"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_18"><tt>vector-quick-sort3</tt>—vector quick sort with 3-way comparisons</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-quick-sort3<i> comp vector [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_424"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-quick-sort3!<i> comp vector [start [end]]</i>)</tt><a name="node_idx_426"></a></p>
</ul><p>
These procedures sort their data using quick sort,
which is not a stable sorting algorithm.</p>
<p>
<tt>Vector-quick-sort3</tt> returns a vector of length <i>end</i>−<i>start</i>.
<tt>Vector-quick-sort3!</tt> is in-place, leaving its result in
<i>vector</i>[<i>start</i>,<i>end</i>).</p>
<p>
These procedures implement a variant of quick-sort that takes a three-way
comparison procedure <em>C</em>. <em>C</em> compares a pair of elements and returns
an exact integer whose sign indicates their relationship:
</p>
<a name="node_eqn_Temp_19"></a>
<div align=center><table width=100%>
<tr><td align=right>
(<em>C</em> <em>x</em> <em>y</em>) < 0 </td><td align=center width=2%>⇒</td><td> <em>x</em><<em>y</em></td></tr>
<tr><td align=right>(<em>C</em> <em>x</em> <em>y</em>) = 0 </td><td align=center width=2%>⇒</td><td> <em>x</em> = <em>y</em></td></tr>
<tr><td align=right>(<em>C</em> <em>x</em> <em>y</em>) > 0 </td><td align=center width=2%>⇒</td><td> <em>x</em>><em>y</em>
</td></tr>
</table></div><p>*</p>
<p>
To help remember the relationship between the sign of the result and
the relation, use the procedure − as the model for <em>C</em>: (− <em>x</em> <em>y</em>) < 0
means that <em>x</em> < <em>y</em>; (− <em>x</em> <em>y</em>) > 0 means that <em>x</em> > <em>y</em>.</p>
<p>
</p>
<a name="node_sec_Temp_20"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_20"><tt>vector-insert-sort</tt>—vector insertion sort</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-insert-sort<i> < vector [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_428"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-insert-sort!<i> < vector [start [end]]</i>)</tt><a name="node_idx_430"></a></p>
</ul><p>
These procedures stably sort their data using insertion sort.
</p>
<ul>
<li><p><tt>Vector-insert-sort</tt> returns a vector of length <i>end</i>−<i>start</i>.
</p>
<li><p><tt>Vector-insert-sort!</tt> is in-place, leaving its result in
<i>vector</i>[<i>start</i>,<i>end</i>).
</p>
</ul><p></p>
<p>
</p>
<a name="node_sec_Temp_21"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_21"><tt>delete-neighbor-duplicates</tt>—list and vector
delete neighbor duplicates</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(list-delete-neighbor-dups<i> = list</i>) –> <i>list</i></tt><a name="node_idx_432"></a></p>
<li><p></p>
<p class=noindent><tt>(list-delete-neighbor-dups!<i> = list</i>) –> <i>list</i></tt><a name="node_idx_434"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-delete-neighbor-dups<i> = vector [start [end]]</i>) –> <i>vector</i></tt><a name="node_idx_436"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-delete-neighbor-dups!<i> = vector [start [end]]</i>) –> <i>end′</i></tt><a name="node_idx_438"></a></p>
</ul><p>
These procedures delete adjacent duplicate elements from a list or
a vector, using a given element-equality procedure = . The first/leftmost
element of a run of equal elements is the one that survives. The list
or vector is not otherwise disordered.</p>
<p>
These procedures are linear time—much faster than the <em>O</em>(<em>n</em><sup>2</sup>) general
duplicate-element deletors that do not assume any “bunching” of elements
(such as the ones provided by SRFI 1). If you want to delete duplicate
elements from a large list or vector, you can sort the elements to bring
equal items together, then use one of these procedures, for a total time
of <em>O</em>(<em>n</em>log(<em>n</em>)).</p>
<p>
The comparison procedure = passed to these procedures is always
applied</p>
<p>
</p>
<pre class=verbatim>( = <em>x</em> <em>y</em>)
</pre><p></p>
<p>
where <em>x</em> comes before <em>y</em> in the containing list or vector.
</p>
<ul>
<li><p><tt>List-delete-neighbor-dups</tt> does not alter its input list; its
answer may share storage with the input list.
</p>
<li><p><tt>Vector-delete-neighbor-dups</tt> does not alter its input vector, but
rather allocates a fresh vector to hold the result.
</p>
<li><p><tt>List-delete-neighbor-dups!</tt> is permitted, but not required, to
mutate its input list in order to construct its answer.
</p>
<li><p><tt>Vector-delete-neighbor-dups!</tt> reuses its input vector to hold the
answer, packing its answer into the index range
[<i>start</i>,<i>end′</i>), where
<i>end′</i> is the non-negative exact integer returned as its value. It
returns <i>end′</i> as its result. The vector is not altered outside the range
[<i>start</i>,<i>end′</i>).
</p>
</ul><p>
Examples:</p>
<p>
</p>
<pre class=verbatim>(list-delete-neighbor-dups = '(1 1 2 7 7 7 0 -2 -2))
===⇒ (1 2 7 0 -2)
(vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2))
===⇒ #(1 2 7 0 -2)
(vector-delete-neighbor-dups = '#(1 1 2 7 7 7 0 -2 -2) 3 7)
===⇒ #(7 0 -2)
;; Result left in v[3,9):
(let ((v (vector 0 0 0 1 1 2 2 3 3 4 4 5 5 6 6)))
(cons (vector-delete-neighbor-dups! = v 3)
v))
===⇒ (9 . #(0 0 0 1 2 3 4 5 6 4 4 5 5 6 6))
</pre><p></p>
<p>
</p>
<a name="node_sec_Temp_22"></a>
<h5 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_Temp_22"><tt>binary-searches</tt>—vector binary search</a></h5>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(vector-binary-search<i> < elt->key key vector [start [end]]</i>) –> <i>integer or <tt>#f</tt></i></tt><a name="node_idx_440"></a></p>
<li><p></p>
<p class=noindent><tt>(vector-binary-search3<i> compare-proc vector [start [end]]</i>) –> <i>integer or <tt>#f</tt></i></tt><a name="node_idx_442"></a></p>
</ul><p></p>
<p>
<tt>vector-binary-search</tt> searches <i>vector</i> in range
[<i>start</i>,<i>end</i>) (which default to 0 and the length of
<i>vector</i>, respectively) for an element whose
associated key is equal to <i>key</i>. The procedure <i>elt->key</i> is used to map
an element to its associated key. The elements of the vector are assumed
to be ordered by the < relation on these keys. That is, </p>
<p>
</p>
<pre class=verbatim>(vector-sorted? (lambda (x y) (< (<i>elt->key</i> x) (<i>elt->key</i> y)))
<i>vector</i> <i>start</i> <i>end</i>) ===⇒ true
</pre><p></p>
<p>
An element <i>e</i> of <i>vector</i> is a match for <i>key</i> if it's
neither less nor greater than the key:</p>
<p>
</p>
<pre class=verbatim>(and (not (< (<i>elt->key</i> <i>e</i>) <i>key</i>))
(not (< <i>key</i> (<i>elt->key</i> <i>e</i>))))
</pre><p></p>
<p>
If there is such an element, the procedure returns its index in the
vector as an exact integer. If there is no such element in the searched
range, the procedure returns false.</p>
<p>
</p>
<pre class=verbatim>(vector-binary-search < car 4 '#((1 . one) (3 . three)
(4 . four) (25 . twenty-five)))
===⇒ 2
(vector-binary-search < car 7 '#((1 . one) (3 . three)
(4 . four) (25 . twenty-five)))
===⇒ #f
</pre><p> </p>
<p>
<tt>Vector-binary-search3</tt> is a variant that uses a three-way comparison
procedure <i>compare-proc</i>. <i>Compare-proc</i> compares its
parameter to the search key, and returns an
exact integer whose sign indicates its relationship to the search key.
</p>
<div class=mathdisplay align=center><table><tr><td></td><td><table><tr><td align=center>
</td><td><table><tr><td align=center>array</td><td><table><tr><td align=center><em>r</em><em>c</em><em>l</em><em>c</em><em>r</em><em>c</em><em>l</em></td></tr></table></td><td>
(</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>compare-proc</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> <em>x</em>) </td><td align=center> < </td><td align=center> 0 </td><td align=center> ⇒ </td><td align=center> <em>x</em> </td><td align=center> < </td><td align=center> </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>search-key</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td></td></tr>
<tr><td align=center>
(</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>compare-proc</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> <em>x</em>) </td><td align=center> = </td><td align=center> 0 </td><td align=center> ⇒ </td><td align=center> <em>x</em> </td><td align=center> = </td><td align=center> </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>search-key</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td></td></tr>
<tr><td align=center>
(</td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>compare-proc</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td> <em>x</em>) </td><td align=center> > </td><td align=center> 0 </td><td align=center> ⇒ </td><td align=center> <em>x</em> </td><td align=center> > </td><td align=center> </td><td><table><tr><td align=center><i></td><td><table><tr><td align=center></td><td><table><tr><td align=center>search-key</td></tr></table></td><td></td></tr></table></td><td></i></td></tr></table></td><td>
endarray</td></tr></table></td><td>
</td></tr></table></td><td></td></tr></table></div>
<p class=noindent></p>
<p>
</p>
<pre class=verbatim>(vector-binary-search3 (lambda (elt) (- (car elt) 4))
'#((1 . one) (3 . three)
(4 . four) (25 . twenty-five)))
===⇒ 2
</pre><p></p>
<p>
</p>
<p>
</p>
<a name="node_sec_5.20.3"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.20.3">5.20.3 Algorithmic properties</a></h3>
<p>Different sort and merge algorithms have different properties.
Choose the algorithm that matches your needs:</p>
<p>
</p>
<dl><dt></dt><dd>
</dd><dt><b>Vector insert sort</b></dt><dd>
Stable, but only suitable for small vectors—<em>O</em>(<em>n</em><sup>2</sup>).
</dd><dt><b>Vector quick sort</b></dt><dd>
Not stable. Is fast on average—<em>O</em>(<em>n</em>log(<em>n</em>))—but has bad worst-case
behaviour. Has good memory locality for big vectors (unlike heap sort).
A clever pivot-picking trick (median of three samples) helps avoid
worst-case behaviour, but pathological cases can still blow up.
</dd><dt><b>Vector heap sort</b></dt><dd>
Not stable. Guaranteed fast—<em>O</em>(<em>n</em>log(<em>n</em>)) <em>worst</em> case. Poor
locality on large vectors. A very reliable workhorse.
</dd><dt><b>Vector merge sort</b></dt><dd>
Stable. Not in-place—requires a temporary buffer of equal size.
Fast—<em>O</em>(<em>n</em>log(<em>n</em>))—and has good memory locality for large vectors.<p>
The implementation of vector merge sort provided by this
implementation is, additionally, a “natural” sort, meaning that it
exploits existing order in the input data, providing <em>O</em>(<em>n</em>) best case.
</p>
</dd><dt><b>Destructive list merge sort</b></dt><dd>
Stable, fast and in-place (i.e., allocates no new cons cells). “Fast”
means <em>O</em>(<em>n</em>log(<em>n</em>)) worst-case, and substantially better if the data
is already mostly ordered, all the way down to linear time for
a completely-ordered input list (i.e., it is a “natural” sort).<p>
Note that sorting lists involves chasing pointers through memory, which
can be a loser on modern machine architectures because of poor cache and
page locality.
Sorting vectors has inherently better locality.</p>
<p>
This implementation's destructive list merge and merge sort
implementations are opportunistic—they avoid redundant
<tt>set-cdr!</tt>s, and try to take long
already-ordered runs of list structure as-is when doing the merges.
</p>
</dd><dt><b>Pure list merge sort</b></dt><dd>
Stable and fast—<em>O</em>(<em>n</em>log(<em>n</em>)) worst-case, and possibly <em>O</em>(<em>n</em>),
depending upon the input list (see discussion above).
</dd></dl><p></p>
<p>
</p>
<div align=center><table><tr><td>
<table border=0><tr><td valign=top >Algorithm </td><td valign=top >Stable? </td><td valign=top >Worst case </td><td valign=top >Average case </td><td valign=top >In-place</td></tr>
<tr><td valign=top >Vector insert </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>) </td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>)</td><td valign=top >Yes</td></tr>
<tr><td valign=top >Vector quick </td><td valign=top >No </td><td valign=top ><em>O</em>(<em>n</em><sup>2</sup>) </td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Yes</td></tr>
<tr><td valign=top >Vector heap </td><td valign=top >No </td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Yes</td></tr>
<tr><td valign=top >Vector merge </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >No</td></tr>
<tr><td valign=top >List merge </td><td valign=top >Yes</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top ><em>O</em>(<em>n</em>log(<em>n</em>))</td><td valign=top >Either
</td></tr></table>
</td></tr></table></div>
<p>
</p>
<a name="node_sec_5.21"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21">5.21 Regular expressions</a></h2>
<p></p>
<p>
This section describes a functional interface for building regular
expressions and matching them against strings.
The matching is done using the POSIX regular expression package.
Regular expressions are in the structure <tt>regexps</tt>.</p>
<p>
A regular expression is either a character set, which matches any character
in the set, or a composite expression containing one or more subexpressions.
A regular expression can be matched against a string to determine success
or failure, and to determine the substrings matched by particular subexpressions.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(regexp?<i> value</i>) –> <i>boolean</i></tt><a name="node_idx_444"></a></p>
</ul><p>
</p>
<p class=noindent>Returns <tt>#t</tt> if <i>value</i> is a regular expression created
using the functional interface for regular expressions, and <tt>#f</tt>
otherwise.</p>
<p>
</p>
<a name="node_sec_5.21.1"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21.1">5.21.1 Character sets</a></h3>
<p>Character sets may be defined using a list of characters and strings,
using a range or ranges of characters, or by using set operations on
existing character sets.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(set<i> character-or-string <tt>...</tt></i>) –> <i>char-set</i></tt><a name="node_idx_446"></a></p>
<li><p></p>
<p class=noindent><tt>(range<i> low-char high-char</i>) –> <i>char-set</i></tt><a name="node_idx_448"></a></p>
<li><p></p>
<p class=noindent><tt>(ranges<i> low-char high-char <tt>...</tt></i>) –> <i>char-set</i></tt><a name="node_idx_450"></a></p>
<li><p></p>
<p class=noindent><tt>(ascii-range<i> low-char high-char</i>) –> <i>char-set</i></tt><a name="node_idx_452"></a></p>
<li><p></p>
<p class=noindent><tt>(ascii-ranges<i> low-char high-char <tt>...</tt></i>) –> <i>char-set</i></tt><a name="node_idx_454"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Set</tt> returns a set that contains the character arguments and the
characters in any string arguments. <tt>Range</tt> returns a character
set that contain all characters between <i>low-char</i> and <i>high-char</i>,
inclusive. <tt>Ranges</tt> returns a set that contains all characters in
the given ranges. <tt>Range</tt> and <tt>ranges</tt> use the ordering induced by
<tt>char->integer</tt>. <tt>Ascii-range</tt> and <tt>ascii-ranges</tt> use the
ASCII ordering.
It is an error for a <i>high-char</i> to be less than the preceding
<i>low-char</i> in the appropriate ordering.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(negate<i> char-set</i>) –> <i>char-set</i></tt><a name="node_idx_456"></a></p>
<li><p></p>
<p class=noindent><tt>(intersection<i> char-set char-set</i>) –> <i>char-set</i></tt><a name="node_idx_458"></a></p>
<li><p></p>
<p class=noindent><tt>(union<i> char-set char-set</i>) –> <i>char-set</i></tt><a name="node_idx_460"></a></p>
<li><p></p>
<p class=noindent><tt>(subtract<i> char-set char-set</i>) –> <i>char-set</i></tt><a name="node_idx_462"></a></p>
</ul><p>
</p>
<p class=noindent>These perform the indicated operations on character sets.</p>
<p>
The following character sets are predefined:
</p>
<div align=center><table><tr><td>
<table border=0><tr><td valign=top ><tt>lower-case</tt> </td><td valign=top ><tt>(set "abcdefghijklmnopqrstuvwxyz")</tt> </td></tr>
<tr><td valign=top ><tt>upper-case</tt> </td><td valign=top ><tt>(set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")</tt> </td></tr>
<tr><td valign=top ><tt>alphabetic</tt> </td><td valign=top ><tt>(union lower-case upper-case)</tt> </td></tr>
<tr><td valign=top ><tt>numeric</tt> </td><td valign=top ><tt>(set "0123456789")</tt> </td></tr>
<tr><td valign=top ><tt>alphanumeric</tt> </td><td valign=top ><tt>(union alphabetic numeric)</tt> </td></tr>
<tr><td valign=top ><tt>punctuation</tt> </td><td valign=top ><tt>(set "</tt><code class=verbatim>!\"#$%&'()*+,‑./:;<=>?@[\\]^_`{|}~</code><tt>")</tt> </td></tr>
<tr><td valign=top ><tt>graphic</tt> </td><td valign=top ><tt>(union alphanumeric punctuation)</tt> </td></tr>
<tr><td valign=top ><tt>printing</tt> </td><td valign=top ><tt>(union graphic (set #</tt><code class=verbatim>\</code><tt>space))</tt> </td></tr>
<tr><td valign=top ><tt>control</tt> </td><td valign=top ><tt>(negate printing)</tt> </td></tr>
<tr><td valign=top ><tt>blank</tt> </td><td valign=top ><tt>(set #</tt><code class=verbatim>\</code><tt>space (ascii->char 9))</tt> ; 9 is tab </td></tr>
<tr><td valign=top ><tt>whitespace</tt> </td><td valign=top ><tt>(union (set #</tt><code class=verbatim>\</code><tt>space) (ascii-range 9 13))</tt> </td></tr>
<tr><td valign=top ><tt>hexdigit</tt> </td><td valign=top ><tt>(set "0123456789abcdefABCDEF")</tt> </td></tr>
<tr><td valign=top ></td></tr></table></td></tr></table></div>
<p class=noindent>The above are taken from the default locale in POSIX.
The characters in <tt>whitespace</tt> are <i>space</i>, <i>tab</i>,
<i>newline</i> (= <i>line feed</i>), <i>vertical tab</i>, <i>form feed</i>, and
<i>carriage return</i>.</p>
<p>
</p>
<a name="node_sec_5.21.2"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21.2">5.21.2 Anchoring</a></h3>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(string-start<i></i>) –> <i>reg-exp</i></tt><a name="node_idx_464"></a></p>
<li><p></p>
<p class=noindent><tt>(string-end<i></i>) –> <i>reg-exp</i></tt><a name="node_idx_466"></a></p>
</ul><p>
</p>
<p class=noindent><tt>String-start</tt> returns a regular expression that matches the beginning
of the string being matched against; string-end returns one that matches
the end.</p>
<p>
</p>
<a name="node_sec_5.21.3"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21.3">5.21.3 Composite expressions</a></h3>
<p></p>
<ul>
<li><p></p>
<p class=noindent><tt>(sequence<i> reg-exp <tt>...</tt></i>) –> <i>reg-exp</i></tt><a name="node_idx_468"></a></p>
<li><p></p>
<p class=noindent><tt>(one-of<i> reg-exp <tt>...</tt></i>) –> <i>reg-exp</i></tt><a name="node_idx_470"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Sequence</tt> matches the concatenation of its arguments, <tt>one-of</tt> matches
any one of its arguments.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(text<i> string</i>) –> <i>reg-exp</i></tt><a name="node_idx_472"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Text</tt> returns a regular expression that matches the characters in
<i>string</i>, in order.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(repeat<i> reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_474"></a></p>
<li><p></p>
<p class=noindent><tt>(repeat<i> count reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_476"></a></p>
<li><p></p>
<p class=noindent><tt>(repeat<i> min max reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_478"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Repeat</tt> returns a regular expression that matches zero or more
occurrences of its <i>reg-exp</i> argument. With no count the result
will match any number of times (<i>reg-exp</i>*). With a single
count the returned expression will match
<i>reg-exp</i> exactly that number of times.
The final case will match from <i>min</i> to <i>max</i>
repetitions, inclusive.
<i>Max</i> may be <tt>#f</tt>, in which case there
is no maximum number of matches.
<i>Count</i> and <i>min</i> should be exact, non-negative integers;
<i>max</i> should either be an exact non-negative integer or <tt>#f</tt>.</p>
<p>
</p>
<a name="node_sec_5.21.4"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21.4">5.21.4 Case sensitivity</a></h3>
<p>Regular expressions are normally case-sensitive.
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(ignore-case<i> reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_480"></a></p>
<li><p></p>
<p class=noindent><tt>(use-case<i> reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_482"></a></p>
</ul><p>
</p>
<p class=noindent>The value returned by
<tt>ignore-case</tt> is identical its argument except that case will be
ignored when matching.
The value returned by <tt>use-case</tt> is protected
from future applications of <tt>ignore-case</tt>.
The expressions returned
by <tt>use-case</tt> and <tt>ignore-case</tt> are unaffected by later uses of the
these procedures.
By way of example, the following matches <tt>"ab"</tt> but not <tt>"aB"</tt>,
<tt>"Ab"</tt>, or <tt>"AB"</tt>.
</p>
<pre class=verbatim><tt>(text "ab")</tt>
</pre><p>
</p>
<p class=noindent>while
</p>
<pre class=verbatim><tt>(ignore-case (test "ab"))</tt>
</pre><p>
</p>
<p class=noindent>matches <tt>"ab"</tt>, <tt>"aB"</tt>,
<tt>"Ab"</tt>, and <tt>"AB"</tt> and
</p>
<pre class=verbatim>(ignore-case (sequence (text "a")
(use-case (text "b"))))
</pre><p>
</p>
<p class=noindent>matches <tt>"ab"</tt> and <tt>"Ab"</tt> but not <tt>"aB"</tt> or <tt>"AB"</tt>.</p>
<p>
</p>
<a name="node_sec_5.21.5"></a>
<h3 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.21.5">5.21.5 Submatches and matching</a></h3>
<p>A subexpression within a larger expression can be marked as a submatch.
When an expression is matched against a string, the success or failure
of each submatch within that expression is reported, as well as the
location of the substring matched be each successful submatch.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(submatch<i> key reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_484"></a></p>
<li><p></p>
<p class=noindent><tt>(no-submatches<i> reg-exp</i>) –> <i>reg-exp</i></tt><a name="node_idx_486"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Submatch</tt> returns a regular expression that matches its argument and
causes the result of matching its argument to be reported by the <tt>match</tt>
procedure.
<i>Key</i> is used to indicate the result of this particular submatch
in the alist of successful submatches returned by <tt>match</tt>.
Any value may be used as a <i>key</i>.
<tt>No-submatches</tt> returns an expression identical to its
argument, except that all submatches have been elided.</p>
<p>
</p>
<ul>
<li><p></p>
<p class=noindent><tt>(any-match?<i> reg-exp string</i>) –> <i>boolean</i></tt><a name="node_idx_488"></a></p>
<li><p></p>
<p class=noindent><tt>(exact-match?<i> reg-exp string</i>) –> <i>boolean</i></tt><a name="node_idx_490"></a></p>
<li><p></p>
<p class=noindent><tt>(match<i> reg-exp string</i>) –> <i>match or <tt>#f</tt></i></tt><a name="node_idx_492"></a></p>
<li><p></p>
<p class=noindent><tt>(match-start<i> match</i>) –> <i>index</i></tt><a name="node_idx_494"></a></p>
<li><p></p>
<p class=noindent><tt>(match-end<i> match</i>) –> <i>index</i></tt><a name="node_idx_496"></a></p>
<li><p></p>
<p class=noindent><tt>(match-submatches<i> match</i>) –> <i>alist</i></tt><a name="node_idx_498"></a></p>
</ul><p>
</p>
<p class=noindent><tt>Any-match?</tt> returns <tt>#t</tt> if <i>string</i> matches <i>reg-exp</i> or
contains a substring that does, and <tt>#f</tt> otherwise.
<tt>Exact-match?</tt> returns <tt>#t</tt> if <i>string</i> matches
<i>reg-exp</i> and <tt>#f</tt> otherwise.</p>
<p>
<tt>Match</tt> returns <tt>#f</tt> if <i>reg-exp</i> does not match <i>string</i>
and a match record if it does match.
A match record contains three values: the beginning and end of the substring
that matched
the pattern and an a-list of submatch keys and corresponding match records
for any submatches that also matched.
<tt>Match-start</tt> returns the index of
the first character in the matching substring and <tt>match-end</tt> gives index
of the first character after the matching substring.
<tt>Match-submatches</tt> returns an alist of submatch keys and match records.
Only the top match record returned by <tt>match</tt> has a submatch alist.</p>
<p>
Matching occurs according to POSIX.
The match returned is the one with the lowest starting index in <i>string</i>.
If there is more than one such match, the longest is returned.
Within that match the longest possible submatches are returned.</p>
<p>
All three matching procedures cache a compiled version of <i>reg-exp</i>.
Subsequent calls with the same <i>reg-exp</i> will be more efficient.</p>
<p>
The C interface to the POSIX regular expression code uses ASCII <tt>nul</tt>
as an end-of-string marker.
The matching procedures will ignore any characters following an
embedded ASCII <tt>nul</tt>s in <i>string</i>.</p>
<p>
</p>
<pre class=verbatim>(define pattern (text "abc"))
(any-match? pattern "abc") <code class=verbatim>=> </code>#t
(any-match? pattern "abx") <code class=verbatim>=> </code>#f
(any-match? pattern "xxabcxx") <code class=verbatim>=> </code>#t
(exact-match? pattern "abc") <code class=verbatim>=> </code>#t
(exact-match? pattern "abx") <code class=verbatim>=> </code>#f
(exact-match? pattern "xxabcxx") <code class=verbatim>=> </code>#f
(match pattern "abc") <code class=verbatim>=> </code>#{match 0 3}
(match pattern "abx") <code class=verbatim>=> </code>#f
(match pattern "xxabcxx") <code class=verbatim>=> </code>#{match 2 5}
(let ((x (match (sequence (text "ab")
(submatch 'foo (text "cd"))
(text "ef"))
"xxxabcdefxx")))
(list x (match-submatches x)))
<code class=verbatim>=> </code>(#{match 3 9} ((foo . #{match 5 7}))
(match-submatches
(match (sequence
(set "a")
(one-of (submatch 'foo (text "bc"))
(submatch 'bar (text "BC"))))
"xxxaBCd"))
<code class=verbatim>=> </code>((bar . #{match 4 6}))
</pre><p></p>
<p>
</p>
<a name="node_sec_5.22"></a>
<h2 class=section><a href="manual-Z-H-1.html#node_toc_node_sec_5.22">5.22 SRFIs</a></h2>
<p>`SRFI' stands for `Scheme Request For Implementation'.
An SRFI is a description of an extension to standard Scheme.
Draft and final SRFI documents, a FAQ, and other information about SRFIs
can be found at
<a href="http://srfi.schemers.org">the SRFI web site</a>.</p>
<p>
Scheme 48 includes implementations of the following (final) SRFIs:
</p>
<ul>
<li><p>SRFI 1 – List Library
</p>
<li><p>SRFI 2 – <tt>and-let*</tt>
</p>
<li><p>SRFI 4 – Homogeneous numeric vector datatypes (see note below)
</p>
<li><p>SRFI 5 – <tt>let</tt> with signatures and rest arguments
</p>
<li><p>SRFI 6 – Basic string ports
</p>
<li><p>SRFI 7 – Program configuration
</p>
<li><p>SRFI 8 – <tt>receive</tt>
</p>
<li><p>SRFI 9 – Defining record types
</p>
<li><p>SRFI 11 – Syntax for receiving multiple values
</p>
<li><p>SRFI 13 – String Library
</p>
<li><p>SRFI 14 – Character-Set Library (see note below)
</p>
<li><p>SRFI 16 – Syntax for procedures of variable arity
</p>
<li><p>SRFI 17 – Generalized <tt>set!</tt>
</p>
<li><p>SRFI 19 – Time Data Types and Procedures
</p>
<li><p>SRFI 22 – Running Scheme Scripts on Unix
</p>
<li><p>SRFI 23 – Error reporting mechanism
</p>
<li><p>SRFI 25 – Multi-dimensional Array Primitives
</p>
<li><p>SRFI 26 – Notation for Specializing Parameters without Currying
</p>
<li><p>SRFI 27 – Sources of Random Bits
</p>
<li><p>SRFI 28 – Basic Format Strings
</p>
<li><p>SRFI 31 – A special form <tt>rec</tt> for recursive evaluation
</p>
<li><p>SRFI 34 – Exception Handling for Programs
</p>
<li><p>SRFI 37 – args-fold: a program argument processor
</p>
<li><p>SRFI 40 – A Library of Streams
</p>
<li><p>SRFI 42 – Eager Comprehensions
</p>
<li><p>SRFI 43 – Vector library
</p>
<li><p>SRFI 45 – Primitives for Expressing Iterative Lazy Algorithms
</p>
<li><p>SRFI 60 – Integers as Bits
</p>
<li><p>SRFI 61 – A more general cond clause
</p>
<li><p>SRFI 62 – S-expression comments
</p>
<li><p>SRFI 63 – Homogeneous and Heterogeneous Arrays
</p>
<li><p>SRFI 66 – Octet Vectors
</p>
<li><p>SRFI 67 – Compare Procedures
</p>
<li><p>SRFI 74 – Octet-Addressed Binary Blocks
</p>
<li><p>SRFI 78 – Lightweight testing
</p>
</ul><p>
Documentation on these can be found at the web site mentioned above.</p>
<p>
SRFI 4 specifies an external representation for homogeneous numeric
vectors that is incompatible with R<sup>5</sup>RS. The Scheme 48 version of
SRFI 4 does not support this external representation.</p>
<p>
SRFI 14 includes the procedure <tt>->char-set</tt> which is not a standard
Scheme identifier (in R<sup>5</sup>RS the only required identifier starting
with <tt>-</tt> is <tt>-</tt> itself).
In the Scheme 48 version of SRFI 14 we have renamed <tt>->char-set</tt>
as <tt>x->char-set</tt>.</p>
<p>
SRFI bindings can be accessed
either by opening the appropriate structure
(the structure <tt>srfi-</tt><i>n</i> contains SRFI <i>n</i>)
or by loading structure <tt>srfi-7</tt> and then using
the <tt>,load-srfi-7-program</tt> command to load an SRFI 7-style program.
The syntax for the command is
</p>
<pre class=verbatim><tt>,load-srfi-7-program <i>name</i> <i>filename</i></tt>
</pre><p>
This creates a new structure and associated package, binds the structure
to <i>name</i> in the configuration package, and then loads the program
found in <i>filename</i> into the package.</p>
<p>
As an example, if the file <tt>test.scm</tt> contains
</p>
<pre class=verbatim>(program (code (define x 10)))
</pre><p>
this program can be loaded as follows:
</p>
<pre class=verbatim>> ,load-package srfi-7
> ,load-srfi-7-program test test.scm
[test]
> ,in test
test> x
10
test>
</pre><p></p>
<p>
</p>
<p>
</p>
<p>
</p>
<div class=smallskip></div>
<p style="margin-top: 0pt; margin-bottom: 0pt">
<div align=right class=navigation>[Go to <span><a href="manual.html">first</a>, <a href="manual-Z-H-5.html">previous</a></span><span>, <a href="manual-Z-H-7.html">next</a></span> page<span>; </span><span><a href="manual-Z-H-1.html#node_toc_start">contents</a></span><span><span>; </span><a href="manual-Z-H-11.html#node_index_start">index</a></span>]</div>
</p>
<p></p>
</div>
</body>
</html>
|