[Haskell-cafe] haskell quiry

Jules Bean jules at jellybean.co.uk
Thu May 17 07:00:00 EDT 2007


ashutosh dimri wrote:

> func [] = []
> func (x:xs)
>     |(ord x > 57) = ((ord x)-87):func xs
>     |otherwise  =  ((ord x)-48):func xs


Your problem is with the type of 'func'.

*Main> :t func
func :: [Char] -> [Int]


'Int' is a 32-bit type, on most systems. The thing that has forced your 
type to Int is your use of ord, because ord is defined to produce Ints:

*Main> :t ord
ord :: Char -> Int


We can use 'fromIntegral' to convert an Int to any other numeric type:

func [] = []
func (x:xs)
     |(ord x > 57) = ((fromIntegral.ord $ x)-87):func xs
     |otherwise  =  ((fromIntegral.ord $ x)-48):func xs


And now 'func' has the more general type:

*Main> :t func
func :: (Num a) => [Char] -> [a]

(i.e. func works on any numeric type 'a').

and now it works for you on very large values:

*Main> mul.func $ "eeeeeeeeee"
1026210852590


Hope that helps,

Jules


More information about the Haskell-Cafe mailing list