[Haskell-cafe] What is a Haskell way to implement flags?

Donn Cave donn at avvanta.com
Tue Feb 19 17:59:58 CET 2013


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



More information about the Haskell-Cafe mailing list