Created
February 3, 2016 16:50
-
-
Save chrisdone/d4362d1302c9dd539b9b to your computer and use it in GitHub Desktop.
Pattern matching bits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE ViewPatterns #-} | |
{-# LANGUAGE PatternSynonyms #-} | |
-- | Demonstration of pattern matching on bits in a byte. | |
module X where | |
import Data.Bits | |
pattern Byte a b c d <- (byte -> (ByteBits a b c d)) | |
data ByteBits = ByteBits Bool Bool Bool Bool | |
byte :: Bits a => a -> ByteBits | |
byte n = ByteBits (testBit n 3) (testBit n 2) (testBit n 1) (testBit n 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment