[project @ 1996-07-25 21:02:03 by partain]
[nofib.git] / real / gg / StdLib.hs
1 module StdLib where
2 pair :: a -> b -> (a,b)
3 pair a b = (a,b)
4 fstcons :: a -> ([a],b) -> ([a],b)
5 fstcons a (as,b) = (a:as,b)
6 sndcons :: b -> (a,[b]) -> (a,[b])
7 sndcons b (a,bs) = (a,b:bs)
8
9 map2 f [] _ = []
10 map2 f _ [] = []
11 map2 f (a:l) (b:k) = f a b:map2 f l k
12
13 mapcat :: (a->[b]) -> [a] -> [b]
14 mapcat f [] = []
15 mapcat f (a:l) = f a ++ mapcat f l
16 sort :: Ord a => [a] -> [a]
17 sort [] = []
18 sort (a:l) = (sort low) ++ [a] ++ (sort high)
19 where (low,high) = group a l
20 group :: Ord a => a -> [a] -> ([a],[a])
21 group _ [] = ([],[])
22 group i (a:l) = f (group i l)
23 where f (low,high) | a<i = (a:low,high)
24 | otherwise = (low,a:high)
25 insert :: Ord a => a -> [a] -> [a]
26 insert a [] = [a]
27 insert a as@(x:xs) | x>a = a:as
28 | otherwise = x:insert a xs
29 replace :: Eq a => a -> [a] -> [a]
30 replace a [] = []
31 replace a (x:xs) | a == x = a:xs
32 | otherwise = x:replace a xs
33 remove :: Eq a => a -> [a] -> [a]
34 remove a [] = []
35 remove a (x:xs) | a==x = xs
36 | otherwise = x:remove a xs
37 collect :: Ord a => (b->a) -> [a] -> [b] -> [b]
38 collect _ [] _ = []
39 collect _ _ [] = []
40 collect p as@(a:l) bs@(b:k) | a==p b = b:collect p l k
41 | p b > a = collect p l bs
42 | otherwise = collect p as k
43 span' :: (a->Bool) -> [a] -> ([a],[a])
44 span' p [] = ([],[])
45 span' p (x:xs') | p x = fixLeak x (span' p xs') where fixLeak x (xs,ys) = (x:xs,ys)
46 span' _ xs = ([],xs)
47 lines' :: [Char] -> [[Char]]
48 lines' "" = []
49 lines' s = plumb (span' ((/=) '\n') s)
50 where plumb (l,s') = l:if null s' then [] else lines' (tail s')
51 strToInt :: String -> Int
52 strToInt x = strToInt' (length x-1) x
53 where strToInt' _ [] = 0
54 strToInt' x (a:l) = (charToInt a)*(10^x) + (strToInt' (x-1) l)
55 charToInt :: Char -> Int
56 charToInt x = (fromEnum x - fromEnum '0')