1

I'm trying to get the values from this function, so, when i call that function "prompt" a send a message and this function return an value according with the supposed question that i made.

But i can't receive that value, so why ? I'm noob with haskell.

module Main (main) where
import System.IO (stdout, hSetBuffering, BufferMode(NoBuffering))
type State = Double

main::IO()
prompt::Read a =>  String ->  IO a


main = do putStrLn "Testing"
          v <- prompt "Whats your name?"
          return ()

prompt str = do putStrLn str
                valor <- readLn
                return valor
1
  • Presumably, you want v to be of type String, but the compiler has no way of knowing that. Commented Jun 28, 2018 at 20:37

1 Answer 1

2

Your problem, I believe, is confined to your type signatures.

prompt :: Read a => String -> IO a

This type signature says that prompt is a function which takes a string, does some I/O magic, and then returns a value of type a, for any readable type a the user wishes. However, your implementation gets a string from the user and returns it.

If your intention is that prompt return a string, simply change your type.

prompt :: Read a => String -> IO a

If your intention truly is that prompt return a value of some arbitrary user-specified type, you need to resolve the ambiguity when you actually read the value.

prompt :: Read a => String -> IO a
prompt str = do putStrLn str
                valor <- readLn
                return valor

You'll get an ambiguity error in main, since Haskell can't figure out what type you want v to be. This can be resolved by explicitly specifying.

v <- prompt "Whats your name?" :: IO String

(or, equivalently, IO Int for an integer, IO Double for a double, or any other readable type)

However, more than likely you intended that prompt always return a string. So I recommend the first solution, as it will be more intuitive to users.

Sign up to request clarification or add additional context in comments.

3 Comments

The read valor step is redundant. readLn already has type Read a => IO a
@4castle I learned something today then. I don't think I've ever used readLn for anything other than strings. Thanks for the correction! :)
Perhaps you were thinking of getLine? It does have type IO String

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.