[Haskell-cafe] Function Returning Type?

Jochem Berndsen jochem at functor.nl
Thu May 21 15:15:33 EDT 2009


jrw4 wrote:
> I wrote this function as I am just learning Haskell.  What it does is it
> generates a list of all rational numbers between 0 and 1, but I only have it
> show the first 20.
> 
> rationals n :: Integer -> String
> rationals n = (putStr . unlines . map show) (take n (nub [x % y | y <-
> [1..], x <- [1..y], x < y]))
> 
> Now my problem is the type declaration, the first line.  I get the error
> "Invalid type signature".  I googled this error and I was not able to find
> out why it is giving it to me.  I have also tried:
> 
> rationals n :: Integer -> [Ratio]
> 
> but it gives me the same error.
> 
> Please someone point me in the right direction.  Thanks.

Just
rationals :: Integer -> String

suffices. (Without the argument 'n'.)

This makes sense, since "rationals" has type Integer. "rationals n" has
type String. (But you still cannot declare that in toplevel that way.)

Regards,

-- 
Jochem Berndsen | jochem at functor.nl
GPG: 0xE6FABFAB


More information about the Haskell-Cafe mailing list