[project @ 1997-07-27 00:43:10 by sof]
[ghc-hetmet.git] / ghc / tests / programs / ipoole_spec_class / GoferPreludeBits.lhs
1 \begin{vb}
2
3 > module GoferPreludeBits where
4
5 This script contains some useful functions taken from the standard
6 Gofer prelude, but which do not appear in the Haskell prelude
7
8 SCCS: %W% %G%
9
10
11 > copy             :: Int -> a -> [a]      -- make list of n copies of x
12 > copy n x          = take n xs where xs = x:xs
13
14
15 > cjustify, ljustify, rjustify :: Int -> String -> String
16
17 > cjustify n s = space halfm ++ s ++ space (m - halfm)
18 >                where m     = n - length s
19 >                      halfm = m `div` 2 
20 > ljustify n s = s ++ space (n - length s)
21 > rjustify n s = space (n - length s) ++ s
22
23 > space       :: Int -> String
24 > space n      = copy n ' '
25
26 > layn        :: [String] -> String
27 > layn         = lay (1::Int) where lay _ []     = []
28 >                                   lay n (x:xs) = rjustify 4 (show n) ++ ") "
29 >                                            ++ x ++ "\n" ++ lay (n+1) xs
30
31 > -- Merging and sorting lists:
32
33 > merge               :: Ord a => [a] -> [a] -> [a]
34 > merge []     ys      = ys
35 > merge xs     []      = xs
36 > merge (x:xs) (y:ys)
37 >         | x <= y     = x : merge xs (y:ys)
38 >         | otherwise  = y : merge (x:xs) ys
39
40 > sort                :: Ord a => [a] -> [a]
41 > sort                 = foldr insert []
42
43 > insert              :: Ord a => a -> [a] -> [a]
44 > insert x []          = [x]
45 > insert x (y:ys)
46 >         | x <= y     = x:y:ys
47 >         | otherwise  = y:insert x ys
48
49 > qsort               :: Ord a => [a] -> [a]
50 > qsort []             = []
51 > qsort (x:xs)         = qsort [ u | u<-xs, u<x ] ++
52 >                              [ x ] ++
53 >                        qsort [ u | u<-xs, u>=x ]
54
55 > --1.3: undefined = error "undefined"
56
57 \end{vb}
58
59