問題A再び

コンパイラ作るつもりがなぜか問題Aの改良を…
Scheme的にはCPSだけど、Haskell的には関数合成だでよ!!
ということで、再実装。
入れ替えを関数にして、それを合成して、それに0を適用。
ついでに入力のパーズをちょっとだけ手直し。
全体的に幾分かすっきりしたのではなかろうか。

main = getContents >>= mapM_ (print.solve) . slice . map (map read . words) . lines where
  slice ([0,0]:_ ) = []
  slice ([n,r]:xs) = (n,take r xs):(slice $ drop r xs)

solve (n,ls) = n - foldr (.) id (map f ls) 0 where
  f [p,c] i = if i<=c then i+p-1 else if i

ときに、↑のソースとか、変なタグが混じるんだけど、これ
どうにかならんのでしょうか?

追記:どうでもいいが、何で上のソース、ガード部使ってないんだ…?
Cソースから難も考えないで写したのがばればれ…

main = getContents >>= mapM_ (print.solve) . slice . map (map read . words) . lines where
  slice ([0,0]:_ ) = []
  slice ([n,r]:xs) = (n,take r xs):(slice $ drop r xs)

solve (n,ls) = n - foldr (.) id (map f ls) 0 where
  f [p,c] i | i <= c    = i+p-1
            | i <  p+c  = i-c
            | otherwise = i