home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / hugs101.zip / hugs101o.zip / Progs / HUGS / Demos / utils.hs < prev   
Text File  |  1995-02-14  |  1KB  |  50 lines

  1. -- Some simple utilities, included for compatibility with the Gofer prelude
  2.  
  3. copy    :: Int -> a -> [a]
  4. copy n x = take n (repeat x)
  5.  
  6. cjustify, ljustify, rjustify :: Int -> String -> String
  7.  
  8. cjustify n s = space halfm ++ s ++ space (m - halfm)
  9.                where m     = n - length s
  10.                      halfm = m `div` 2
  11. ljustify n s = s ++ space (n - length s)
  12. rjustify n s = space (n - length s) ++ s
  13.  
  14. space       :: Int -> String
  15. space n      = copy n ' '
  16.  
  17. layn        :: [String] -> String
  18. layn         = lay 1 where lay _ []     = []
  19.                            lay n (x:xs) = rjustify 4 (show n) ++ ") "
  20.                                            ++ x ++ "\n" ++ lay (n+1) xs
  21.  
  22. merge               :: Ord a => [a] -> [a] -> [a]
  23. merge []     ys      = ys
  24. merge xs     []      = xs
  25. merge (x:xs) (y:ys)
  26.         | x <= y     = x : merge xs (y:ys)
  27.         | otherwise  = y : merge (x:xs) ys
  28.  
  29. sort                :: Ord a => [a] -> [a]
  30. sort                 = foldr insert []
  31.  
  32. insert              :: Ord a => a -> [a] -> [a]
  33. insert x []          = [x]
  34. insert x (y:ys)
  35.         | x <= y     = x:y:ys
  36.         | otherwise  = y:insert x ys
  37.  
  38. fst3           :: (a,b,c) -> a
  39. fst3 (x,_,_)    = x
  40.  
  41. snd3           :: (a,b,c) -> b
  42. snd3 (_,x,_)    = x
  43.  
  44. thd3           :: (a,b,c) -> c
  45. thd3 (_,_,x)    = x
  46.  
  47. run             :: (String -> String) -> Dialogue
  48. run f            = echo False exit (interact f)
  49.  
  50.