
If I want to define a constant set in Python I would do this: class Colors: red = 1 blue=2 ... and then I use them like this: Colors.red, Colors.blue... Can I do something similar in Haskell? thanks

Ovidiu Deac
If I want to define a constant set in Python I would do this: class Colors: red = 1 blue=2 ...
and then I use them like this: Colors.red, Colors.blue...
Can I do something similar in Haskell?
In Haskell this would usually be an abstract type: data Color = Red | Blue | ... You could then use functions to convert between integers and colors. fromColor :: Color -> Int toColor :: Int -> Maybe Color You could also derive an Enum instance and get the conversion functions 'fromEnum' and 'toEnum' for free, although in that case the 'toEnum' function is less safe (no Maybe): data Color = Red | Blue | ... deriving Enum fromEnum :: Color -> Int toEnum :: Int -> Color Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/

Thanks for the answers. I guess my example with the colors was misleading. In that case it makes sense to use an abstract type. In my case an abstract type is not ok. What I want is a set of int constants i.e. some names for some ints that I will use in my code, to avoid magic numbers in the code. Of course I could define them like this: red = 1 blue = 2 ...but that would pollute the namespace Also I don't want to define them in a separate file. I would like to define them in the same file where I use them. I already tried this approach: module Main where import .... module Colors where red = 1 blue = 2 ..... but the compiler complains at the line "module Colors where". It says: parse error on input `module' In Haskell this would usually be an abstract type:
data Color = Red | Blue | ...
You could then use functions to convert between integers and colors.
fromColor :: Color -> Int toColor :: Int -> Maybe Color
You could also derive an Enum instance and get the conversion functions 'fromEnum' and 'toEnum' for free, although in that case the 'toEnum' function is less safe (no Maybe):
data Color = Red | Blue | ... deriving Enum
fromEnum :: Color -> Int toEnum :: Int -> Color
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

If you simply want color names to correspond to Ints, you can just define them at the top level:
red = 1 blue = 2 green = 3
What are you trying to gain here by having the color namespace?
2012/3/10 Ovidiu Deac
Thanks for the answers. I guess my example with the colors was misleading. In that case it makes sense to use an abstract type.
In my case an abstract type is not ok. What I want is a set of int constants i.e. some names for some ints that I will use in my code, to avoid magic numbers in the code.
Of course I could define them like this: red = 1 blue = 2
...but that would pollute the namespace
Also I don't want to define them in a separate file. I would like to define them in the same file where I use them.
I already tried this approach:
module Main where
import ....
module Colors where red = 1 blue = 2 .....
but the compiler complains at the line "module Colors where". It says: parse error on input `module'
In Haskell this would usually be an abstract type:
data Color = Red | Blue | ...
You could then use functions to convert between integers and colors.
fromColor :: Color -> Int toColor :: Int -> Maybe Color
You could also derive an Enum instance and get the conversion functions 'fromEnum' and 'toEnum' for free, although in that case the 'toEnum' function is less safe (no Maybe):
data Color = Red | Blue | ... deriving Enum
fromEnum :: Color -> Int toEnum :: Int -> Color
Greets, Ertugrul
-- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

What are you trying to gain here by having the color namespace?
I want to avoid polluting the global namespace and to avoid name collisions. As I said, the example with the colours wasn't probably a good one. Let me change the question: How can I create a sub-namespace in the same file?

A name space in Haskell is a module, and GHC only allows one module per
file.
So I would put these in a separate file, or do manual namespacing using
prefixes, as in "colorRed".
On Mar 10, 2012 11:14 AM, "Ovidiu Deac"
What are you trying to gain here by having the color namespace?
I want to avoid polluting the global namespace and to avoid name collisions.
As I said, the example with the colours wasn't probably a good one.
Let me change the question: How can I create a sub-namespace in the same file?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

OK. Thanks for the answers
On Sat, Mar 10, 2012 at 7:30 PM, Antoine Latter
A name space in Haskell is a module, and GHC only allows one module per file.
So I would put these in a separate file, or do manual namespacing using prefixes, as in "colorRed". On Mar 10, 2012 11:14 AM, "Ovidiu Deac"
wrote: What are you trying to gain here by having the color namespace?
I want to avoid polluting the global namespace and to avoid name collisions.
As I said, the example with the colours wasn't probably a good one.
Let me change the question: How can I create a sub-namespace in the same file?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

2012/3/10 Ovidiu Deac
I already tried this approach:
module Main where
import ....
module Colors where red = 1 blue = 2 .....
but the compiler complains at the line "module Colors where". It says: parse error on input `module'
Right, you can have only one module per file. You would define them in a separate file, possibly making use of qualified imports:
module Constants.AColors where red = 1 blue = 2
module Constants.BColors where yellow = 1 red = 2
module Main where import qualified Constants.AColors as AC import qualified Constants.BColors as BC ... -- code that uses AC.red and BC.red
This is not entirely ideal from the standpoint of using types to help manage your data, but without more details as to how you're using them, it's hard to present appropriate solutions. -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (5)
-
Antoine Latter
-
Brandon Allbery
-
Ertugrul Söylemez
-
Lyndon Maydwell
-
Ovidiu Deac