Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Type signature faggotry

Name: Anonymous 2007-04-12 15:27 ID:yhED11h4

I have this function:

secondsToUnit seconds n = let multiplier = foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ]
                              units = floor $ seconds / multiplier
                              rest = seconds - units * multiplier
                          in
                          (units, rest)


When I try to use it somewhere, GHC complains:

|code]    Ambiguous type variable `a' in the constraints:
      `RealFrac a' arising from use of `secondsToUnit'
      `Integral a' arising from use of `secondsToUnit'
    Probable fix: add a type signature that fixes these type variable(s)[/code]

The problem is, I'm still a newbie and I have no idea what kind of type signature I should use, since I don't completely understand Haskell's numeric types yet. Any pointers? Can it even be fixed just by adding a proper type signature or is there something else I need to alter?

Name: Anonymous 2007-04-12 17:15 ID:mbyhwCjv

>>8
Funny. Anyway, I tried:

secondsToUnit :: Int -> Int -> (Int, Int)
secondsToUnit seconds n = let multiplier = foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ]
                              units = seconds / multiplier
                              rest = seconds - units * multiplier
                          in
                          (units, rest)|/code]

and GHC gives me:

[code]    No instance for (Fractional Int)
      arising from use of `/' at ./TimeData.hs:11:46
    Probable fix: add an instance declaration for (Fractional Int)
    In the definition of `units': units = seconds / multiplier
    In the definition of `secondsToUnit':
        secondsToUnit seconds n
                        = let
                            multiplier = foldl1 (*) ([... | x <- ...])
                            units = seconds / multiplier
                            rest = seconds - (units * multiplier)
                          in (units, rest)


What am I missing?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List