
Hello, I have some trouble evaluating Enum class. Following is my source: data Authors= Buzzati | Werber | Verne | Ray | Asimov | Voltaire deriving (Enum, Show) auths=[Buzzati .. Verne] ++ enumFrom Ray pref=take 3 auths -- 1 disp_pref=mapM_ (putStrLn) pref auth1 = Buzzati auth2 = succ auth1 -- Werber auth3 = pred Asimov -- Ray num_auth4=fromEnum Verne -- 2 -- 2 toEnum main= display Buzzati display x = do putStrLn (show x) display (succ x) -- 3 end of enum 1. could'nt match expected type [Char] against inferred type Authors I would like to display each data constructor name on one line , giving : Buzzati Werber Verne How can i translate data from one type to another (Authors to [Char])? 2. Like fromEnum gives data constructor index, is it possible with toEnum (or other way) to get the nth constructor of the given type ? eg : 2 applied to Authors (not in scope ?) would give Verne 3. tried to take 'succ' of last tag in enumeration How to detect the end of an enumeration ? Can i get the maxbound index ? Thanks, Didier.

Hi,
On Sun, Dec 27, 2009 at 8:42 AM, legajid
Hello,
I have some trouble evaluating Enum class. Following is my source:
data Authors= Buzzati | Werber | Verne | Ray | Asimov | Voltaire deriving (Enum, Show)
auths=[Buzzati .. Verne] ++ enumFrom Ray pref=take 3 auths -- 1 disp_pref=mapM_ (putStrLn) pref
putStrLn wants a String as argument, here you are passing an Authors. Try this: disp_pref=mapM_ (putStrLn . show) pref
auth1 = Buzzati auth2 = succ auth1 -- Werber auth3 = pred Asimov -- Ray num_auth4=fromEnum Verne -- 2 -- 2 toEnum
You can use toEnum to get the nth constructor. For example: nthAuthor :: Int -> Authors nthAuthor i = toEnum i
main= display Buzzati display x = do putStrLn (show x) display (succ x) -- 3 end of enum
To use maxBound you need to make your type an instance of Bounded. You will also need to make it an instance of Eq in order to compare it. main= display Buzzati display x = do putStrLn (show x) if x == maxBound then return () else display (succ x) Patrick
1. could'nt match expected type [Char] against inferred type Authors I would like to display each data constructor name on one line , giving : Buzzati Werber Verne How can i translate data from one type to another (Authors to [Char])?
2. Like fromEnum gives data constructor index, is it possible with toEnum (or other way) to get the nth constructor of the given type ? eg : 2 applied to Authors (not in scope ?) would give Verne
3. tried to take 'succ' of last tag in enumeration How to detect the end of an enumeration ? Can i get the maxbound index ?
Thanks, Didier.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Thanks. for 1, i tried mapM ( putStrLn) (show pref) but show gives only one line with all values, instead of n lines with 1 value. I now understand that show must be in the function that is mapped. for 2, ok for 3, ok with new classes in deriving. I thought that i would have to write new instances of Bounded or Eq, but now i think it's unuseful since standard functions exist in these classes. Patrick LeBoutillier a écrit :
Hi,
On Sun, Dec 27, 2009 at 8:42 AM, legajid
wrote: Hello,
I have some trouble evaluating Enum class. Following is my source:
data Authors= Buzzati | Werber | Verne | Ray | Asimov | Voltaire deriving (Enum, Show)
auths=[Buzzati .. Verne] ++ enumFrom Ray pref=take 3 auths -- 1 disp_pref=mapM_ (putStrLn) pref
putStrLn wants a String as argument, here you are passing an Authors. Try this:
disp_pref=mapM_ (putStrLn . show) pref
auth1 = Buzzati auth2 = succ auth1 -- Werber auth3 = pred Asimov -- Ray num_auth4=fromEnum Verne -- 2 -- 2 toEnum
You can use toEnum to get the nth constructor. For example:
nthAuthor :: Int -> Authors nthAuthor i = toEnum i
main= display Buzzati display x = do putStrLn (show x) display (succ x) -- 3 end of enum
To use maxBound you need to make your type an instance of Bounded. You will also need to make it an instance of Eq in order to compare it.
main= display Buzzati display x = do putStrLn (show x) if x == maxBound then return () else display (succ x)
Patrick
1. could'nt match expected type [Char] against inferred type Authors I would like to display each data constructor name on one line , giving : Buzzati Werber Verne How can i translate data from one type to another (Authors to [Char])?
2. Like fromEnum gives data constructor index, is it possible with toEnum (or other way) to get the nth constructor of the given type ? eg : 2 applied to Authors (not in scope ?) would give Verne
3. tried to take 'succ' of last tag in enumeration How to detect the end of an enumeration ? Can i get the maxbound index ?
Thanks, Didier.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sun, Dec 27, 2009 at 06:17:34PM +0100, legajid wrote:
Thanks.
for 1, i tried mapM ( putStrLn) (show pref) but show gives only one line with all values, instead of n lines with 1 value. I now understand that show must be in the function that is mapped.
Right, pref is a list so (show pref) will just give you a String representing the entire list. You can do (map show pref) to give you a list of Strings, one for each value. So you could write mapM_ putStrLn (map show pref) which is actually equivalent to mapM_ (putStrLn . show) pref -Brent

