I want to write a simple function that reads a string from console and parses it to a custom data type.
My attempt:
data Custom = A | B Int | C String deriving Read
getFormula::IO Custom
getFormula = do
putStrLn("Introduce una formula: ")
toParse <- getLine
return read toParse::Custom
But this does not work and I do not know how to interpret the resulting compiling error. How do I fix this? What am I misunderstanding about how IO functions work?
EDIT: This is the error I get when I try to load the file into GCHI
test.hs:7:5:
Couldn't match type ‘String -> a0’ with ‘Custom’
Expected type: String -> Custom
Actual type: String -> String -> a0
The function ‘return’ is applied to two arguments,
but its type ‘(String -> a0) -> String -> String -> a0’
has only three
In a stmt of a 'do' block: return read toParse :: Custom
In the expression:
do { putStrLn ("Introduce una formula: ");
toParse <- getLine;
return read toParse :: Custom }
test.hs:7:5:
Couldn't match expected type ‘IO Custom’ with actual type ‘Custom’
In a stmt of a 'do' block: return read toParse :: Custom
In the expression:
do { putStrLn ("Introduce una formula: ");
toParse <- getLine;
return read toParse :: Custom }
In an equation for ‘getFormula’:
getFormula
= do { putStrLn ("Introduce una formula: ");
toParse <- getLine;
return read toParse :: Custom }
readLndirectly insteadgetLineandread.