コンソールから文字列を読み込み、それをカスタムデータ型に解析する単純な関数を書いてみたいと思います。単純な入力関数Haskell
私の試み:
data Custom = A | B Int | C String deriving Read
getFormula::IO Custom
getFormula = do
putStrLn("Introduce una formula: ")
toParse <- getLine
return read toParse::Custom
しかし、これは動作しませんし、私は結果のコンパイルエラーを解釈する方法がわかりません。これをどうやって解決するのですか? IO機能の仕組みについて私が誤解していることは何ですか?
EDIT:これは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 }
'getLine'と' read'の代わりに 'readLn'を直接使うことができます。 – 4castle