Odd word problem: Difference between revisions

Content added Content deleted
(Added Wren)
Line 1,121: Line 1,121:
While it seems like this solution would break the task's rules, Haskell is non-strict, therefore this yields the same behavior of reading and printing one character at a time, without excess storage into a "string". To prove it, run the program and manually enter the input string (Windows command prompt does not respect buffering settings, but urxvt on on Linux does).
While it seems like this solution would break the task's rules, Haskell is non-strict, therefore this yields the same behavior of reading and printing one character at a time, without excess storage into a "string". To prove it, run the program and manually enter the input string (Windows command prompt does not respect buffering settings, but urxvt on on Linux does).
<lang Haskell>import System.IO
<lang Haskell>import System.IO
(BufferMode(..), getContents, hSetBuffering, stdin, stdout)

import Data.Char (isAlpha)
isAlpha :: Char -> Bool
isAlpha = flip elem $ ['a'..'z'] ++ ['A'..'Z']


split :: String -> (String, String)
split :: String -> (String, String)
split = break $ not . isAlpha
split = span isAlpha


parse :: String -> String
parse :: String -> String
parse [] = []
parse [] = []
parse l =
parse l =
let (a, w) = split l
let (a, w) = split l
(b, x) = splitAt 1 w
(b, x) = splitAt 1 w
(c, y) = split x
(c, y) = split x
(d, z) = splitAt 1 y
(d, z) = splitAt 1 y
in a ++ b ++ reverse c ++ d ++ parse z
in a <> b <> reverse c <> d <> parse z


main :: IO ()
main :: IO ()
main =
main = hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >>
hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >> getContents >>=
getContents >>= putStr . (takeWhile (/= '.')) . parse >> putStrLn "."</lang>
putStr . takeWhile (/= '.') . parse >>
putStrLn "."</lang>
If the above is not acceptable, or if Haskell was implicitly strict, then this solution would satisfy the requirements:
If the above is not acceptable, or if Haskell was implicitly strict, then this solution would satisfy the requirements:
<lang Haskell>isAlpha :: Char -> Bool
<lang Haskell>isAlpha :: Char -> Bool