次のような標準入力があります。
1 2 3
4 5 6
1 3 2
5 3 2
...
各行は正確に 3 つの数値で構成され、関数値を計算したい行ごとに
f :: (Int, Int, Int) -> Int
結果を印刷します。どうやってするか?
あなたの探求に役立ついくつかの便利な機能を指摘します。
readFile :: FilePath -> IO String
lines :: String -> [String]
words :: String -> [String]
print :: (Show a) => a -> IO ()
ghci
セッションを開いて、これらの機能を少し試して、その機能を確認し、創造的な方法でそれらを組み合わせてみることをお勧めします。私はあなたに有利なスタートを与えます:
Prelude> str <- readFile "test.txt"
Prelude> print (length (lines str))
<The number of lines in the file "test.txt">
簡単な解決策:
tuple3 :: [Int] -> (Int,Int,Int)
tuple3 [x,y,z] = (x,y,z)
main = mapM_ print . map (f . tuple3 . map read . words) . lines =<< getContents
タプルを引数として使用する理由はまだわかりません。それは醜い変換を引き起こします。