3 > module GoferPreludeBits where
5 This script contains some useful functions taken from the standard
6 Gofer prelude, but which do not appear in the Haskell prelude
11 > copy :: Int -> a -> [a] -- make list of n copies of x
12 > copy n x = take n xs where xs = x:xs
15 > cjustify, ljustify, rjustify :: Int -> String -> String
17 > cjustify n s = space halfm ++ s ++ space (m - halfm)
18 > where m = n - length s
20 > ljustify n s = s ++ space (n - length s)
21 > rjustify n s = space (n - length s) ++ s
23 > space :: Int -> String
24 > space n = copy n ' '
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
31 > -- Merging and sorting lists:
33 > merge :: Ord a => [a] -> [a] -> [a]
37 > | x <= y = x : merge xs (y:ys)
38 > | otherwise = y : merge (x:xs) ys
40 > sort :: Ord a => [a] -> [a]
41 > sort = foldr insert []
43 > insert :: Ord a => a -> [a] -> [a]
47 > | otherwise = y:insert x ys
49 > qsort :: Ord a => [a] -> [a]
51 > qsort (x:xs) = qsort [ u | u<-xs, u<x ] ++
53 > qsort [ u | u<-xs, u>=x ]
55 > --1.3: undefined = error "undefined"