[Haskell-cafe] type Rational and the % operator

Ryan Ingram ryani.spam at gmail.com
Sat Mar 28 23:17:53 EDT 2009


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
>
>


More information about the Haskell-Cafe mailing list