On Sun, Dec 27, 2009 at 02:42:48PM +0100, legajid wrote:
Hello,
I have some trouble evaluating Enum class. Following is my source:
data Authors= Buzzati | Werber | Verne | Ray | Asimov | Voltaire deriving (Enum, Show)
auths=[Buzzati .. Verne] ++ enumFrom Ray pref=take 3 auths -- 1 disp_pref=mapM_ (putStrLn) pref
This is a type error since putStrLn expects a String and pref is a list of Authors. But the 'deriving Show' means that you can use the 'show' function to convert: disp_pref = mapM_ (putStrLn . show) pref In fact, since print = putStrLn . show, you can just say disp_pref = mapM_ print pref
2. Like fromEnum gives data constructor index, is it possible with toEnum (or other way) to get the nth constructor of the given type ? eg : 2 applied to Authors (not in scope ?) would give Verne
Yes, with toEnum. Just use e.g. 'toEnum 2'. How does it know that you want an Author and not some other Enum type? Why, through the magic of type inference! As long as you use 'toEnum 2' in a context where an Author value is expected it will evaluate to Verne.
3. tried to take 'succ' of last tag in enumeration How to detect the end of an enumeration ? Can i get the maxbound index ?
Yes, if you add 'Bounded' to the list of derived classes, then 'maxBound' gives you the last one. -Brent

To add to what the other guys have said, I'd like to comment on style..
re 1 (disp_pref), it's much more compositional to split that function into
two parts:
the pure part: showPrefs = unlines . map show $ pref
and the non pure part: putStrLn
The reason that this is better is that a future function can manipulate the
String that showPrefs produces – it can append, it can drop characters from
it, it can reverse it, etc... This is not true of what is left over after
the action of printing the string has happened.
It lets you reuse that function *much* more.
Bob
On Sun, Dec 27, 2009 at 1:42 PM, legajid
Hello,
I have some trouble evaluating Enum class. Following is my source:
data Authors= Buzzati | Werber | Verne | Ray | Asimov | Voltaire deriving (Enum, Show)
auths=[Buzzati .. Verne] ++ enumFrom Ray pref=take 3 auths -- 1 disp_pref=mapM_ (putStrLn) pref
auth1 = Buzzati auth2 = succ auth1 -- Werber auth3 = pred Asimov -- Ray num_auth4=fromEnum Verne -- 2 -- 2 toEnum
main= display Buzzati display x = do putStrLn (show x) display (succ x) -- 3 end of enum
1. could'nt match expected type [Char] against inferred type Authors I would like to display each data constructor name on one line , giving : Buzzati Werber Verne How can i translate data from one type to another (Authors to [Char])?
2. Like fromEnum gives data constructor index, is it possible with toEnum (or other way) to get the nth constructor of the given type ? eg : 2 applied to Authors (not in scope ?) would give Verne
3. tried to take 'succ' of last tag in enumeration How to detect the end of an enumeration ? Can i get the maxbound index ?
Thanks, Didier.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Brent Yorgey
-
legajid
-
Patrick LeBoutillier
-
Tom Davie