Created
October 18, 2011 01:24
-
-
Save passingloop/1294391 to your computer and use it in GitHub Desktop.
7l7w-haskell-day-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Passingloop where | |
import Char | |
wrapJoin :: Int -> (String, Int) -> (String, Int) -> (String, Int) | |
wrapJoin n (str, lineLength) (word, length) | |
| lineLength + length < n = (str ++ " " ++ word, lineLength + length) | |
| otherwise = (str ++ "\n" ++ word, length) | |
wrap :: Int -> String -> String | |
wrap n str = fst $ foldl1 (wrapJoin n) [ (w, length w) | w <- words str] | |
number :: Int -> String -> String | |
number n l = [intToDigit n] ++ ": " ++ l | |
numberLines :: String -> String | |
numberLines str = unlines $ zipWith number [1 ..] (lines str) | |
justify :: Int -> String -> String | |
justify n l = take n $ l ++ repeat '_' | |
alignLeft :: Int -> (String -> String) | |
alignLeft n = unlines . (map (justify n)) . lines | |
alignRight :: Int -> (String -> String) | |
alignRight n = unlines . (map (reverse . (justify n) . reverse)) . lines | |
padding :: Int -> String -> String | |
padding n str = (take ((n - length str) `div` 2) $ repeat '_') ++ str | |
center :: Int -> (String -> String) | |
center n = unlines . (map ((justify n) . (padding n))) . lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://passingloop.tumblr.com/post/11619723957/7l7w-haskell-day-2-homework