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

Justin Goguen adekoba at hamiltonshells.ca
Mon Dec 29 23:27:06 EST 2008


Andrew Wagner <wagner.andrew <at> gmail.com> writes:

> 
> 
> 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 listHaskell-Cafe <at>
haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 

My purpose is to have operations such as ["aa".."bc"] be possible, with its
result being ["aa", "ab", "ac" ..<snip>.. "ba", "bb", "bc"]

The example you provided for fromEnum seems to break down after a string length
of about 5 or so.

I'm just having trouble getting toEnum to decode the Int's into the proper
strings.



More information about the Haskell-Cafe mailing list