[Haskell-cafe] Rewriting Char.ord

Felipe Almeida Lessa felipe.lessa at gmail.com
Sat Sep 29 10:23:55 EDT 2007


(sending the e-mail again, this time with CC to the list)

On 9/29/07, PR Stanley <prstanley at ntlworld.com> wrote:
> 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.


More information about the Haskell-Cafe mailing list