import IO -- getLine -- getChar -- print incMaybe :: Maybe Int -> Maybe Int incMaybe m = m >>= (\x -> return (x+1)) sumMaybe :: Maybe Int -> Maybe Int -> Maybe Int sumMaybe m1 m2 = m1 >>= (\x -> m2 >>= (\y -> return (x + y))) sumMaybe1 m1 m2 = do x <- m1 y <- m2 return x + y {-- class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a (>>) :: m a -> m b -> m b fail :: String -> m a m >> k = m >>= \_ -> k --} doubleElements :: [Int] -> [Int] doubleElements l = l >>= (\x -> [x,x]) decProduct1 a b = [ (x,y) | x <- a, y <- b ] decProduct2 a b = a >>= (\x -> b >>= (\y -> return (x,y))) decProduct3 a b = do x <- a y <- b return (x,y) -- Main mainDoubleLine :: IO () mainDoubleLine = getLine >>= (\x -> putStrLn x >> putStrLn x) mainDoubleLines :: IO () mainDoubleLines = getLine >>= (\x -> if x == "q" then return () else putStrLn x >> putStrLn x >> mainDoubleLines) getInt :: IO Int getInt = getLine >>= (\s -> return (read s)) mainDoubleInts :: IO () mainDoubleInts = getInt >>= (\x -> if x == 0 then return () else print (2*x) >> mainDoubleInts) mainFilter :: IO () mainFilter = interact (\s -> [c | c <- s, c >= '0', c <= '9']) -- type IO a = RealWorld -> (a, RealWorld) -- ReadLine mainRead = do x <- openFile "/tmp/foo.txt" ReadMode y <- hGetLine x putStr y hClose x mainRead2 = do n <- getLine x <- openFile n ReadMode y <- hGetLine x putStr y hClose x mainWrite = do x <- openFile "/tmp/foo.txt" WriteMode hPutStr x "sss" hClose x