Odd word problem: Difference between revisions

m
(Added Wren)
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).
<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 = break $ not .span isAlpha
 
parse :: String -> String
parse [] = []
parse l =
let (a, w) = split l
(b, x) = splitAt 1 w
(c, y) = split x
(d, z) = splitAt 1 y
in a ++<> b ++<> reverse c ++<> d ++<> parse z
 
main :: IO ()
main =
main = hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >> getContents >>=
getContents >>= putStr . (takeWhile (/= '.')) . parse >> putStrLn "."</lang>
putStrLn "."</lang>
If the above is not acceptable, or if Haskell was implicitly strict, then this solution would satisfy the requirements:
<lang Haskell>isAlpha :: Char -> Bool
9,655

edits