Advertisement
fsimen

Untitled

Apr 22nd, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. dropLine :: Int -> [Word] -> Line
  2. dropLine len [] = []
  3. dropLine len (w:ws)
  4. | len >= length w = dropLine newlen ws
  5. | otherwise = (w:ws)
  6. where newlen = len - (length w)
  7.  
  8. joinLine :: Line -> String
  9. joinLine [] = []
  10. joinLine (x:xs) = (x ++ " ") ++ joinLine xs
  11.  
  12.  
  13. appendSpaceToWord :: Word -> Word
  14. appendSpaceToWord w = w ++ " "
  15.  
  16. appendSpaceToFirstElementOfLine :: Word -> (Int, Line) -> (Int, Line)
  17. appendSpaceToFirstElementOfLine x (n, xs) = (n, (appendSpaceToWord x):xs)
  18.  
  19. joinLineN :: (Int, Line) -> (Int, Line)
  20. joinLineN (n, []) = (n, [])
  21. joinLineN (0, xs) = (0, xs)
  22. joinLineN (n, (x:xs))
  23. | (wordLength x) < n = (fst result, snd result)
  24. | otherwise = (n, (x:xs))
  25. where result = appendSpaceToFirstElementOfLine x (joinLineN (left, xs))
  26. wordLength x = length x
  27. left = n - wordLength x
  28.  
  29. lineLength :: Line -> Int
  30. lineLength line = Prelude.sum [length x | x <- line]
  31.  
  32. joinLineA :: Int -> Line -> Line
  33. joinLineA 0 xs = xs
  34. joinLineA _ [] = []
  35. joinLineA n xs
  36. | n > 0 = joinLineA (fst result) (snd result)
  37. | otherwise = snd result
  38. where
  39. result = joinLineN (n, xs)
  40.  
  41.  
  42.  
  43.  
  44. joinLines :: [Line] -> String
  45. joinLines [] = []
  46. joinLines (x:xs) = (line ++ "\n") ++ joinLines xs
  47. where line = joinLine x
  48.  
  49.  
  50. -- Splitting into lines.
  51.  
  52. splitLines :: [Word] -> [Line]
  53. splitLines [] = []
  54. splitLines ws
  55. = getLine lineLen ws
  56. : splitLines (dropLine lineLen ws)
  57.  
  58. -- To fill a text string into lines, we write
  59.  
  60. fill :: String -> [Line]
  61. fill = splitLines . splitWords
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement