Branimir Maksimovic | 19 Feb 16:11 2013
Picon

What is a Haskell way to implement flags?

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
mukesh tiwari | 19 Feb 17:23 2013
Picon

Re: What is a Haskell way to implement flags?

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


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Donn Cave | 19 Feb 17:59 2013

Re: What is a Haskell way to implement flags?

Quoth Branimir Maksimovic <bmaxa <at> hotmail.com>,
>  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?

Of course you may do the very same thing, if you like.  I think if
there's only one of these for the entire program, most Haskell programmers
would use a Boolean value, as the space you save with a single bit
is of too little consequence to bother with bit twiddling in Haskell.
The various flags that belong together might be housed in a `record'.

Or, in some contexts a list of an algebraic type might be useful -
like,  "if elem FlagDebug flags", or
       "if any (flip elem flags) [FlagDebug, FlagVerbose]"

	Donn
Brandon Allbery | 19 Feb 18:07 2013
Picon

Re: What is a Haskell way to implement flags?

On Tue, Feb 19, 2013 at 10:11 AM, 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?

You can do that, but a somewhat more idiomatic way would be a list (or, slightly less conveniently but more accurately, a Data.Set) of constructors from a flags ADT.

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b <at> gmail.com                                  ballbery <at> sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Ertugrul Söylemez | 20 Feb 00:39 2013
Picon

Re: What is a Haskell way to implement flags?

Brandon Allbery <allbery.b <at> gmail.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?
>
> You can do that, but a somewhat more idiomatic way would be a list
> (or, slightly less conveniently but more accurately, a Data.Set) of
> constructors from a flags ADT.

The Set way is the one I would prefer.  In fact together with lenses you
even get the boolean interface and a nice interface in general.  Define
your option types:

    data Flag =
        Debug | Verbose
        deriving (Ord)

    data Options =
        Options {
          _optFiles :: Set FilePath,
          _optFlags :: Set Flag
        }

    makeLenses ''Options

The fun starts when you have a state monad around Options, because then
you can use lenses very easily.  Let's add a file:

    optFiles . contains "blah.txt" .= True

Let's set the Verbose flag:

    optFlags . contains Verbose .= True

Let's flip the Verbose flag:

    optFlags . contains Verbose %= not

Are we verbose?

    verbose <- use (optFlags . contains Verbose)

Have fun. =)

Greets,
Ertugrul

--

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane