[Haskell-cafe] Trouble with numbers

Lloyd Smith lloyd.g.smith at gmail.com
Fri Feb 29 03:09:57 EST 2008

I have a question about how numeric classes and type checking works. I have two
functions that I think should behave the same but don't.

-- I want to split n things up using a given list of fractions
-- for example >allocate' 100 [1/3,1/3,1/3]
-- [33,33,33]
allocate' n fs = vs
    where vs = map (floor . (*n)) fs

-- I want to find anything left over eventually I will want to
-- return what is unallocated as well but for now allocated
-- and unallocated are not used!
allocate n fs = vs
    where vs = map (floor . (*n)) fs
          allocated = sum vs
          unallocated = n - allocated

When I load these function in the top level everything looks good

[1 of 1] Compiling Main             ( allocate.hs, interpreted )
Ok, modules loaded: Main.main
*Main> allocate' 100 [1/3,1/3,1/3]
*Main> allocate 100 [1/3,1/3,1/3]

    Ambiguous type variable `t' in the constraints:
      `Integral t'
        arising from a use of `allocate' at <interactive>:1:0-25
      `RealFrac t'
        arising from a use of `allocate' at <interactive>:1:0-25
    Probable fix: add a type signature that fixes these type variable(s)

I mixed up my types when finding the allocated and unallocated,
but I am not sure why it produces an error when unallocated and
allocated are never used? Shouldn't the two functions be compiled
down to the same thing?

Suggestions on how to do this more elegantly as well as pointers for
understanding numeric
type classes would be appreciated.


More information about the Haskell-Cafe mailing list