Haskell Iteratee ライブラリを使用して、「wc -l」に相当するものを考え出そうとしています。以下は "wc" のコード (単語をカウントするだけです - ハックの iteratee の例のコードに似ています) で、非常に高速に実行されます:
{-# LANGUAGE BangPatterns #-}
import Data.Iteratee as I
import Data.ListLike as LL
import Data.Iteratee.IO
import Data.ByteString
length1 :: (Monad m, Num a, LL.ListLike s el) => Iteratee s m a
length1 = liftI (step 0)
  where
    step !i (Chunk xs) = liftI (step $ i + fromIntegral (LL.length xs))
    step !i stream     = idone i stream
{-# INLINE length1 #-}
main = do
  i' <- enumFile 1024 "/usr/share/dict/words" (length1 :: (Monad m) => Iteratee ByteString m Int)
  result <- run i'
  print result
  {- Time measured on a linux x86 box: 
  $ time ./test ## above haskell compiled code
  4950996
  real    0m0.013s
  user    0m0.004s
  sys     0m0.007s
  $  time wc -c /usr/share/dict/words
  4950996 /usr/share/dict/words
  real    0m0.003s
  user    0m0.000s
  sys     0m0.002s
  -}
では、実行速度が速すぎる行の数をカウントするように拡張するにはどうすればよいでしょうか。Prelude.filter を使用して "\n" のみをフィルター処理するバージョンを作成しましたが、メモリが多すぎるため、Linux の "wc -l" と gc (遅延評価だと思います) よりも遅くなります。そのため、Data.ListLike.filter を使用して別のバージョンを作成しましたが、型チェックがないためコンパイルされません。ここで助けていただければ幸いです。
  {-# LANGUAGE BangPatterns #-}
  import Data.Iteratee as I
  import Data.ListLike as LL
  import Data.Iteratee.IO
  import Data.ByteString
  import Data.Char
  import Data.ByteString.Char8 (pack)
  numlines :: (Monad m, Num a, LL.ListLike s el) => Iteratee s m a
  numlines = liftI $ step 0
    where
      step !i (Chunk xs) = liftI (step $i + fromIntegral (LL.length $ LL.filter (\x ->  x == Data.ByteString.Char8.pack "\n")  xs))
      step !i stream = idone i stream
  {-# INLINE numlines #-}
  main = do
    i' <- enumFile 1024 "/usr/share/dict/words" (numlines :: (Monad m) => Iteratee ByteString m Int)
    result <- run i'
    print result