Odd word problem: Difference between revisions
Content added Content deleted
(Added Wren) |
m (→{{header|Haskell}}: Tidying.) |
||
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 = |
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 |
in a <> b <> reverse c <> d <> parse z |
||
main :: IO () |
main :: IO () |
||
main = |
|||
hSetBuffering stdin NoBuffering >> hSetBuffering stdout NoBuffering >> getContents >>= |
|||
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 |