Created
July 20, 2024 14:30
-
-
Save emanoelbarreiros/d843a354e8e7d7a6145f4b554f95d007 to your computer and use it in GitHub Desktop.
Resolução da lista de interatividade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lib | |
( someFunc, getLine', putStr', putStrLn', putStr'', acumular, somador, somador2, obterLinha | |
) where | |
import Text.Printf | |
import System.IO | |
someFunc :: IO () | |
someFunc = putStrLn "someFunc" | |
getLine' :: IO String | |
getLine' = do x <- getChar | |
if x == '\n' then | |
return [] | |
else | |
do xs <- getLine' | |
return (x:xs) | |
putStr' :: String -> IO () | |
putStr' [] = return () | |
putStr' (x:xs) = do putChar x | |
putStr' xs | |
putStrLn' :: String -> IO () | |
putStrLn' [] = return () | |
putStrLn' s = do putStr' s | |
putChar '\n' | |
putStr'' :: String -> IO () | |
putStr'' [] = return () | |
putStr'' s = sequence_ [putChar c | c <- s] | |
somador :: IO () | |
somador = do putStr "Quantos números? " | |
n <- readLn :: IO Int | |
soma <- acumular n 0 | |
putStr $ printf "O total é %d" soma | |
acumular :: Int -> Int -> IO Int | |
acumular 0 acc = return acc | |
acumular n acc = do x <- readLn :: IO Int | |
acumular (n - 1) (x + acc) | |
somador2 :: IO () | |
somador2 = do putStr "Quantos números? " | |
n <- readLn :: IO Int | |
ns <- sequence [readLn :: IO Int | _ <- [1 .. n]] | |
putStr $ show $ sum ns | |
obterChar:: IO Char | |
obterChar = do hSetEcho stdin False | |
x <- getChar | |
hSetEcho stdin True | |
return x | |
obterLinha :: IO String | |
obterLinha = do x <- obterChar | |
if x == '\n' then | |
do putChar x | |
return [] | |
else | |
if x == '\DEL' then | |
do putStr "\b \b" | |
return "\DEL" | |
else | |
do putChar x | |
xs <- obterLinha | |
if xs == "\DEL" then | |
do obterLinha | |
else | |
return (x:xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment