views:

120

answers:

2

I have the following expression:

getCount :: (Num a) => a -> [a]
getCount int = foldl 
       processOneCount 
       [0,0,0,0,0,0,0,0,0,0] 
       (map (singleDigitCount) (map (digitToInt) (show int)))

and i get the following error:

Couldn't match expected type `a' against inferred type `Int'
  `a' is a rigid type variable bound by
      the type signature for `getCount'
        at C:\Users\RCIX\Desktop\Haskell Code\test.hs:23:17
  Expected type: [a]
  Inferred type: [Int]
In the expression:
    foldl
      processOneCount
      [0, 0, 0, 0, ....]
      (map (singleDigitCount) (map (digitToInt) (show int)))
In the definition of `getCount':
    getCount int
               = foldl
                   processOneCount
                   [0, 0, 0, ....]
                   (map (singleDigitCount) (map (digitToInt) (show int)))

yet when i do a :t [0,0,0,0,0,0,0,0,0,0] i get back [0,0,0,0,0,0,0,0,0,0] :: (Num t) => [t]. So why can't i use it in the first expression?

+4  A: 

You're using digitToInt, which returns an Int, not the input type.

Chuck
Oh. Should i stick with an int type then or is there a way to coerce the output into a Num?
RCIX
`fromIntegral :: (Integral a, Num b) => a -> b`
ephemient
Thanks for the help guys :)
RCIX
A: 

Chuck is right. To avoid cluttering your code you can use the . operator to add the needed function:

  (map (singleDigitCount) (map (fromIntegral . digitToInt) (show int)))

This is presuming that singleDigitCount and processOneCount also work on arbitrary numeric types.

Dan