
Am Donnerstag, 26. Februar 2009 12:26 schrieb Philip Scott:
Hi ho,
One more improvement, include also Enum among the derived classes, then you can write
months = cycle [Jan .. Dec]
Oh, and cycle is also exported from the Prelude, so it's not necessary to import Data.List for that (though you will probably want to imoprt it anyway).
Ohh so many little tasty titbits of Haskell - I was trying for ages to get the '..' operator to work. I guess I will start to know where to look for these guys as time goes on.
The '..' things are syntactic sugar for enumerations: [a .. ] === enumFrom a [a, b .. ] === enumFromThen a b [a .. b] === enumFromTo a b [a, b .. c] === enumFromThenTom a b c they are methods of class Enum. :i Enum in ghci or hugs gives the class information (methods and instances currently in scope). One place to look for such things is the Haskell report, also many tutorials and books have a sectioned 'Overview of standard type classes' or some such, there you should find this and similarly important things. Another method is to ask ghci or hugs - you have to do it the right way, which is not always obvious - as in Prelude> :t \a b -> [a .. b] \a b -> [a .. b] :: (Enum t) => t -> t -> [t] so you know you have to look at Enum Prelude> :i Enum class Enum a where succ :: a -> a pred :: a -> a toEnum :: Int -> a fromEnum :: a -> Int enumFrom :: a -> [a] enumFromThen :: a -> a -> [a] enumFromTo :: a -> a -> [a] enumFromThenTo :: a -> a -> a -> [a] -- Defined in GHC.Enum instance Enum Integer -- Defined in GHC.Num instance Enum Float -- Defined in GHC.Float instance Enum Double -- Defined in GHC.Float instance Enum Bool -- Defined in GHC.Enum instance Enum Ordering -- Defined in GHC.Enum instance Enum Char -- Defined in GHC.Enum instance Enum () -- Defined in GHC.Enum instance Enum Int -- Defined in GHC.Enum looking at this, you don't see '..', but you see two functions with the correct type, that it's more likely to be enumFromTo rather than enumFromThen can easily be inferred from the functions' names.
Cheers,
Philip
Cheers, Daniel