[Haskell-cafe] instance Enum [Char] where ...

Andrew Wagner wagner.andrew at gmail.com
Mon Dec 29 23:02:01 EST 2008

Err, is this just for academic purposes, or is there some deeper reason you
want an Enum instance of String? That will dictate how to define the
functions. For example, you could just as easily imagine other definitions
of fromEnum, such as:

fromEnum = read . concatMap (show . ord)

Why? *shrug*...the types match. So...you need to figure out why you're doing
what you're doing before you can really figure out what you're doing.

Hope this helps!

On Mon, Dec 29, 2008 at 10:25 PM, JustinGoguen <adekoba at hamiltonshells.ca>wrote:

> I am having difficulty making [Char] an instance of Enum. fromEnum is easy
> enough: map fromEnum to each char in the string and take the sum. However,
> toEnum has no way of knowing what the original string was.
> For example, running fromEnum on the string "d" will result in 100. But
> when we
> pass 100 to toEnum, it does not know if it should treat 100 as "d" or "22"
> (fromEnum '2' == 50).
> Source so far:
> instance Enum [Char] where
>    succ = undefined
>    pred = undefined
>    toEnum n = undefined -- what to do?
>    fromEnum xs = sum $ map fromEnum xs
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20081229/bb232463/attachment.htm

More information about the Haskell-Cafe mailing list