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_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to