n
行の文字列を文字列のリストに読み込もうとしています。私は下のコードのいくつかのバリエーションを試しましたが、何も機能しませんでした。n行を[String]に読み込みます
main = do
input <- getLine
inputs <- mapM getLine [1..read input]
print $ length input
これは、次のエラーがスローされます。
Couldn't match expected type `a0 -> IO b0'
with actual type `IO String'
In the first argument of `mapM', namely `getLine'
In a stmt of a 'do' block: inputs <- mapM getLine [1 .. read input]
In the expression:
do { input <- getLine;
inputs <- mapM getLine [1 .. read input];
print $ length input }
そして
main = do
input <- getLine
let inputs = map getLine [1..read input]
print $ length input
は
Couldn't match expected type `a0 -> b0'
with actual type `IO String'
In the first argument of `map', namely `getLine'
In the expression: map getLine [1 .. read input]
In an equation for `inputs': inputs = map getLine [1 .. read input]
私はこれをどのように行うことができますがスローされますか?
ところで、 'inputs < - mapM(const getLine)[1 .. read input]'は必要なものを正確に行います。問題は、 'getLine'を数字' [1 .. n] 'にマッピングしようとしていますが、' getLine'は関数ではありません。 'const'を使うことで、最初の引数を無視する関数に変換します。 – Vitus
@Vitusそれは別の答えとして価値がある、私はそれを+1します。 – vikingsteve