いくつかのコマンド ライン引数を取るプログラムがあります。
最初のコマンド ライン引数が整数のカンマ区切り値 (CSV) リストであるとします。
"1,2,4,8,16"
最初の引数をに変換したい[1,2,4,8,16]
。文字列をリストに解析しようとしましたInt
が、コンパイル エラーが発生しました。
Haskell コード:
import System.Environment
import Data.List
import Text.Regex
main = do
args <- getArgs
ints <- if (length args > 1)
then (mapM read (splitRegex (mkRegex ",") (args!!1)))
else [1,3,5] -- defaults
print (ints)
コンパイル エラー:
myProg.hs:10:16:
Couldn't match expected type `IO' with actual type `[]'
In the expression: [1, 3, 5]
In a stmt of a 'do' block:
ints <- if (length args > 1) then
(mapM read (splitRegex (mkRegex ",") (args !! 1)))
else
[1, 3, 5]
In the expression:
do { args <- getArgs;
ints <- if (length args > 1) then
(mapM read (splitRegex (mkRegex ",") (args !! 1)))
else
[1, ....];
print (ints) }
このタイプのエラーが何を意味するのかわかりません。誰かが型エラーを説明し、コードを変更して目的の結果を達成する方法を教えていただければ幸いです。