[Haskell-cafe] type Rational and the % operator

michael rice nowgate at yahoo.com
Sat Mar 28 23:03:20 EDT 2009


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 at ece.cmu.edu> wrote:

From: Brandon S. Allbery KF8NH <allbery at ece.cmu.edu>
Subject: Re: [Haskell-cafe] type Rational and the % operator
To: "michael rice" <nowgate at yahoo.com>
Cc: "Brandon S. Allbery KF8NH" <allbery at ece.cmu.edu>, "Duane Johnson" <duane.johnson at gmail.com>, haskell-cafe at 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 at kf8nh.comsystem administrator [openafs,heimdal,too many hats] allbery at ece.cmu.eduelectrical and computer engineering, carnegie mellon university    KF8NH
 



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


More information about the Haskell-Cafe mailing list