[Haskell-beginners] Enumerated types
tom.davie at gmail.com
Sun Dec 27 18:13:48 EST 2009
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
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.
On Sun, Dec 27, 2009 at 1:42 PM, legajid <legajid at free.fr> wrote:
> 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 :
> 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 ?
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners