[Haskell-cafe] Enum instantiation
I'd like to make Day an instance of class Enum, but the definition of toEnum below seems to be completely wrong, because integers seem not permit pattern matching. How is toEnum defined? Thanks. data Day = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Eq, Ord, Show) instance Enum Day wherefromEnum Sunday= 0fromEnum Monday = 1fromEnum Tuesday = 2fromEnum Wednesday = 3fromEnum Thursday = 4fromEnum Friday= 5fromEnum Saturday = 6toEnum 0 = SundaytoEnum 1 = Monday toEnum 2 = TuesdaytoEnum 3 = Wednesday toEnum 4 = ThursdaytoEnum 5 = Friday toEnum 6 = Saturday _ Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Enum instantiation
2010/5/21 R J rj248...@hotmail.com: I'd like to make Day an instance of class Enum, but the definition of toEnum below seems to be completely wrong, because integers seem not permit pattern matching. How is toEnum defined? Thanks. Hi, What error are you getting when you try your class instance? Thanks, Antoine ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Enum instantiation
R J rj248...@hotmail.com writes: I'd like to make Day an instance of class Enum, but the definition of toEnum below seems to be completely wrong, because integers seem not permit pattern matching. How is toEnum defined? Thanks. You could try using guards: toEnum x | x == 0= Sunday | x == 1= Monday | x == 2= Tuesday | x == 3= Wednesday | x == 4= Thursday | x == 5= Friday | x == 6= Saturday | otherwise = error bad enum BTW if none of your constructors have fields you can just add Enum to the deriving (...) list for your type, and the compiler will write basically the same instance for you. G. -- Gregory Collins g...@gregorycollins.net ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Enum instantiation
On Fri, May 21, 2010 at 8:59 PM, R J rj248...@hotmail.com wrote: If I type toEnum 5, the error I get is: interactive:1:0: Ambiguous type variable `a' in the constraint: `Enum a' arising from a use of `toEnum' at interactive:1:0-7 Probable fix: add a type signature that fixes these type variable(s) *Main Here the problem is not in your instance of Enum, but that GHCi doesn't know which instance of enum to use. In most code this isn't a problem, because the surrounding context often gives enough information for the type-checker to work with. But when there isn't enough context, you'll need to provide a type signature. Does toEnum 5 :: Day work? Antoine ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe