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

Pages: 1-

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 15:29 ID:Heaven

fuck, s/|code]/[code]/

Name: Anonymous 2007-04-12 15:35 ID:Heaven

Use integer division. Also, stop trying to be so fucking clever.

Name: Anonymous 2007-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: Anonymous 2007-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? :/

Name: Anonymous 2007-04-12 16:33 ID:Heaven

>>5
But I'd rather have integers in the resulting tuple... suggestions?

>> 3
Use integer division.

Has the sage man been trolled, or are you really this stupid?

Name: Anonymous 2007-04-12 16:48 ID:mbyhwCjv

>>6
I'm trying to find info about the specifics of this, but I'm not having any luck so far. Please enlighten me.

Name: Anonymous 2007-04-12 17:02 ID:Heaven

>>7
Go to Hoogle, search for Int -> Int -> Int, take a look at the results.

Name: Anonymous 2007-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.

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?

Name: Anonymous 2007-04-12 17:21 ID:Heaven

>>10
OH FUCKING PLEASE.

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

Name: Anonymous 2007-04-12 17:43 ID:vg7TDEA+

Someone code a one line haskell version.

Name: Anonymous 2007-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] ])

Name: Anonymous 2007-04-12 17:45 ID:Heaven

secondsToUnit seconds n = seconds `divMod` multiplier where multiplier = foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ]

Line numbers are overrated anyway.

Name: Anonymous 2007-04-12 17:46 ID:Heaven

>>14
Oops. That was pretty stupid.

Name: Anonymous 2007-04-12 18:18 ID:2Vn3WprZ

[tt]foldl1 (*) [ [1,60,60,24] !! x | x<-[0..n] ][/tt] ≡ [tt]product (take (n + 1) [1,60,60,24])[/tt]

Name: Anonymous 2007-04-12 18:37 ID:Heaven

But seriously. Don't create vague functions like secondsToUnit that convert to some `unit' specified using integers in range [0..3].

Name: Anonymous 2007-04-12 19:25 ID:sINAzFag

secondsToUnit = (. (foldl1 (*) . return . (([1, 60, 60, 24] !! x | x) <-) . enumFromTo 0)) . divMod

Name: Anonymous 2007-04-12 19:40 ID:Heaven

>>18
That seems rather pointless.

Name: Anonymous 2007-04-12 19:48 ID:pEx797Zd

>>19

It is Haskhell after all!

Name: Anonymous 2009-01-14 13:55

I just ordered another copy of SICP

Name: Anonymous 2009-03-06 5:45

The server and dish   the pages out?

Name: Anonymous 2009-03-06 7:50

The cost of reserving   all names starting   with four underscores?

Name: Anonymous 2010-12-17 1:41

This post brought to you by the Gay Nigger Association of America

Name: Sgt.Kabu勢�kiman姷㾫 2012-05-29 0:18

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

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