Ich möchte eine einfache Funktion schreiben, die eine Zeichenfolge aus der Konsole liest und sie in einen benutzerdefinierten Datentyp parst.Einfache Eingabefunktion Haskell
Mein Versuch:
data Custom = A | B Int | C String deriving Read
getFormula::IO Custom
getFormula = do
putStrLn("Introduce una formula: ")
toParse <- getLine
return read toParse::Custom
Aber das funktioniert nicht, und ich weiß nicht, wie der resultierenden Übersetzungsfehler zu interpretieren. Wie behebe ich das? Was verstehe ich falsch darüber, wie IO-Funktionen funktionieren?
EDIT: Das ist der Fehler, den ich bekomme, wenn ich versuchen, die Datei in 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 }
Sie könnten 'readLn' direkt anstelle von' getLine' und 'read' verwenden. – 4castle