
Hi ord :: Char -> Int ord c = sum [1 | x <- ['\0'..'\255'], x < c] Any comments? Any alternatives? Cheers, Paul

(sending the e-mail again, this time with CC to the list)
On 9/29/07, PR Stanley
Hi ord :: Char -> Int ord c = sum [1 | x <- ['\0'..'\255'], x < c]
Any comments? Any alternatives?
Well, you're using Enum class there, so maybe you want ord' = fromEnum Also, an alternative is to stop the loop as soon as possible ord'' c = length $ takeWhile (< c) ['\0'..'\255'] Humm... and it seems that you can have Char's above '\255', so it would be wiser to use ord''' c = length $ takeWhile (< c) ['\0'..] -- Felipe.

On Sat, Sep 29, 2007 at 03:11:20PM +0100, PR Stanley wrote:
Hi ord :: Char -> Int ord c = sum [1 | x <- ['\0'..'\255'], x < c]
Any comments? Any alternatives? Cheers, Paul
It's waay slow, it breaks for characters >255. ord :: Char -> Int ord = fromEnum As Char is an instance of 'Enum'. If you think this is cheating, then consider that (<) for Char is probably implemented using ord on some level. You need primitives somewhere... Stefan

Okay, a couple of things
what is fromEnum?
What's the role of % in length 5 takeWhile ( On Sat, Sep 29, 2007 at 03:11:20PM +0100, PR Stanley wrote: Hi
ord :: Char -> Int
ord c = sum [1 | x <- ['\0'..'\255'], x < c] Any comments? Any alternatives?
Cheers, Paul It's waay slow, it breaks for characters >255. ord :: Char -> Int
ord = fromEnum As Char is an instance of 'Enum'. If you think this is cheating, then consider that (<) for Char is
probably implemented using ord on some level. You need primitives
somewhere... Stefan

(sending to the whole list this time ...)
On 9/29/07, PR Stanley
Okay, a couple of things what is fromEnum?
fromEnum :: (Enum a) => a -> Int fromEnum 'c' :: Int 'fromEnum' converts any member of the "Enum" typeclass to an 'Int'. It works fine for a Char.
What's the role of % in length 5 takeWhile (
I assume you mean the '$': ($) :: (a -> b) -> a -> b It's the function application operator, but it has extremely low precedence, so it applies everything to it's left to everything to its right. It's mainly a tool for cutting down on writing paranthesis: f (1 + 1) = f $ 1 + 1
participants (4)
-
Antoine Latter
-
Felipe Almeida Lessa
-
PR Stanley
-
Stefan O'Rear