[Haskell-cafe] type Rational and the % operator

michael rice nowgate at yahoo.com
Sat Mar 28 23:48:57 EDT 2009


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 at gmail.com> wrote:

From: Ryan Ingram <ryani.spam at gmail.com>
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>, haskell-cafe at 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 at 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 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.com
> system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
> electrical and computer engineering, carnegie mellon university    KF8NH
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



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


More information about the Haskell-Cafe mailing list