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?
Use integer division. Also, stop trying to be so fucking clever.
Name:
Anonymous2007-04-12 15:36 ID:dGWuDF8G
>>1
The compiler complains because you use /, which is defined in both the RealFrac and Integral classes -- you'll have to help the type inference engine a little here by explicitly specifying types of something... the function itself is a good start.
Or, that's my guess, at least ;)
Name:
Anonymous2007-04-12 16:25 ID:rhEx6Bci
>>4
I sort of got it working - if I drop the floor function, the signature secondsToUnit :: (Fractional a) => a -> Int -> (a, a) seems to work. But I'd rather have integers in the resulting tuple... suggestions? :/
>>7
Go to Hoogle, search for Int -> Int -> Int, take a look at the results.
Name:
Anonymous2007-04-12 17:11 ID:HaxiKfE1
>>8
As a related note, has anyone ever noticed how gay Hoogle is? The logo is colored in bright, happy gay pride colors and features a lowercase lambda, a well-known homosexual symbol.
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)
secondsToUnit :: Int -> Int -> (Int, Int)
secondsToUnit seconds n =
let multiplier = foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ]
in seconds `divMod` multiplier
Name:
Anonymous2007-04-12 17:43 ID:vg7TDEA+
Someone code a one line haskell version.
Name:
Anonymous2007-04-12 17:44 ID:W7bWjdi7
>>12 secondsToUnit :: Int -> Int -> (Int, Int)
secondsToUnit seconds n = seconds `divMod` (foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ])
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy