Skip to content

Instantly share code, notes, and snippets.

@jrm2k6
Last active December 29, 2015 03:19
Show Gist options
  • Select an option

  • Save jrm2k6/7607022 to your computer and use it in GitHub Desktop.

Select an option

Save jrm2k6/7607022 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text as T
import Data.Char (isDigit)
import System.IO
import Data.List (partition, intersperse)
import System.Environment (getArgs)
main = do
args <- getArgs
content <- readFile(args !! 0)
let l = lines content
let lf = filter (\x -> length x > 0) l
mapM_ (putStrLn . arrange) lf
tokens :: String -> [String]
tokens [] = []
tokens s =
case break(== ',') s of
(x, []) -> [x]
(x, _:xs) -> x:tokens xs
untokens :: [String] -> String
untokens l = concat . intersperse "," $ l
isANumber :: String -> Bool
isANumber = all isDigit
-- ["i", "am", "the", "12", "light"]
separate :: [String] -> ([String], [String])
separate l = partition (not . isANumber) l
generateSentence :: ([String], [String]) -> String
generateSentence (xa, []) = untokens xa
generateSentence ([], xb) = untokens xb
generateSentence (xa, xb) = untokens xa ++ "|" ++ untokens xb
arrange :: String -> String
arrange = generateSentence . separate . tokens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment