Skip to content

Instantly share code, notes, and snippets.

@kfl
Created November 13, 2012 09:33
Show Gist options
  • Save kfl/4064879 to your computer and use it in GitHub Desktop.
Save kfl/4064879 to your computer and use it in GitHub Desktop.
Capitalise text in Haskell
module Lorem where
import Control.Monad(replicateM_)
import System.Environment(getArgs)
import qualified System.IO as SIO
import qualified Data.Char as C
import qualified Data.ByteString.Char8 as BC
import qualified Data.Word8 as W8
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
genText filename n = do
outh <- SIO.openFile filename SIO.WriteMode
replicateM_ n $ BC.hPutStrLn outh lorem
where lorem = BC.pack "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
processC file = do
str <- BC.readFile file
BC.putStr $ title str
where title bs =
snd $ BC.mapAccumL (\prev c -> (c, if C.isSpace prev then C.toUpper c else c)) ' ' bs
processB file = do
str <- B.readFile file
B.putStr $ title str
where title bs =
snd $ B.mapAccumL (\prev c -> (c, if W8.isSpace prev then W8.toUpper c else c)) W8._space bs
processBL file = do
str <- BL.readFile file
BL.putStr $ title str
where title bs =
snd $ BL.mapAccumL (\prev c -> (c, if W8.isSpace prev then W8.toUpper c else c)) W8._space bs
main :: IO()
main = do
args <- getArgs
case args of
["-g", f, n] -> genText f (read n)
["-c", f] -> processC f
["-b", f] -> processB f
["-l", f] -> processBL f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment