Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Codewars.Kata.Format where
- import Data.Char
- format :: String -> Int -> String
- format str n = if length str <= n then str
- else init $ unlines $ formatM n $ wordsWithSpaces str
- formatM :: Int -> [String] -> [String]
- formatM _ [] = []
- formatM x y = [removeLastSpace $ concat $ parsed] ++ formatM x (dropS (length parsed) y)
- where parsed = parseW x y
- removeLastSpace :: String -> String
- removeLastSpace x = if isSpace $ last x then init x else x
- dropS :: Int -> [String] -> [String]
- dropS x y = if firstElementIsSpace dropped then tail dropped
- else dropped
- where dropped = drop x y
- firstElementIsSpace :: [String] -> Bool
- firstElementIsSpace x = if null $ take 1 x then False
- else isSpace $ head $ head $ take 1 x
- parseW :: Int -> [String] -> [String]
- parseW x y = parse 0 x y
- parse :: Int -> Int -> [String] -> [String]
- parse _ _ [] = []
- parse x y (a:ax) | len < y = [a] ++ parse len y ax
- | len == y = if isSpace $ head a then [] else [a]
- | len > y = []
- where len = (+x) $ length a
- wordsWithSpaces :: String -> [String]
- wordsWithSpaces = addSpace . words
- addSpace :: [String] -> [String]
- addSpace [] = []
- addSpace (a:[]) = [a]
- addSpace (a:ax) = [a] ++ [" "] ++ addSpace ax
Advertisement
Add Comment
Please, Sign In to add comment