
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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Andrew Wagner
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
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.

On 2008 Dec 29, at 23:27, Justin Goguen wrote:
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.
It will; toEnum and fromEnum are defined in terms of Int, not Integer, so they'll run into a maximum bounds issue. (See section 6.3.4 of http://haskell.org/onlinereport/basic.html) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Mon, Dec 29, 2008 at 11:27 PM, Justin Goguen
My purpose is to have operations such as ["aa".."bc"] be possible, with its result being ["aa", "ab", "ac" ..<snip>.. "ba", "bb", "bc"]
Are you planning to have this restricted to lower-case letters?
According to Char, one might expect "az" to be followed by "a{" and so
on up to "a\1114111". The number of possible 2-character strings is
larger than the number of Ints.
I suggest defining a special-purpose version from enumFromTo for
strings and leaving Enum alone.
--
Dave Menendez

Justin Goguen schrieb:
My purpose is to have operations such as ["aa".."bc"] be possible, with its result being ["aa", "ab", "ac" ..<snip>.. "ba", "bb", "bc"]
... what do you want to get, if the lengths of the start and the end string do not match? Maybe what you are after is the Ix class: Prelude> Data.Ix.range (('a','a'), ('b','c')) [('a','a'),('a','b'),('a','c'),('b','a'),('b','b'),('b','c')] Prelude> map (\(x,y) -> x:y:[]) $ Data.Ix.range (('a','a'), ('b','c')) ["aa","ab","ac","ba","bb","bc"]

Better would be [] = 0 ['a'] = 1 ['b'] = 2 ... ['z'] = 26 ['a','a'] = 27 ['a','b'] = 28 (asuming Char = ['a'..'z']) Am 30.12.2008 um 04:25 schrieb JustinGoguen:
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am 30.12.2008 um 04:25 schrieb JustinGoguen:
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.
The problem you're having is that your implementation does not correctly enumerate lists of characters – in order to do so correctly, you must not create the clashes you get with (for example "ab" and "ba"). I'd suggest rethinking how you would enumerate such strings. Bob
participants (8)
-
Adrian Neumann
-
Andrew Wagner
-
Brandon S. Allbery KF8NH
-
David Menendez
-
Henning Thielemann
-
Justin Goguen
-
JustinGoguen
-
Thomas Davie