
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. -- View this message in context: http://www.nabble.com/Function-Returning-Type--tp23658819p23658819.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

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@functor.nl GPG: 0xE6FABFAB

Jochem,
rationals n = (putStr . unlines . map show) (take n (nub [x % y | y <- [1..], x <- [1..y], x < y]))
rationals n :: Integer -> [Ratio]
I meant "Integer -> String" obviously.
Er... what about rationals :: Int -> IO () ? ;-) To the original poster: next time, just leave the function definition without the signature and query GHCi for the correct type: Prelude> :m + List Prelude List> :m + Ratio Prelude List Ratio> let rationals n = (putStr . unlines . map show) \ (take n (nub [x % y | y <- [1..], x <- [1..y], x < y])) Prelude List Ratio> :t rationals rationals :: Int -> IO () Cheers, Stefan

Op 21 May 2009, om 21:52 heeft Stefan Holdermans het volgende geschreven:
Jochem,
rationals n = (putStr . unlines . map show) (take n (nub [x % y | y <- [1..], x <- [1..y], x < y]))
rationals n :: Integer -> [Ratio]
I meant "Integer -> String" obviously.
Er... what about
rationals :: Int -> IO ()
Of note here though, the original type Integer -> String is more desirable – the Int/Integer part doesn't matter so much, what matters is the IO type – we can no longer pass the value that rationals generates into another function, because it printed it and dumped it. Lets fix the function instead: rationals :: Int -> [Rational] rationals n = take n $ nub [x % y | y <- [1..], x <- 1..], x < y] showLines :: Show a => [a] -> String showLines = unlines . map show rationalsS :: Int -> String rationalsS = showLines . rationals Now we can chose to use the actual rationals we got out of the function, we can chose to use the string we get back, we can reuse our function for showing multiple showable values, and we can print any of these if we chose :). Bob
participants (5)
-
Jochem Berndsen
-
jrw4
-
Mark Wassell
-
Stefan Holdermans
-
Thomas Davie