Re: [Haskell-cafe] type Rational and the % operator
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 Hugs> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : x % 1 + 1 % cf xs *** Term : x % 1 *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer) Data.Ratio> --- On Sat, 3/28/09, Ryan Ingram <ryani.spam@gmail.com> wrote: From: Ryan Ingram <ryani.spam@gmail.com> Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" <nowgate@yahoo.com> Cc: "Brandon S. Allbery KF8NH" <allbery@ece.cmu.edu>, haskell-cafe@haskell.org Date: Saturday, March 28, 2009, 11:17 PM Just use "/" for division. % is for construction of rationals from the "underlying" numeric type. For example, instead of "toRational x" you can write "x % 1". -- ryan 2009/3/28 michael rice <nowgate@yahoo.com>:
I may be missing something here, but this is what I intended.
An expression of the form
1 a1 + ------ 1 a2 + ------ 1 a3 + -- a4 + ...
Where the ai's are positive integers is called a continued fraction.
Function cf should take [1,2,6,5] to produce
1 1 + ----- 1 2 + ----- 1 6 + -- 5
Michael
--- On Sat, 3/28/09, Brandon S. Allbery KF8NH <allbery@ece.cmu.edu> wrote:
From: Brandon S. Allbery KF8NH <allbery@ece.cmu.edu> Subject: Re: [Haskell-cafe] type Rational and the % operator To: "michael rice" <nowgate@yahoo.com> Cc: "Brandon S. Allbery KF8NH" <allbery@ece.cmu.edu>, "Duane Johnson" <duane.johnson@gmail.com>, haskell-cafe@haskell.org Date: Saturday, March 28, 2009, 10:39 PM
On 2009 Mar 28, at 22:36, michael rice wrote:
import Data.Ratio cf :: [Integer] -> Rational cf (x:xs) = (toRational x) + (1 % (cf xs)) cf (x:[]) = toRational x cf [] = toRational 0
Data.Ratio> :load cf.hs ERROR "cf.hs":3 - Type error in application *** Expression : toRational x + 1 % cf xs *** Term : toRational x *** Type : Ratio Integer *** Does not match : Ratio (Ratio Integer)
Your function cf produces a Rational (Ratio Int); you're using it in the denominator of another Ratio, which makes that Ratio's type Ratio (Ratio Int). This is almost certainly not what you intended, but I couldn't say what you actually want. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
2009/3/28 michael rice <nowgate@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
participants (2)
-
Luke Palmer -
michael rice