home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / Gofer.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  1.9 KB  |  59 lines

  1. -----------------------------------------------------------------------------
  2. -- Utility functions, for compatibility with Gofer prelude & Bird and Wadler:
  3. --
  4. -- Suitable for use with Hugs 98.
  5. -----------------------------------------------------------------------------
  6.  
  7. module Gofer where
  8.  
  9. -- String formatting: -------------------------------------------------------
  10.  
  11. ljustify, rjustify, cjustify :: Int -> String -> String
  12. ljustify n s                  = s ++ space (n - length s)
  13. rjustify n s                  = space (n - length s) ++ s
  14. cjustify n s                  = space halfm ++ s ++ space (m - halfm)
  15.                                 where m     = n - length s
  16.                                       halfm = m `div` 2
  17.  
  18. space                        :: Int -> String
  19. space n                       = copy n ' '
  20.  
  21. layn        :: [String] -> String
  22. layn         = lay 1 where lay _ []     = []
  23.                            lay n (x:xs) = rjustify 4 (show n) ++ ") "
  24.                                            ++ x ++ "\n" ++ lay (n+1) xs
  25.  
  26. -- Misc. list utilities: ----------------------------------------------------
  27.  
  28. copy                :: Int -> a -> [a]
  29. copy n x             = take n (repeat x)
  30.  
  31. merge               :: Ord a => [a] -> [a] -> [a]
  32. merge []     ys      = ys
  33. merge xs     []      = xs
  34. merge (x:xs) (y:ys)
  35.         | x <= y     = x : merge xs (y:ys)
  36.         | otherwise  = y : merge (x:xs) ys
  37.  
  38. sort                :: Ord a => [a] -> [a]
  39. sort                 = foldr insert []
  40.  
  41. insert              :: Ord a => a -> [a] -> [a]
  42. insert x []          = [x]
  43. insert x (y:ys)
  44.         | x <= y     = x:y:ys
  45.         | otherwise  = y:insert x ys
  46.  
  47. -- Other functions: ---------------------------------------------------------
  48.  
  49. fst3                :: (a,b,c) -> a
  50. fst3 (x,_,_)         = x
  51.  
  52. snd3                :: (a,b,c) -> b
  53. snd3 (_,x,_)         = x
  54.  
  55. thd3                :: (a,b,c) -> c
  56. thd3 (_,_,x)         = x
  57.  
  58. -----------------------------------------------------------------------------
  59.