crislanio_macedo Posted March 23, 2015 at 03:34 AM Report Share #580003 Posted March 23, 2015 at 03:34 AM (edited) Olá pessoal estou a fazer um programa que irá acumular um valor a medida que eu digito, como eu farei isto. Terei que converter duma string para um numero para depois somar ? me ajudem. import Control.Monad main = forever $ do putStrLn "Informe um numero para ir somando, ou digite 0 para sair" numero <- readLn when (numero /= 0) $ do -- here putStrLn $ "Soma " ++ (show numero ) main No caso como faria diferente disto. OU seja sem tantas funções leInt :: IO (Int) leInt = do putStr "Digite um valor inteiro: " readLn -- Ler um número e imprimir a lista de 0 a n lista::IO () lista = do y <- leInt print [0..y] -- Imprimir a soma de vários números, digitando cada um deles geraSoma::IO () geraSoma = do putStrLn "Entre com varios numeros (até 0)" z <- soma print (z) soma::IO Int soma = do x <- leInt if (x==0) then return 0 else (do y <- soma return (x+y)) Edited March 23, 2015 at 03:36 AM by crislanio_macedo Link to comment Share on other sites More sharing options...
thoga31 Posted March 23, 2015 at 12:32 PM Report Share #580030 Posted March 23, 2015 at 12:32 PM A função forever não é necessária neste caso 😉 action xs = do x <- readLn if (x /= 0) then action $ x:xs else do putStrLn . show $ (xs, sum xs) main = do action [] Knowledge is free! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now