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
|
<title>The Haskell 98 Report: Standard Prelude</title>
<body bgcolor="#ffffff"> <i>The Haskell 98 Report</i><br> <a href="index.html">top</a> | <a href="io-13.html">back</a> | <a href="syntax-iso.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><hr>
<a name="stdprelude"></a><a name="sectA"></a>
<h2>A<tt> </tt>Standard Prelude</h2>
<p>
In this appendix the entire Haskell Prelude is given. It constitutes
a <I>specification</I> for the Prelude. Many of the definitions are
written with clarity rather than efficiency in mind,
and it is <I>not</I> required that the specification be implemented as shown here.<p>
The Prelude shown here is organized into a root module, <tt>Prelude</tt>,
and three sub-modules, <tt>PreludeList</tt>, <tt>PreludeText</tt>, and <tt>PreludeIO</tt>.
This structure is purely presentational.
An implementation is <I>not</I> required to use
this organisation for the Prelude,
nor are these three modules available for import separately.
Only the exports of module <tt>Prelude</tt> are significant.<p>
Some of these modules import Library modules, such as <tt>Char</tt>, <tt>Monad</tt>, <tt>IO</tt>,
and <tt>Numeric</tt>. These modules are described fully in the accompanying
Haskell 98 Library Report. These imports are not, of course, part of the specification
of the <tt>Prelude</tt>. That is, an implementation is free to import more, or less,
of the Library modules, as it pleases.<p>
Primitives that
are not definable in Haskell , indicated by names starting with "<tt>prim</tt>", are
defined in a system dependent manner in module <tt>PreludeBuiltin</tt> and
are not shown here. Instance declarations that simply bind primitives to
class methods are omitted. Some of the more verbose instances with
obvious functionality have been left out for the sake of brevity.<p>
Declarations for special types such as <tt>Integer</tt>, <tt>()</tt>, or <tt>(->)</tt> are
included in the Prelude for completeness even though the declaration
may be incomplete or syntactically invalid.<p>
<tt><br>
module Prelude (<br>
module PreludeList, module PreludeText, module PreludeIO,<br>
Bool(False, True),<br>
Maybe(Nothing, Just),<br>
Either(Left, Right),<br>
Ordering(LT, EQ, GT),<br>
Char, String, Int, Integer, Float, Double, Rational, IO,<br>
-- List type: []((:), [])<br>
-- Tuple types: (,), (,,), etc.<br>
-- Trivial type: ()<br>
-- Functions: (->)<br>
Eq((==), (/=)),<br>
Ord(compare, (<), (<=), (>=), (>), max, min),<br>
Enum(succ, pred, toEnum, fromEnum, enumFrom, enumFromThen,<br>
enumFromTo, enumFromThenTo),<br>
Bounded(minBound, maxBound),<br>
Num((+), (-), (*), negate, abs, signum, fromInteger),<br>
Real(toRational),<br>
Integral(quot, rem, div, mod, quotRem, divMod, toInteger),<br>
Fractional((/), recip, fromRational),<br>
Floating(pi, exp, log, sqrt, (**), logBase, sin, cos, tan,<br>
asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh),<br>
RealFrac(properFraction, truncate, round, ceiling, floor),<br>
RealFloat(floatRadix, floatDigits, floatRange, decodeFloat,<br>
encodeFloat, exponent, significand, scaleFloat, isNaN,<br>
isInfinite, isDenormalized, isIEEE, isNegativeZero, atan2),<br>
Monad((>>=), (>>), return, fail),<br>
Functor(fmap),<br>
mapM, mapM_, sequence, sequence_, (=<<), <br>
maybe, either,<br>
(&&), (||), not, otherwise,<br>
subtract, even, odd, gcd, lcm, (^), (^^), <br>
fromIntegral, realToFrac, <br>
fst, snd, curry, uncurry, id, const, (.), flip, ($), until,<br>
asTypeOf, error, undefined,<br>
seq, ($!)<br>
) where<br>
<br>
import PreludeBuiltin -- Contains all `prim' values<br>
import PreludeList<br>
import PreludeText<br>
import PreludeIO<br>
import Ratio( Rational )<br>
<br>
infixr 9 .<br>
infixr 8 ^, ^^, **<br>
infixl 7 *, /, `quot`, `rem`, `div`, `mod`<br>
infixl 6 +, -<br>
infixr 5 :<br>
infix 4 ==, /=, <, <=, >=, ><br>
infixr 3 &&<br>
infixr 2 ||<br>
infixl 1 >>, >>=<br>
infixr 1 =<<<br>
infixr 0 $, $!, `seq`<br>
<br>
-- Standard types, classes, instances and related functions<br>
<br>
-- Equality and Ordered classes<br>
<br>
<a name="$tEq"></a><br>
class Eq a where<br>
(==), (/=) :: a -> a -> Bool<br>
<br>
-- Minimal complete defintion:<br>
-- (==) or (/=)<br>
x /= y = not (x == y)<br>
x == y = not (x /= y)<br>
<br>
<a name="$tOrd"></a><br>
class (Eq a) => Ord a where<br>
compare :: a -> a -> Ordering<br>
(<), (<=), (>=), (>) :: a -> a -> Bool<br>
max, min :: a -> a -> a<br>
<br>
-- Minimal complete definition:<br>
-- (<=) or compare<br>
-- Using compare can be more efficient for complex types.<br>
compare x y<br>
| x == y = EQ<br>
| x <= y = LT<br>
| otherwise = GT<br>
<br>
x <= y = compare x y /= GT<br>
x < y = compare x y == LT<br>
x >= y = compare x y /= LT<br>
x > y = compare x y == GT<br>
<br>
-- note that (min x y, max x y) = (x,y) or (y,x)<br>
max x y <br>
| x >= y = x<br>
| otherwise = y<br>
min x y<br>
| x < y = x<br>
| otherwise = y<br>
<br>
-- Enumeration and Bounded classes<br>
<br>
<a name="$tEnum"></a><br>
class Enum a where<br>
succ, pred :: a -> a<br>
toEnum :: Int -> a<br>
fromEnum :: a -> Int<br>
enumFrom :: a -> [a] -- [n..]<br>
enumFromThen :: a -> a -> [a] -- [n,n'..]<br>
enumFromTo :: a -> a -> [a] -- [n..m]<br>
enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]<br>
<br>
-- Minimal complete definition:<br>
-- toEnum, fromEnum<br>
succ = toEnum . (+1) . fromEnum<br>
pred = toEnum . (subtract 1) . fromEnum<br>
enumFrom x = map toEnum [fromEnum x ..]<br>
enumFromTo x y = map toEnum [fromEnum x .. fromEnum y]<br>
enumFromThenTo x y z = <br>
map toEnum [fromEnum x, fromEnum y .. fromEnum z]<br>
<br>
<a name="$tBounded"></a><br>
class Bounded a where<br>
minBound :: a<br>
maxBound :: a<br>
<br>
-- Numeric classes<br>
<br>
<a name="$tNum"></a><br>
class (Eq a, Show a) => Num a where<br>
(+), (-), (*) :: a -> a -> a<br>
negate :: a -> a<br>
abs, signum :: a -> a<br>
fromInteger :: Integer -> a<br>
<br>
-- Minimal complete definition:<br>
-- All, except negate or (-)<br>
x - y = x + negate y<br>
negate x = 0 - x<br>
<br>
<a name="$tReal"></a><br>
class (Num a, Ord a) => Real a where<br>
toRational :: a -> Rational<br>
<br>
<a name="$tIntegral"></a><br>
class (Real a, Enum a) => Integral a where<br>
quot, rem :: a -> a -> a <br>
div, mod :: a -> a -> a<br>
quotRem, divMod :: a -> a -> (a,a)<br>
toInteger :: a -> Integer<br>
<br>
-- Minimal complete definition:<br>
-- quotRem, toInteger<br>
n `quot` d = q where (q,r) = quotRem n d<br>
n `rem` d = r where (q,r) = quotRem n d<br>
n `div` d = q where (q,r) = divMod n d<br>
n `mod` d = r where (q,r) = divMod n d<br>
divMod n d = if signum r == - signum d then (q-1, r+d) else qr<br>
where qr@(q,r) = quotRem n d<br>
<br>
<a name="$tFractional"></a><br>
class (Num a) => Fractional a where<br>
(/) :: a -> a -> a<br>
recip :: a -> a<br>
fromRational :: Rational -> a<br>
<br>
-- Minimal complete definition:<br>
-- fromRational and (recip or (/))<br>
recip x = 1 / x<br>
x / y = x * recip y<br>
<br>
<a name="$tFloating"></a><br>
class (Fractional a) => Floating a where<br>
pi :: a<br>
exp, log, sqrt :: a -> a<br>
(**), logBase :: a -> a -> a<br>
sin, cos, tan :: a -> a<br>
asin, acos, atan :: a -> a<br>
sinh, cosh, tanh :: a -> a<br>
asinh, acosh, atanh :: a -> a<br>
<br>
-- Minimal complete definition:<br>
-- pi, exp, log, sin, cos, sinh, cosh<br>
-- asinh, acosh, atanh<br>
x ** y = exp (log x * y)<br>
logBase x y = log y / log x<br>
sqrt x = x ** 0.5<br>
tan x = sin x / cos x<br>
tanh x = sinh x / cosh x<br>
<br>
<br>
<a name="$tRealFrac"></a><br>
class (Real a, Fractional a) => RealFrac a where<br>
properFraction :: (Integral b) => a -> (b,a)<br>
truncate, round :: (Integral b) => a -> b<br>
ceiling, floor :: (Integral b) => a -> b<br>
<br>
-- Minimal complete definition:<br>
-- properFraction<br>
truncate x = m where (m,_) = properFraction x<br>
<br>
round x = let (n,r) = properFraction x<br>
m = if r < 0 then n - 1 else n + 1<br>
in case signum (abs r - 0.5) of<br>
-1 -> n<br>
0 -> if even n then n else m<br>
1 -> m<br>
<br>
ceiling x = if r > 0 then n + 1 else n<br>
where (n,r) = properFraction x<br>
<br>
floor x = if r < 0 then n - 1 else n<br>
where (n,r) = properFraction x<br>
<br>
<a name="$tRealFloat"></a><br>
class (RealFrac a, Floating a) => RealFloat a where<br>
floatRadix :: a -> Integer<br>
floatDigits :: a -> Int<br>
floatRange :: a -> (Int,Int)<br>
decodeFloat :: a -> (Integer,Int)<br>
encodeFloat :: Integer -> Int -> a<br>
exponent :: a -> Int<br>
significand :: a -> a<br>
scaleFloat :: Int -> a -> a<br>
isNaN, isInfinite, isDenormalized, isNegativeZero, isIEEE<br>
:: a -> Bool<br>
atan2 :: a -> a -> a<br>
<br>
-- Minimal complete definition:<br>
-- All except exponent, significand, <br>
-- scaleFloat, atan2<br>
exponent x = if m == 0 then 0 else n + floatDigits x<br>
where (m,n) = decodeFloat x<br>
<br>
significand x = encodeFloat m (- floatDigits x)<br>
where (m,_) = decodeFloat x<br>
<br>
scaleFloat k x = encodeFloat m (n+k)<br>
where (m,n) = decodeFloat x<br>
<br>
atan2 y x<br>
| x>0 = atan (y/x)<br>
| x==0 && y>0 = pi/2<br>
| x<0 && y>0 = pi + atan (y/x) <br>
|(x<=0 && y<0) ||<br>
(x<0 && isNegativeZero y) ||<br>
(isNegativeZero x && isNegativeZero y)<br>
= -atan2 (-y) x<br>
| y==0 && (x<0 || isNegativeZero x)<br>
= pi -- must be after the previous test on zero y<br>
| x==0 && y==0 = y -- must be after the other double zero tests<br>
| otherwise = x + y -- x or y is a NaN, return a NaN (via +)<br>
<br>
-- Numeric functions<br>
<br>
<a name="$vsubtract"></a><br>
subtract :: (Num a) => a -> a -> a<br>
subtract = flip (-)<br>
<br>
<a name="$veven"></a><a name="$vodd"></a><br>
even, odd :: (Integral a) => a -> Bool<br>
even n = n `rem` 2 == 0<br>
odd = not . even<br>
<br>
<a name="$vgcd"></a><br>
gcd :: (Integral a) => a -> a -> a<br>
gcd 0 0 = error "Prelude.gcd: gcd 0 0 is undefined"<br>
gcd x y = gcd' (abs x) (abs y)<br>
where gcd' x 0 = x<br>
gcd' x y = gcd' y (x `rem` y)<br>
<br>
<a name="$vlcm"></a><br>
lcm :: (Integral a) => a -> a -> a<br>
lcm _ 0 = 0<br>
lcm 0 _ = 0<br>
lcm x y = abs ((x `quot` (gcd x y)) * y)<br>
<br>
<a name="$v$U"></a><br>
(^) :: (Num a, Integral b) => a -> b -> a<br>
x ^ 0 = 1<br>
x ^ n | n > 0 = f x (n-1) x<br>
where f _ 0 y = y<br>
f x n y = g x n where<br>
g x n | even n = g (x*x) (n `quot` 2)<br>
| otherwise = f x (n-1) (x*y)<br>
_ ^ _ = error "Prelude.^: negative exponent"<br>
<br>
<a name="$v$U$U"></a><br>
(^^) :: (Fractional a, Integral b) => a -> b -> a<br>
x ^^ n = if n >= 0 then x^n else recip (x^(-n))<br>
<br>
<a name="$vfromIntegral"></a><br>
fromIntegral :: (Integral a, Num b) => a -> b<br>
fromIntegral = fromInteger . toInteger<br>
<br>
<a name="$vrealToFrac"></a><br>
realToFrac :: (Real a, Fractional b) => a -> b<br>
realToFrac = fromRational . toRational<br>
<br>
-- Monadic classes<br>
<br>
<a name="$tFunctor"></a><br>
class Functor f where<br>
fmap :: (a -> b) -> f a -> f b<br>
<br>
<a name="$tMonad"></a><br>
class Monad m where<br>
(>>=) :: m a -> (a -> m b) -> m b<br>
(>>) :: m a -> m b -> m b<br>
return :: a -> m a<br>
fail :: String -> m a<br>
<br>
-- Minimal complete definition:<br>
-- (>>=), return<br>
m >> k = m >>= \_ -> k<br>
fail s = error s<br>
<br>
<a name="$vsequence"></a><br>
sequence :: Monad m => [m a] -> m [a] <br>
sequence = foldr mcons (return [])<br>
where mcons p q = p >>= \x -> q >>= \y -> return (x:y)<br>
<br>
<a name="$vsequence_"></a><br>
sequence_ :: Monad m => [m a] -> m () <br>
sequence_ = foldr (>>) (return ())<br>
<br>
-- The xxxM functions take list arguments, but lift the function or<br>
-- list element to a monad type<br>
<a name="$vmapM"></a><br>
mapM :: Monad m => (a -> m b) -> [a] -> m [b]<br>
mapM f as = sequence (map f as)<br>
<br>
<a name="$vmapM_"></a><br>
mapM_ :: Monad m => (a -> m b) -> [a] -> m ()<br>
mapM_ f as = sequence_ (map f as)<br>
<br>
<a name="$v$Q$L$L"></a><br>
(=<<) :: Monad m => (a -> m b) -> m a -> m b<br>
f =<< x = x >>= f<br>
<br>
<br>
-- Trivial type<br>
<br>
<a name="$t$P$C"></a><br>
data () = () deriving (Eq, Ord, Enum, Bounded)<br>
<br>
-- Function type<br>
<br>
<a name="$ta"></a><br>
data a -> b -- No constructor for functions is exported.<br>
<br>
-- identity function<br>
<a name="$vid"></a><br>
id :: a -> a<br>
id x = x<br>
<br>
-- constant function<br>
<a name="$vconst"></a><br>
const :: a -> b -> a<br>
const x _ = x<br>
<br>
-- function composition<br>
<a name="$v."></a><br>
(.) :: (b -> c) -> (a -> b) -> a -> c<br>
f . g = \ x -> f (g x)<br>
<br>
-- flip f takes its (first) two arguments in the reverse order of f.<br>
<a name="$vflip"></a><br>
flip :: (a -> b -> c) -> b -> a -> c<br>
flip f x y = f y x<br>
<br>
<a name="$vseq"></a><br>
seq :: a -> b -> b<br>
seq = ... -- Primitive<br>
<br>
-- right-associating infix application operators <br>
-- (useful in continuation-passing style)<br>
<a name="$v$D"></a><a name="$v$D$E"></a><br>
($), ($!) :: (a -> b) -> a -> b<br>
f $ x = f x<br>
f $! x = x `seq` f x<br>
<br>
<br>
-- Boolean type<br>
<br>
<a name="$tBool"></a><br>
data Bool = False | True deriving (Eq, Ord, Enum, Read, Show, Bounded)<br>
<br>
-- Boolean functions<br>
<br>
<a name="$v$A$A"></a><a name="$v$b$b"></a><br>
(&&), (||) :: Bool -> Bool -> Bool<br>
True && x = x<br>
False && _ = False<br>
True || _ = True<br>
False || x = x<br>
<br>
<a name="$vnot"></a><br>
not :: Bool -> Bool<br>
not True = False<br>
not False = True<br>
<br>
<a name="$votherwise"></a><br>
otherwise :: Bool<br>
otherwise = True<br>
<br>
<br>
-- Character type<br>
<br>
<a name="$tChar"></a><br>
data Char = ... 'a' | 'b' ... -- 2^16 unicode values<br>
<br>
<a name="$iEq$$Char"></a><br>
instance Eq Char where<br>
c == c' = fromEnum c == fromEnum c'<br>
<br>
<a name="$iOrd$$Char"></a><br>
instance Ord Char where<br>
c <= c' = fromEnum c <= fromEnum c'<br>
<br>
<a name="$iEnum$$Char"></a><br>
instance Enum Char where<br>
toEnum = primIntToChar<br>
fromEnum = primCharToInt<br>
enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Char)]<br>
enumFromThen c c' = map toEnum [fromEnum c, fromEnum c' .. fromEnum lastChar]<br>
where lastChar :: Char<br>
lastChar | c' < c = minBound<br>
| otherwise = maxBound<br>
<br>
<a name="$iBounded$$Char"></a><br>
instance Bounded Char where<br>
minBound = '\0'<br>
maxBound = '\xffff'<br>
<br>
<a name="$tString"></a><br>
type String = [Char]<br>
<br>
<br>
-- Maybe type<br>
<br>
<a name="$tMaybe"></a><br>
data Maybe a = Nothing | Just a deriving (Eq, Ord, Read, Show)<br>
<br>
<a name="$vmaybe"></a><br>
maybe :: b -> (a -> b) -> Maybe a -> b<br>
maybe n f Nothing = n<br>
maybe n f (Just x) = f x<br>
<br>
<a name="$iFunctor$$Maybe"></a><br>
instance Functor Maybe where<br>
fmap f Nothing = Nothing<br>
fmap f (Just x) = Just (f x)<br>
<br>
<a name="$iMonad$$Maybe"></a><br>
instance Monad Maybe where<br>
(Just x) >>= k = k x<br>
Nothing >>= k = Nothing<br>
return = Just<br>
fail s = Nothing<br>
<br>
-- Either type<br>
<br>
<a name="$tEither"></a><br>
data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)<br>
<br>
<a name="$veither"></a><br>
either :: (a -> c) -> (b -> c) -> Either a b -> c<br>
either f g (Left x) = f x<br>
either f g (Right y) = g y<br>
<br>
-- IO type<br>
<br>
<a name="$tIO"></a><br>
data IO a -- abstract<br>
<br>
<a name="$iFunctor$$IO"></a><br>
instance Functor IO where<br>
fmap f x = x >>= (return . f)<br>
<br>
<a name="$iMonad$$IO"></a><br>
instance Monad IO where<br>
(>>=) = ...<br>
return = ...<br>
<br>
m >> k = m >>= \_ -> k<br>
fail s = error s<br>
<br>
-- Ordering type<br>
<br>
<a name="$tOrdering"></a><br>
data Ordering = LT | EQ | GT<br>
deriving (Eq, Ord, Enum, Read, Show, Bounded)<br>
<br>
<br>
-- Standard numeric types. The data declarations for these types cannot<br>
-- be expressed directly in Haskell since the constructor lists would be<br>
-- far too large.<br>
<br>
<a name="$tInt"></a><br>
data Int = minBound ... -1 | 0 | 1 ... maxBound<br>
<a name="$iEq$$Int"></a><br>
instance Eq Int where ...<br>
<a name="$iOrd$$Int"></a><br>
instance Ord Int where ...<br>
<a name="$iNum$$Int"></a><br>
instance Num Int where ...<br>
<a name="$iReal$$Int"></a><br>
instance Real Int where ...<br>
<a name="$iIntegral$$Int"></a><br>
instance Integral Int where ...<br>
<a name="$iEnum$$Int"></a><br>
instance Enum Int where ...<br>
<a name="$iBounded$$Int"></a><br>
instance Bounded Int where ...<br>
<br>
<a name="$tInteger"></a><br>
data Integer = ... -1 | 0 | 1 ...<br>
<a name="$iEq$$Integer"></a><br>
instance Eq Integer where ...<br>
<a name="$iOrd$$Integer"></a><br>
instance Ord Integer where ...<br>
<a name="$iNum$$Integer"></a><br>
instance Num Integer where ...<br>
<a name="$iReal$$Integer"></a><br>
instance Real Integer where ...<br>
<a name="$iIntegral$$Integer"></a><br>
instance Integral Integer where ...<br>
<a name="$iEnum$$Integer"></a><br>
instance Enum Integer where ...<br>
<br>
<a name="$tFloat"></a><br>
data Float<br>
<a name="$iEq$$Float"></a><br>
instance Eq Float where ...<br>
<a name="$iOrd$$Float"></a><br>
instance Ord Float where ...<br>
<a name="$iNum$$Float"></a><br>
instance Num Float where ...<br>
<a name="$iReal$$Float"></a><br>
instance Real Float where ...<br>
<a name="$iFractional$$Float"></a><br>
instance Fractional Float where ...<br>
<a name="$iFloating$$Float"></a><br>
instance Floating Float where ...<br>
<a name="$iRealFrac$$Float"></a><br>
instance RealFrac Float where ...<br>
<a name="$iRealFloat$$Float"></a><br>
instance RealFloat Float where ...<br>
<br>
<a name="$tDouble"></a><br>
data Double<br>
<a name="$iEq$$Double"></a><br>
instance Eq Double where ...<br>
<a name="$iOrd$$Double"></a><br>
instance Ord Double where ...<br>
<a name="$iNum$$Double"></a><br>
instance Num Double where ...<br>
<a name="$iReal$$Double"></a><br>
instance Real Double where ...<br>
<a name="$iFractional$$Double"></a><br>
instance Fractional Double where ...<br>
<a name="$iFloating$$Double"></a><br>
instance Floating Double where ...<br>
<a name="$iRealFrac$$Double"></a><br>
instance RealFrac Double where ...<br>
<a name="$iRealFloat$$Double"></a><br>
instance RealFloat Double where ...<br>
<br>
-- The Enum instances for Floats and Doubles are slightly unusual.<br>
-- The `toEnum' function truncates numbers to Int. The definitions<br>
-- of enumFrom and enumFromThen allow floats to be used in arithmetic<br>
-- series: [0,0.1 .. 1.0]. However, roundoff errors make these somewhat<br>
-- dubious. This example may have either 10 or 11 elements, depending on<br>
-- how 0.1 is represented.<br>
<br>
<a name="$iEnum$$Float"></a><br>
instance Enum Float where<br>
succ x = x+1<br>
pred x = x-1<br>
toEnum = fromIntegral<br>
fromEnum = fromInteger . truncate -- may overflow<br>
enumFrom = numericEnumFrom<br>
enumFromThen = numericEnumFromThen<br>
enumFromTo = numericEnumFromTo<br>
enumFromThenTo = numericEnumFromThenTo<br>
<br>
<a name="$iEnum$$Double"></a><br>
instance Enum Double where<br>
succ x = x+1<br>
pred x = x-1<br>
toEnum = fromIntegral<br>
fromEnum = fromInteger . truncate -- may overflow<br>
enumFrom = numericEnumFrom<br>
enumFromThen = numericEnumFromThen<br>
enumFromTo = numericEnumFromTo<br>
enumFromThenTo = numericEnumFromThenTo<br>
<br>
<a name="$vnumericEnumFrom"></a><br>
numericEnumFrom :: (Fractional a) => a -> [a]<br>
<a name="$vnumericEnumFromThen"></a><br>
numericEnumFromThen :: (Fractional a) => a -> a -> [a]<br>
<a name="$vnumericEnumFromTo"></a><br>
numericEnumFromTo :: (Fractional a, Ord a) => a -> a -> [a]<br>
<a name="$vnumericEnumFromThenTo"></a><br>
numericEnumFromThenTo :: (Fractional a, Ord a) => a -> a -> a -> [a]<br>
numericEnumFrom = iterate (+1)<br>
numericEnumFromThen n m = iterate (+(m-n)) n<br>
numericEnumFromTo n m = takeWhile (<= m+1/2) (numericEnumFrom n)<br>
numericEnumFromThenTo n n' m = takeWhile p (numericEnumFromThen n n')<br>
where<br>
p | n' > n = (<= m + (n'-n)/2)<br>
| otherwise = (>= m + (n'-n)/2)<br>
<br>
-- Lists<br>
<br>
-- This data declaration is not legal Haskell<br>
-- but it indicates the idea<br>
<a name="$t$Ba$c"></a><br>
data [a] = [] | a : [a] deriving (Eq, Ord)<br>
<br>
<a name="$iFunctor$$$Ba$c"></a><br>
instance Functor [] where<br>
fmap = map<br>
<br>
<a name="$iMonad$$$Ba$c"></a><br>
instance Monad [] where<br>
m >>= k = concat (map k m)<br>
return x = [x]<br>
fail s = []<br>
<br>
-- Tuples<br>
<br>
<a name="$t$Pa$xb$C"></a><br>
data (a,b) = (a,b) deriving (Eq, Ord, Bounded)<br>
<a name="$t$Pa$xb$xc$C"></a><br>
data (a,b,c) = (a,b,c) deriving (Eq, Ord, Bounded)<br>
<br>
<br>
-- component projections for pairs:<br>
-- (NB: not provided for triples, quadruples, etc.)<br>
<a name="$vfst"></a><br>
fst :: (a,b) -> a<br>
fst (x,y) = x<br>
<br>
<a name="$vsnd"></a><br>
snd :: (a,b) -> b<br>
snd (x,y) = y<br>
<br>
-- curry converts an uncurried function to a curried function;<br>
-- uncurry converts a curried function to a function on pairs.<br>
<a name="$vcurry"></a><br>
curry :: ((a, b) -> c) -> a -> b -> c<br>
curry f x y = f (x, y)<br>
<br>
<a name="$vuncurry"></a><br>
uncurry :: (a -> b -> c) -> ((a, b) -> c)<br>
uncurry f p = f (fst p) (snd p)<br>
<br>
-- Misc functions<br>
<br>
-- until p f yields the result of applying f until p holds.<br>
<a name="$vuntil"></a><br>
until :: (a -> Bool) -> (a -> a) -> a -> a<br>
until p f x <br>
| p x = x<br>
| otherwise = until p f (f x)<br>
<br>
-- asTypeOf is a type-restricted version of const. It is usually used<br>
-- as an infix operator, and its typing forces its first argument<br>
-- (which is usually overloaded) to have the same type as the second.<br>
<a name="$vasTypeOf"></a><br>
asTypeOf :: a -> a -> a<br>
asTypeOf = const<br>
<br>
-- error stops execution and displays an error message<br>
<br>
<a name="$verror"></a><br>
error :: String -> a<br>
error = primError<br>
<br>
-- It is expected that compilers will recognize this and insert error<br>
-- messages that are more appropriate to the context in which undefined <br>
-- appears. <br>
<br>
<a name="$vundefined"></a><br>
undefined :: a<br>
undefined = error "Prelude.undefined"<br>
<br>
<a name="preludelist"></a><p>
</tt><a name="sectA.1"></a>
<h3>A.1<tt> </tt>Prelude <tt>PreludeList</tt></h3>
<tt><br>
-- Standard list functions<br>
<br>
module PreludeList (<br>
map, (++), filter, concat,<br>
head, last, tail, init, null, length, (!!), <br>
foldl, foldl1, scanl, scanl1, foldr, foldr1, scanr, scanr1,<br>
iterate, repeat, replicate, cycle,<br>
take, drop, splitAt, takeWhile, dropWhile, span, break,<br>
lines, words, unlines, unwords, reverse, and, or,<br>
any, all, elem, notElem, lookup,<br>
Sum, product, maximum, minimum, concatMap, <br>
zip, zip3, zipWith, zipWith3, unzip, unzip3)<br>
where<br>
<br>
import qualified Char(isSpace)<br>
<br>
infixl 9 !!<br>
infixr 5 ++<br>
infix 4 `elem`, `notElem`<br>
<br>
-- Map and append<br>
<a name="$vmap"></a><br>
map :: (a -> b) -> [a] -> [a]<br>
map f [] = []<br>
map f (x:xs) = f x : map f xs<br>
<br>
<a name="$v++"></a><br>
(++) :: [a] -> [a] -> [a]<br>
[] ++ ys = ys<br>
(x:xs) ++ ys = x : (xs ++ ys)<br>
<br>
<a name="$vfilter"></a><br>
filter :: (a -> Bool) -> [a] -> [a]<br>
filter p [] = []<br>
filter p (x:xs) | p x = x : filter p xs<br>
| otherwise = filter p xs<br>
<br>
<a name="$vconcat"></a><br>
concat :: [[a]] -> [a]<br>
concat xss = foldr (++) [] xss<br>
<br>
<br>
-- head and tail extract the first element and remaining elements,<br>
-- respectively, of a list, which must be non-empty. last and init<br>
-- are the dual functions working from the end of a finite list,<br>
-- rather than the beginning.<br>
<br>
<a name="$vhead"></a><br>
head :: [a] -> a<br>
head (x:_) = x<br>
head [] = error "Prelude.head: empty list"<br>
<br>
<a name="$vlast"></a><br>
last :: [a] -> a<br>
last [x] = x<br>
last (_:xs) = last xs<br>
last [] = error "Prelude.last: empty list"<br>
<br>
<a name="$vtail"></a><br>
tail :: [a] -> [a]<br>
tail (_:xs) = xs<br>
tail [] = error "Prelude.tail: empty list"<br>
<br>
<a name="$vinit"></a><br>
init :: [a] -> [a]<br>
init [x] = []<br>
init (x:xs) = x : init xs<br>
init [] = error "Prelude.init: empty list"<br>
<br>
<a name="$vnull"></a><br>
null :: [a] -> Bool<br>
null [] = True<br>
null (_:_) = False<br>
<br>
-- length returns the length of a finite list as an Int.<br>
<a name="$vlength"></a><br>
length :: [a] -> Int<br>
length [] = 0<br>
length (_:l) = 1 + length l<br>
<br>
-- List index (subscript) operator, 0-origin<br>
<a name="$v$E$E"></a><br>
(!!) :: [a] -> Int -> a<br>
(x:_) !! 0 = x<br>
(_:xs) !! n | n > 0 = xs !! (n-1)<br>
(_:_) !! _ = error "Prelude.!!: negative index"<br>
[] !! _ = error "Prelude.!!: index too large"<br>
<br>
-- foldl, applied to a binary operator, a starting value (typically the<br>
-- left-identity of the operator), and a list, reduces the list using<br>
-- the binary operator, from left to right:<br>
-- foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn<br>
-- foldl1 is a variant that has no starting value argument, and thus must<br>
-- be applied to non-empty lists. scanl is similar to foldl, but returns<br>
-- a list of successive reduced values from the left:<br>
-- scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]<br>
-- Note that last (scanl f z xs) == foldl f z xs.<br>
-- scanl1 is similar, again without the starting element:<br>
-- scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]<br>
<br>
<a name="$vfoldl"></a><br>
foldl :: (a -> b -> a) -> a -> [b] -> a<br>
foldl f z [] = z<br>
foldl f z (x:xs) = foldl f (f z x) xs<br>
<br>
<a name="$vfoldl1"></a><br>
foldl1 :: (a -> a -> a) -> [a] -> a<br>
foldl1 f (x:xs) = foldl f x xs<br>
foldl1 _ [] = error "Prelude.foldl1: empty list"<br>
<br>
<a name="$vscanl"></a><br>
scanl :: (a -> b -> a) -> a -> [b] -> [a]<br>
scanl f q xs = q : (case xs of<br>
[] -> []<br>
x:xs -> scanl f (f q x) xs)<br>
<br>
<a name="$vscanl1"></a><br>
scanl1 :: (a -> a -> a) -> [a] -> [a]<br>
scanl1 f (x:xs) = scanl f x xs<br>
scanl1 _ [] = error "Prelude.scanl1: empty list"<br>
<br>
-- foldr, foldr1, scanr, and scanr1 are the right-to-left duals of the<br>
-- above functions.<br>
<br>
<a name="$vfoldr"></a><br>
foldr :: (a -> b -> b) -> b -> [a] -> b<br>
foldr f z [] = z<br>
foldr f z (x:xs) = f x (foldr f z xs)<br>
<br>
<a name="$vfoldr1"></a><br>
foldr1 :: (a -> a -> a) -> [a] -> a<br>
foldr1 f [x] = x<br>
foldr1 f (x:xs) = f x (foldr1 f xs)<br>
foldr1 _ [] = error "Prelude.foldr1: empty list"<br>
<br>
<a name="$vscanr"></a><br>
scanr :: (a -> b -> b) -> b -> [a] -> [b]<br>
scanr f q0 [] = [q0]<br>
scanr f q0 (x:xs) = f x q : qs<br>
where qs@(q:_) = scanr f q0 xs <br>
<br>
<a name="$vscanr1"></a><br>
scanr1 :: (a -> a -> a) -> [a] -> [a]<br>
scanr1 f [x] = [x]<br>
scanr1 f (x:xs) = f x q : qs<br>
where qs@(q:_) = scanr1 f xs <br>
scanr1 _ [] = error "Prelude.scanr1: empty list"<br>
<br>
-- iterate f x returns an infinite list of repeated applications of f to x:<br>
-- iterate f x == [x, f x, f (f x), ...]<br>
<a name="$viterate"></a><br>
iterate :: (a -> a) -> a -> [a]<br>
iterate f x = x : iterate f (f x)<br>
<br>
-- repeat x is an infinite list, with x the value of every element.<br>
<a name="$vrepeat"></a><br>
repeat :: a -> [a]<br>
repeat x = xs where xs = x:xs<br>
<br>
-- replicate n x is a list of length n with x the value of every element<br>
<a name="$vreplicate"></a><br>
replicate :: Int -> a -> [a]<br>
replicate n x = take n (repeat x)<br>
<br>
-- cycle ties a finite list into a circular one, or equivalently,<br>
-- the infinite repetition of the original list. It is the identity<br>
-- on infinite lists.<br>
<br>
<a name="$vcycle"></a><br>
cycle :: [a] -> [a]<br>
cycle [] = error "Prelude.cycle: empty list"<br>
cycle xs = xs' where xs' = xs ++ xs'<br>
<br>
-- take n, applied to a list xs, returns the prefix of xs of length n,<br>
-- or xs itself if n > length xs. drop n xs returns the suffix of xs<br>
-- after the first n elements, or [] if n > length xs. splitAt n xs<br>
-- is equivalent to (take n xs, drop n xs).<br>
<br>
<a name="$vtake"></a><br>
take :: Int -> [a] -> [a]<br>
take 0 _ = []<br>
take _ [] = []<br>
take n (x:xs) | n > 0 = x : take (n-1) xs<br>
take _ _ = error "Prelude.take: negative argument"<br>
<br>
<a name="$vdrop"></a><br>
drop :: Int -> [a] -> [a]<br>
drop 0 xs = xs<br>
drop _ [] = []<br>
drop n (_:xs) | n > 0 = drop (n-1) xs<br>
drop _ _ = error "Prelude.drop: negative argument"<br>
<br>
<a name="$vsplitAt"></a><br>
splitAt :: Int -> [a] -> ([a],[a])<br>
splitAt 0 xs = ([],xs)<br>
splitAt _ [] = ([],[])<br>
splitAt n (x:xs) | n > 0 = (x:xs',xs'') where (xs',xs'') = splitAt (n-1) xs<br>
splitAt _ _ = error "Prelude.splitAt: negative argument"<br>
<br>
-- takeWhile, applied to a predicate p and a list xs, returns the longest<br>
-- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs<br>
-- returns the remaining suffix. Span p xs is equivalent to <br>
-- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.<br>
<br>
<a name="$vtakeWhile"></a><br>
takeWhile :: (a -> Bool) -> [a] -> [a]<br>
takeWhile p [] = []<br>
takeWhile p (x:xs) <br>
| p x = x : takeWhile p xs<br>
| otherwise = []<br>
<br>
<a name="$vdropWhile"></a><br>
dropWhile :: (a -> Bool) -> [a] -> [a]<br>
dropWhile p [] = []<br>
dropWhile p xs@(x:xs')<br>
| p x = dropWhile p xs'<br>
| otherwise = xs<br>
<br>
<a name="$vspan"></a><a name="$vbreak"></a><br>
span, break :: (a -> Bool) -> [a] -> ([a],[a])<br>
span p [] = ([],[])<br>
span p xs@(x:xs') <br>
| p x = (x:ys,zs) <br>
| otherwise = ([],xs)<br>
where (ys,zs) = span p xs'<br>
<br>
break p = span (not . p)<br>
<br>
-- lines breaks a string up into a list of strings at newline characters.<br>
-- The resulting strings do not contain newlines. Similary, words<br>
-- breaks a string up into a list of words, which were delimited by<br>
-- white space. unlines and unwords are the inverse operations.<br>
-- unlines joins lines with terminating newlines, and unwords joins<br>
-- words with separating spaces.<br>
<br>
<a name="$vlines"></a><br>
lines :: String -> [String]<br>
lines "" = []<br>
lines s = let (l, s') = break (== '\n') s<br>
in l : case s' of<br>
[] -> []<br>
(_:s'') -> lines s''<br>
<br>
<a name="$vwords"></a><br>
words :: String -> [String]<br>
words s = case dropWhile Char.isSpace s of<br>
"" -> []<br>
s' -> w : words s''<br>
where (w, s'') = break Char.isSpace s'<br>
<br>
<a name="$vunlines"></a><br>
unlines :: [String] -> String<br>
unlines = concatMap (++ "\n")<br>
<br>
<a name="$vunwords"></a><br>
unwords :: [String] -> String<br>
unwords [] = ""<br>
unwords ws = foldr1 (\w s -> w ++ ' ':s) ws<br>
<br>
-- reverse xs returns the elements of xs in reverse order. xs must be finite.<br>
<a name="$vreverse"></a><br>
reverse :: [a] -> [a]<br>
reverse = foldl (flip (:)) []<br>
<br>
-- and returns the conjunction of a Boolean list. For the result to be<br>
-- True, the list must be finite; False, however, results from a False<br>
-- value at a finite index of a finite or infinite list. or is the<br>
-- disjunctive dual of and.<br>
<a name="$vand"></a><a name="$vor"></a><br>
and, or :: [Bool] -> Bool<br>
and = foldr (&&) True<br>
or = foldr (||) False<br>
<br>
-- Applied to a predicate and a list, any determines if any element<br>
-- of the list satisfies the predicate. Similarly, for all.<br>
<a name="$vany"></a><a name="$vall"></a><br>
any, all :: (a -> Bool) -> [a] -> Bool<br>
any p = or . map p<br>
all p = and . map p<br>
<br>
-- elem is the list membership predicate, usually written in infix form,<br>
-- e.g., x `elem` xs. notElem is the negation.<br>
<a name="$velem"></a><a name="$vnotElem"></a><br>
elem, notElem :: (Eq a) => a -> [a] -> Bool<br>
elem x = any (== x)<br>
notElem x = all (/= x)<br>
<br>
-- lookup key assocs looks up a key in an association list.<br>
<a name="$vlookup"></a><br>
lookup :: (Eq a) => a -> [(a,b)] -> Maybe b<br>
lookup key [] = Nothing<br>
lookup key ((x,y):xys)<br>
| key == x = Just y<br>
| otherwise = lookup key xys<br>
<br>
-- sum and product compute the sum or product of a finite list of numbers.<br>
<a name="$vsum"></a><a name="$vproduct"></a><br>
sum, product :: (Num a) => [a] -> a<br>
sum = foldl (+) 0 <br>
product = foldl (*) 1<br>
<br>
-- maximum and minimum return the maximum or minimum value from a list,<br>
-- which must be non-empty, finite, and of an ordered type.<br>
<a name="$vmaximum"></a><a name="$vminimum"></a><br>
maximum, minimum :: (Ord a) => [a] -> a<br>
maximum [] = error "Prelude.maximum: empty list"<br>
maximum xs = foldl1 max xs<br>
<br>
minimum [] = error "Prelude.minimum: empty list"<br>
minimum xs = foldl1 min xs<br>
<br>
<a name="$vconcatMap"></a><br>
concatMap :: (a -> [b]) -> [a] -> [b]<br>
concatMap f = concat . map f<br>
<br>
-- zip takes two lists and returns a list of corresponding pairs. If one<br>
-- input list is short, excess elements of the longer list are discarded.<br>
-- zip3 takes three lists and returns a list of triples. Zips for larger<br>
-- tuples are in the List library<br>
<br>
<a name="$vzip"></a><br>
zip :: [a] -> [b] -> [(a,b)]<br>
zip = zipWith (,)<br>
<br>
<a name="$vzip3"></a><br>
zip3 :: [a] -> [b] -> [c] -> [(a,b,c)]<br>
zip3 = zipWith3 (,,)<br>
<br>
-- The zipWith family generalises the zip family by zipping with the<br>
-- function given as the first argument, instead of a tupling function.<br>
-- For example, zipWith (+) is applied to two lists to produce the list<br>
-- of corresponding sums.<br>
<br>
<a name="$vzipWith"></a><br>
zipWith :: (a->b->c) -> [a]->[b]->[c]<br>
zipWith z (a:as) (b:bs)<br>
= z a b : zipWith z as bs<br>
zipWith _ _ _ = []<br>
<br>
<a name="$vzipWith3"></a><br>
zipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->[d]<br>
zipWith3 z (a:as) (b:bs) (c:cs)<br>
= z a b c : zipWith3 z as bs cs<br>
zipWith3 _ _ _ _ = []<br>
<br>
<br>
-- unzip transforms a list of pairs into a pair of lists. <br>
<br>
<a name="$vunzip"></a><br>
unzip :: [(a,b)] -> ([a],[b])<br>
unzip = foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[])<br>
<br>
<a name="$vunzip3"></a><br>
unzip3 :: [(a,b,c)] -> ([a],[b],[c])<br>
unzip3 = foldr (\(a,b,c) ~(as,bs,cs) -> (a:as,b:bs,c:cs))<br>
([],[],[])<br>
<br>
<br>
<a name="preludetext"></a><p>
</tt><a name="sectA.2"></a>
<h3>A.2<tt> </tt>Prelude <tt>PreludeText</tt></h3>
<tt><br>
module PreludeText (<br>
ReadS, ShowS,<br>
Read(readsPrec, readList),<br>
Show(showsPrec, showList),<br>
reads, shows, show, read, lex,<br>
showChar, showString, readParen, showParen ) where<br>
<br>
-- The instances of Read and Show for<br>
-- Bool, Char, Maybe, Either, Ordering<br>
-- are done via "deriving" clauses in Prelude.hs<br>
<br>
import Char(isSpace, isAlpha, isDigit, isAlphaNum,<br>
showLitChar, readLitChar, lexLitChar)<br>
<br>
import Numeric(showSigned, showInt, readSigned, readDec, showFloat,<br>
readFloat, lexDigits)<br>
<br>
<a name="$tReadS"></a><br>
type ReadS a = String -> [(a,String)]<br>
<a name="$tShowS"></a><br>
type ShowS = String -> String<br>
<br>
<a name="$tRead"></a><br>
class Read a where<br>
readsPrec :: Int -> ReadS a<br>
readList :: ReadS [a]<br>
<br>
-- Minimal complete definition:<br>
-- readsPrec<br>
readList = readParen False (\r -> [pr | ("[",s) <- lex r,<br>
pr <- readl s])<br>
where readl s = [([],t) | ("]",t) <- lex s] ++<br>
[(x:xs,u) | (x,t) <- reads s,<br>
(xs,u) <- readl' t]<br>
readl' s = [([],t) | ("]",t) <- lex s] ++<br>
[(x:xs,v) | (",",t) <- lex s,<br>
(x,u) <- reads t,<br>
(xs,v) <- readl' u]<br>
<br>
<a name="$tShow"></a><br>
class Show a where<br>
showsPrec :: Int -> a -> ShowS<br>
show :: a -> String <br>
showList :: [a] -> ShowS<br>
<br>
-- Mimimal complete definition:<br>
-- show or showsPrec<br>
showsPrec _ x s = show x ++ s<br>
<br>
show x = showsPrec 0 x ""<br>
<br>
showList [] = showString "[]"<br>
showList (x:xs) = showChar '[' . shows x . showl xs<br>
where showl [] = showChar ']'<br>
showl (x:xs) = showChar ',' . shows x .<br>
showl xs<br>
<br>
<a name="$vreads"></a><br>
reads :: (Read a) => ReadS a<br>
reads = readsPrec 0<br>
<br>
<a name="$vshows"></a><br>
shows :: (Show a) => a -> ShowS<br>
shows = showsPrec 0<br>
<br>
<a name="$vread"></a><br>
read :: (Read a) => String -> a<br>
read s = case [x | (x,t) <- reads s, ("","") <- lex t] of<br>
[x] -> x<br>
[] -> error "Prelude.read: no parse"<br>
_ -> error "Prelude.read: ambiguous parse"<br>
<br>
<a name="$vshowChar"></a><br>
showChar :: Char -> ShowS<br>
showChar = (:)<br>
<br>
<a name="$vshowString"></a><br>
showString :: String -> ShowS<br>
showString = (++)<br>
<br>
<a name="$vshowParen"></a><br>
showParen :: Bool -> ShowS -> ShowS<br>
showParen b p = if b then showChar '(' . p . showChar ')' else p<br>
<br>
<a name="$vreadParen"></a><br>
readParen :: Bool -> ReadS a -> ReadS a<br>
readParen b g = if b then mandatory else optional<br>
where optional r = g r ++ mandatory r<br>
mandatory r = [(x,u) | ("(",s) <- lex r,<br>
(x,t) <- optional s,<br>
(")",u) <- lex t ]<br>
<br>
-- This lexer is not completely faithful to the Haskell lexical syntax.<br>
-- Current limitations:<br>
-- Qualified names are not handled properly<br>
-- Octal and hexidecimal numerics are not recognized as a single token<br>
-- Comments are not treated properly<br>
<br>
<a name="$vlex"></a><br>
lex :: ReadS String<br>
lex "" = [("","")]<br>
lex (c:s)<br>
| isSpace c = lex (dropWhile isSpace s)<br>
lex ('\'':s) = [('\'':ch++"'", t) | (ch,'\'':t) <- lexLitChar s,<br>
ch /= "'" ]<br>
lex ('"':s) = [('"':str, t) | (str,t) <- lexString s]<br>
where<br>
lexString ('"':s) = [("\"",s)]<br>
lexString s = [(ch++str, u)<br>
| (ch,t) <- lexStrItem s,<br>
(str,u) <- lexString t ]<br>
<br>
lexStrItem ('\\':'&':s) = [("\\&",s)]<br>
lexStrItem ('\\':c:s) | isSpace c<br>
= [("\\&",t) | <br>
'\\':t <-<br>
[dropWhile isSpace s]]<br>
lexStrItem s = lexLitChar s<br>
<br>
lex (c:s) | isSingle c = [([c],s)]<br>
| isSym c = [(c:sym,t) | (sym,t) <- [span isSym s]]<br>
| isAlpha c = [(c:nam,t) | (nam,t) <- [span isIdChar s]]<br>
| isDigit c = [(c:ds++fe,t) | (ds,s) <- [span isDigit s],<br>
(fe,t) <- lexFracExp s ]<br>
| otherwise = [] -- bad character<br>
where<br>
isSingle c = c `elem` ",;()[]{}_`"<br>
isSym c = c `elem` "!@#$%&*+./<=>?\\^|:-~"<br>
isIdChar c = isAlphaNum c || c `elem` "_'"<br>
<br>
lexFracExp ('.':c:cs) | isDigit c<br>
= [('.':ds++e,u) | (ds,t) <- lexDigits (c:cs),<br>
(e,u) <- lexExp t]<br>
lexFracExp s = [("",s)]<br>
<br>
lexExp (e:s) | e `elem` "eE"<br>
= [(e:c:ds,u) | (c:t) <- [s], c `elem` "+-",<br>
(ds,u) <- lexDigits t] ++<br>
[(e:ds,t) | (ds,t) <- lexDigits s]<br>
lexExp s = [("",s)]<br>
<br>
<a name="$iShow$$Int"></a><br>
instance Show Int where<br>
showsPrec = showSigned showInt<br>
<br>
<a name="$iRead$$Int"></a><br>
instance Read Int where<br>
readsPrec p = readSigned readDec<br>
<br>
<a name="$iShow$$Integer"></a><br>
instance Show Integer where<br>
showsPrec = showSigned showInt<br>
<br>
<a name="$iRead$$Integer"></a><br>
instance Read Integer where<br>
readsPrec p = readSigned readDec<br>
<br>
<a name="$iShow$$Float"></a><br>
instance Show Float where <br>
showsPrec p = showFloat<br>
<br>
<a name="$iRead$$Float"></a><br>
instance Read Float where<br>
readsPrec p = readFloat<br>
<br>
<a name="$iShow$$Double"></a><br>
instance Show Double where<br>
showsPrec p = showFloat<br>
<br>
<a name="$iRead$$Double"></a><br>
instance Read Double where<br>
readsPrec p = readFloat<br>
<br>
<a name="$iShow$$$P$C"></a><br>
instance Show () where<br>
showsPrec p () = showString "()"<br>
<br>
<a name="$iRead$$$P$C"></a><br>
instance Read () where<br>
readsPrec p = readParen False<br>
(\r -> [((),t) | ("(",s) <- lex r,<br>
(")",t) <- lex s ] )<br>
<a name="$iShow$$Char"></a><br>
instance Show Char where<br>
showsPrec p '\'' = showString "'\\''"<br>
showsPrec p c = showChar '\'' . showLitChar c . showChar '\''<br>
<br>
showList cs = showChar '"' . showl cs<br>
where showl "" = showChar '"'<br>
showl ('"':cs) = showString "\\\"" . showl cs<br>
showl (c:cs) = showLitChar c . showl cs<br>
<br>
<a name="$iRead$$Char"></a><br>
instance Read Char where<br>
readsPrec p = readParen False<br>
(\r -> [(c,t) | ('\'':s,t)<- lex r,<br>
(c,"\'") <- readLitChar s])<br>
<br>
readList = readParen False (\r -> [(l,t) | ('"':s, t) <- lex r,<br>
(l,_) <- readl s ])<br>
where readl ('"':s) = [("",s)]<br>
readl ('\\':'&':s) = readl s<br>
readl s = [(c:cs,u) | (c ,t) <- readLitChar s,<br>
(cs,u) <- readl t ]<br>
<br>
<a name="$iShow$$$Ba$c"></a><br>
instance (Show a) => Show [a] where<br>
showsPrec p = showList<br>
<br>
<a name="$iRead$$$Ba$c"></a><br>
instance (Read a) => Read [a] where<br>
readsPrec p = readList<br>
<br>
-- Tuples<br>
<br>
<a name="$iShow$$$Pa$xb$C"></a><br>
instance (Show a, Show b) => Show (a,b) where<br>
showsPrec p (x,y) = showChar '(' . shows x . showChar ',' .<br>
shows y . showChar ')'<br>
<br>
<a name="$iRead$$$Pa$xb$C"></a><br>
instance (Read a, Read b) => Read (a,b) where<br>
readsPrec p = readParen False<br>
(\r -> [((x,y), w) | ("(",s) <- lex r,<br>
(x,t) <- reads s,<br>
(",",u) <- lex t,<br>
(y,v) <- reads u,<br>
(")",w) <- lex v ] )<br>
<br>
-- Other tuples have similar Read and Show instances<br>
<br>
<br>
<br>
<a name="preludeio"></a><p>
</tt><a name="sectA.3"></a>
<h3>A.3<tt> </tt>Prelude <tt>PreludeIO</tt></h3>
<tt><br>
module PreludeIO (<br>
FilePath, IOError, ioError, userError, catch,<br>
putChar, putStr, putStrLn, print,<br>
getChar, getLine, getContents, interact,<br>
readFile, writeFile, appendFile, readIO, readLn<br>
) where<br>
<br>
import PreludeBuiltin<br>
<br>
<br>
<a name="$tFilePath"></a><br>
type FilePath = String<br>
<br>
<a name="$tIOError"></a><br>
data IOError -- The internals of this type are system dependent<br>
<br>
<a name="$iShow$$IOError"></a><br>
instance Show IOError where ...<br>
<a name="$iEq$$IOError"></a><br>
instance Eq IOError where ...<br>
<br>
<a name="$vioError"></a><br>
ioError :: IOError -> IO a <br>
ioError = primIOError<br>
<br>
<a name="$vuserError"></a><br>
userError :: String -> IOError<br>
userError = primUserError<br>
<br>
<a name="$vcatch"></a><br>
catch :: IO a -> (IOError -> IO a) -> IO a <br>
catch = primCatch<br>
<br>
<a name="$vputChar"></a><br>
putChar :: Char -> IO ()<br>
putChar = primPutChar<br>
<br>
<a name="$vputStr"></a><br>
putStr :: String -> IO ()<br>
putStr s = mapM_ putChar s<br>
<br>
<a name="$vputStrLn"></a><br>
putStrLn :: String -> IO ()<br>
putStrLn s = do putStr s<br>
putStr "\n"<br>
<br>
<a name="$vprint"></a><br>
print :: Show a => a -> IO ()<br>
print x = putStrLn (show x)<br>
<br>
<a name="$vgetChar"></a><br>
getChar :: IO Char<br>
getChar = primGetChar<br>
<br>
<a name="$vgetLine"></a><br>
getLine :: IO String<br>
getLine = do c <- getChar<br>
if c == '\n' then return "" else <br>
do s <- getLine<br>
return (c:s)<br>
<br>
<a name="$vgetContents"></a><br>
getContents :: IO String<br>
getContents = primGetContents<br>
<br>
<a name="$vinteract"></a><br>
interact :: (String -> String) -> IO ()<br>
interact f = do s <- getContents<br>
putStr (f s)<br>
<br>
<a name="$vreadFile"></a><br>
readFile :: FilePath -> IO String<br>
readFile = primReadFile<br>
<br>
<a name="$vwriteFile"></a><br>
writeFile :: FilePath -> String -> IO ()<br>
writeFile = primWriteFile<br>
<br>
<a name="$vappendFile"></a><br>
appendFile :: FilePath -> String -> IO ()<br>
appendFile = primAppendFile<br>
<br>
-- raises an exception instead of an error<br>
<a name="$vreadIO"></a><br>
readIO :: Read a => String -> IO a<br>
readIO s = case [x | (x,t) <- reads s, ("","") <- lex t] of<br>
[x] -> return x<br>
[] -> ioError (userError "Prelude.readIO: no parse")<br>
_ -> ioError (userError "Prelude.readIO: ambiguous parse")<br>
<br>
<a name="$vreadLn"></a><br>
readLn :: Read a => IO a<br>
readLn = do l <- getLine<br>
r <- readIO l<br>
return r<br>
<p>
<hr><i>The Haskell 98 Report</i><br><a href="index.html">top</a> | <a href="io-13.html">back</a> | <a href="syntax-iso.html">next</a> | <a href="index98.html">contents</a> | <a href="prelude-index.html">function index</a> <br><font size=2>1 February, 1999</font>
<p>
</tt>
|