Skip to content

Instantly share code, notes, and snippets.

@spion
Created June 10, 2014 12:15
Show Gist options
  • Save spion/491b49bd64a2cc440b28 to your computer and use it in GitHub Desktop.
Save spion/491b49bd64a2cc440b28 to your computer and use it in GitHub Desktop.
module Tets where
getLine' :: IO [Char]
getLine' = getChar >>= \c ->
if c == '\n' then
return []
else
getLine' >>= \cs ->
return (c : cs)
getLine'' :: IO [Char]
getLine'' =
sequenceWhile notNewline $ repeat getChar
where notNewline c = not (c == '\n')
sequenceWhile :: (a -> Bool) -> [IO a] -> IO [a]
sequenceWhile _ [] = return []
sequenceWhile f (op:ops) =
op >>= \result ->
if f result then
sequenceWhile f ops >>= \results ->
return (result : results)
else
return []
main :: IO ()
main = getLine'' >>= putStrLn
@bor0
Copy link

bor0 commented Jun 10, 2014

import Control.Monad (when)

-- when is: when p s = if p then s else return ()

getLine' = do
x <- getChar
putChar x
when (x /= '\n') getLine'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment