[Haskell-cafe] What is a Haskell way to implement flags?
mukesh tiwari
mukeshtiwari.iiitm at gmail.com
Tue Feb 19 17:23:49 CET 2013
The same as C way. You can import Data.Bits and can use the functions.
Prelude> import Data.Bits
Prelude Data.Bits> Data.Bits.
Data.Bits..&. Data.Bits.bitDefault Data.Bits.complementBit
Data.Bits.rotate Data.Bits.shift
Data.Bits.testBitDefault
Data.Bits..|. Data.Bits.bitSize Data.Bits.isSigned
Data.Bits.rotateL Data.Bits.shiftL Data.Bits.unsafeShiftL
Data.Bits.Bits Data.Bits.clearBit Data.Bits.popCount
Data.Bits.rotateR Data.Bits.shiftR Data.Bits.unsafeShiftR
Data.Bits.bit Data.Bits.complement
Data.Bits.popCountDefault Data.Bits.setBit
Data.Bits.testBit Data.Bits.xor
Prelude Data.Bits> (.&.) 1 2
0
Prelude Data.Bits> (.&.) 2 2
2
I wrote a minimum spanning tree code and rather than maintaining the list
of visited nodes, I took a Integer because of arbitrary precision and set
the bits accordingly.
visited :: Int -> Integer -> ( Bool , Integer )
visited x vis = ( t == 0 , vis' ) where
t = ( B..&. ) ( B.shiftL ( 1 :: Integer ) x ) vis
vis' = ( B..|. ) ( B.shiftL ( 1 :: Integer ) x ) vis
Mukesh
On Tue, Feb 19, 2013 at 8:41 PM, Branimir Maksimovic <bmaxa at hotmail.com>wrote:
> In C usual way is to set some bit in integer variable by shifting or
> oring,
> and than check flag integer variable by anding with particular flag value.
> What is Haskell way?
>
> Thanks.
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130219/52ceaf0b/attachment.htm>
More information about the Haskell-Cafe
mailing list