整数「n」とリストを取り、リストを「n」個のリストに分割するこのコードを取得しました。
chunk n xs = chunk' i xs
where
chunk' _ [] = []
chunk' n xs = a : chunk' n b where (a,b) = splitAt n xs
i = ceiling (fromIntegral (length xs) / fromIntegral n)
そして、これはそれがどのように機能するかの例です:
*Main> chunk 5 [1..10]
[[1,2],[3,4],[5,6],[7,8],[9,10]]
私はこれを Data.ByteString ライブラリで動作させようとしましたが、理解できません。
これは、使用しようとしているコードです。
import qualified Data.ByteString as B
B.readFile "meow.txt" >>= (\x -> return $ chunk 4 x)
そして、これは私に与えるエラーです:
<interactive>:402:51:
Couldn't match expected type `[a10]'
with actual type `B.ByteString'
In the second argument of `chunk', namely `x'
In the second argument of `($)', namely `chunk 4 x'
In the expression: return $ chunk 4 x
型の不一致の問題のようですが、fromIntegral
. バイト文字列を受け入れるチャンク関数を取得する方法はありますか?
この関数での私の目標は、任意の長さのバイナリ ファイルを厳密に受け入れ、プロセスでデータを失うことなくほぼ同じ長さの 4 つの部分に分割することです。