LoginSignup
3
3

More than 5 years have passed since last update.

巡回冗長検査(CRC-32)のHaskellでのゴルフ的コード

Posted at

(説明はあとで書く)

ゴルフっぽいけどゴルフではないコード。

CRC.hs
module CRC (check, calc) where

import Control.Applicative ((<$>))
import Control.Arrow (first, (&&&))
import Data.Array (Array, (!), listArray)
import Data.Bits (complement, xor, shiftR, testBit)
import Data.Bool (bool)
import Data.Word (Word8, Word32)
import qualified Data.ByteString as BS (ByteString, foldl')

check :: BS.ByteString -> Bool
check = (== 0x2144df1c) . calc

calc :: BS.ByteString -> Word32
calc = complement . BS.foldl' (flip st) 0xffffffff
        where st b = uncurry xor . first ((table !) . (xor b))
                . (fromIntegral &&& (`shiftR` 8))

table :: Array Word8 Word32
table = listArray (0, 255) . (<$> [0 .. 255]) . ((!! 8) .) . iterate $
        uncurry (bool id $ xor 0xedb88320) . ((`testBit` 0) &&& (`shiftR` 1))
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3