How to change typing in Haskell

Asked

Viewed 85 times

2

I want the function to draw a number and, if it is larger than 7, send an approval message and call this add function. However, my function only falls in the 'Else'. The "failed" message appears. I believe it is the typing of IO Float with Float. How to solve?

sortear :: Float -> Int
sortear x = ceiling(10 * x)


numeroSorteado :: IO ()
numeroSorteado = do    
    num <- randomIO :: IO Float
    print $ sortear num
    if(num >= 7) then
        do
            putStrLn ("Aprovado!" ++ "\n") >> adicionar
        else
        do
            putStrLn "Reprovado!"

adicionar = do
    putStrLn "Nome:"
    nome <- getLine
    putStrLn "Sobrenome:"
    sobrenome <- getLine
    putStrLn "CPF:"
    cpf <- getLine
    putStrLn "Idade:"
    idade <- getLine
    putStrLn "Salario:"
    salario <- getLine
    putStrLn "Cargo (Estagiario, Analista, Gerente, Diretor, VicePresidente ou Presidente) :"
    cargo <- getLine
    let new =  (nome ++ " "++ sobrenome ++ " " ++ cpf ++ " " ++ idade ++ " " ++ salario ++ " " ++ cargo ++ "\n")
    appendFile "funcionarios.txt" new
    putStrLn "Funcionario Salvo!"
  • The function adicionar It doesn’t seem important to reproduce your problem. It is recommended that you only enter the code of what matters, and delete the rest to avoid distractions. This is called minimum example: https://answall.com/help/mcve

1 answer

2


The problem in your code is due to the line containing the following information:

in a <- randomly :: IO Float

This in a shall never be greater than 1, since randomIO :: IO Float generate only floating-comma numbers that are less than 1.

A solution to this problem will be the implementation of the function random type randomRIO :: Random a => (a, a) -> IO a which will generate a number limited to a certain interval.

A possible resolution:

numeroSorteado :: IO ()
numeroSorteado = do    
       num <- randomRIO (1::Float,20::Float)
       print $ sortear num
       if(num >= 7) then
           do
               putStrLn ("Aprovado!" ++ "\n") >> adicionar
           else
           do
               putStrLn "Reprovado!"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.