Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- It's an educational app that turns sentences into sets of words and
- -- shuffles the words. Characters that don't make up words, e.g.
- -- punctuation marks, are removed, capital letters are replaced with
- -- their lowercase counterparts. The sentences are read from stdin.
- -- Each sentence should be on a separate line. If a sentence consists
- -- of only one word, the app changes the order of the letters in the
- -- word. The result is written to stdout as sets of comma-separated
- -- words, each set is on a separate line. The order of the shuffled
- -- words isn't random, but its "randomness" is sufficient. A
- -- "sentence" like this "One two three, four five six — seven eight
- -- nine; ten eleven twelve: thirteen fourteen fifteen." will be
- -- changed into the following set of words: "four, eleven, seven,
- -- twelve, two, thirteen, eight, fourteen, one, fifteen, nine, five,
- -- ten, six, three".
- import Data.List (intercalate, sort)
- import Data.Char (toLower)
- import System.Exit (die)
- main :: IO ()
- main = do
- input <- getContents
- if null input
- then die "No input given!"
- else mapM_ putStrLn $
- listsOfWordsToLines $
- map
- ((shuffleList . processSingleWords) . words)
- (lines $ proccessChars input)
- processChars :: String -> String
- processChars [] = []
- processChars string =
- filter (`elem` "abcdefghijklmnopqrstuvwxyz’'- \n") $ map toLower string
- processSingleWords :: [String] -> [String]
- processSingleWords [] = []
- processSingleWords [x]
- | x == sort x = [reverse x]
- | otherwise = [sort x]
- processSingleWords x = x
- increasingChunks :: [String] -> [[String]]
- increasingChunks [] = []
- increasingChunks list = aux 1 list
- where
- aux _ [] = []
- aux n lst = take n lst : aux (n + 1) (drop n lst)
- interlaceLists :: [String] -> [String] -> [String]
- interlaceLists x [] = x
- interlaceLists [] x = x
- interlaceLists listOne@(x:xs) listTwo@(y:ys)
- | length listOne > length listTwo = x : y : interlaceLists xs ys
- | otherwise = y : x : interlaceLists ys xs
- shuffleList :: [String] -> [String]
- shuffleList [] = []
- shuffleList list = foldl1 interlaceLists $ increasingChunks list
- listsOfWordsToLines :: [[String]] -> [String]
- listsOfWordsToLines [[]] = []
- listsOfWordsToLines list = map (intercalate ", ") list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement