
I have started to program in Haskell. Now I want programm something like flags. It's a set of flags. It sould be possible to convert the Flag as Int/String (the bit combination). I have written this: import Data.Set as Set type Flags = Set Flag data Flag = Flag1 | Flag2 | Flag3 deriving(Eq, Ord, Enum, Show) flagToInt :: Flags -> Int flagToInt flags = flag1 + flag2 + flag3 where flag1 = if member Flag1 flags then 1 else 0 flag2 = if member Flag2 flags then 2 else 0 flag3 = if member Flag3 flags then 4 else 0 but this seems to me to be inelegant. Can somebody help me making it better, please. -- View this message in context: http://www.nabble.com/Flags-tf2083897.html#a5741741 Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

Maduser:
I have started to program in Haskell. Now I want programm something like flags. It's a set of flags. It sould be possible to convert the Flag as Int/String (the bit combination). I have written this:
import Data.Set as Set
type Flags = Set Flag data Flag = Flag1 | Flag2 | Flag3 deriving(Eq, Ord, Enum, Show)
flagToInt :: Flags -> Int flagToInt flags = flag1 + flag2 + flag3 where flag1 = if member Flag1 flags then 1 else 0 flag2 = if member Flag2 flags then 2 else 0 flag3 = if member Flag3 flags then 4 else 0
but this seems to me to be inelegant. Can somebody help me making it better, please.
Something like: type Flags = [Flag] data Flag = Flag1 | Flag2 | Flag3 deriving (Eq, Ord, Enum, Show) flagToInt :: Flags -> Int flagToInt = sum . map ((2 ^) . fromEnum) Perhaps? -- Don

Donald Bruce Stewart wrote:
Perhaps?
Yes, Thank you. -- View this message in context: http://www.nabble.com/Flags-tf2083897.html#a5742181 Sent from the Haskell - Haskell-Cafe forum at Nabble.com.
participants (2)
-
dons@cse.unsw.edu.au
-
Maduser