[Haskell-cafe] type Rational and the % operator

Luke Palmer lrpalmer at gmail.com
Sun Mar 29 00:23:53 EDT 2009


2009/3/28 michael rice <nowgate at yahoo.com>

> Still the same problem. The book says the answer is supposed to be an exact
> 98/67.
>
> Michael
>
> import Data.Ratio
> cf :: [Integer] -> Rational
> cf (x:xs) = (x % 1) + (1 % (cf xs))
> cf (x:[]) = x % 1
> cf [] = 0 % 1


Use (/) for division:

cf :: [Integer] -> Rational
cf (x:xs) = toRational x + 1 / cf xs
cf [x] = toRational x
cf [] = 0

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090328/ff7adb8b/attachment.htm


More information about the Haskell-Cafe mailing list