wvogel日記

自分用の技術備忘録が多めです.

scanf

Haskellで使えるようなscanf関数が欲しいなあと思っていた
競技プログラミングの問題を解いていると、要素として受け取る数を指定し、その数だけ値を読み込む、というのが結構ある。


なので、簡易的なものを書いてみた

module Scan where

scanf :: String -> IO [String]
scanf n = getContents
           >>= return. take (read n).lines

使用例。

import Scan

main = print.maximum.map (f.words) =<< scanf =<< getLine

f :: [String]->Int
f (a:b:_) = read a + read b