Jump to content

IO Haskell Ajuda com a função forever


crislanio_macedo
 Share

Recommended Posts

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 by crislanio_macedo
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.