This seems to work:

import Data.Ratio
cf :: [Integer] -> Rational
cf (x:[]) = toRational x
cf (x:xs) = toRational x + (1 / cf xs)

Hugs> :load cf.hs
Main> cf [1,2,6,5]
98 % 67
Main> cf [2,3]
7 % 3
Main> 

Thanks, guys!

Michael


--- On Sun, 3/29/09, Luke Palmer <lrpal...@gmail.com> wrote:

From: Luke Palmer <lrpal...@gmail.com>
Subject: Re: [Haskell-cafe] type Rational and the % operator
To: "michael rice" <nowg...@yahoo.com>
Cc: "Ryan Ingram" <ryani.s...@gmail.com>, haskell-cafe@haskell.org
Date: Sunday, March 29, 2009, 12:23 AM

2009/3/28 michael rice <nowg...@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] -> Rationalcf (x:xs) = toRational x + 1 / cf xscf [x] = 
toRational x
cf [] = 0
Luke



      
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to