Re: [Haskell-cafe] Rational and % operator remix

I'm a Lisper, kind of feeling my way around here in Haskell, so please bear with me.
I did the things you suggested, but I think the last one may have gotten garbled. Anyway, this is what I ended up with
cf2 :: Rational -> [Int]
cf2 a = let ai = floor a
in
if a == (toRational ai)
then [ai]
else ai : cf2 (1 / (a - ai))
but I'm still at least one error short of a clean run
Main> :load cf.hs
ERROR "cf.hs":7 - Type error in application
*** Expression : ai : cf2 (1 / (a - ai))
*** Term : ai
*** Type : Ratio Integer
*** Does not match : Int
Where did I go wrong or what did I leave out?
Michael
--- On Sun, 3/29/09, Lennart Augustsson
Hi,
Thanks again for the help last night.
The second function cf2 is an attempt to reverse the process of the first function, i.e., given a rational number it returns a list of integers, possibly infinite, but you shouldn't get into trouble if you use 98%67 as input (output should be [1,2,6,5]). The interpreter is complaining about the '=' following the 'in' keyword. Is there a better way to state this?
Michael
import Data.Ratio cf :: [Int] -> Rational cf (x:[]) = toRational x cf (x:xs) = toRational x + 1 / cf xs
cf2 :: Rational -> [Int] cf2 a = let ai = toRational (floor ((numerator a) / (denominator a))) in if a = ai then [a] else ai : cf2 ((toRational 1) / (subtract ai a))
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

cf2 a = let ai = floor a air = toRational ai in ai : if a == air then [] else cf2 (1 / (a - air)) On 30 Mar 2009, at 00:19, michael rice wrote:
I'm a Lisper, kind of feeling my way around here in Haskell, so please bear with me.
I did the things you suggested, but I think the last one may have gotten garbled. Anyway, this is what I ended up with
cf2 :: Rational -> [Int] cf2 a = let ai = floor a in if a == (toRational ai) then [ai] else ai : cf2 (1 / (a - ai))
but I'm still at least one error short of a clean run
Main> :load cf.hs ERROR "cf.hs":7 - Type error in application *** Expression : ai : cf2 (1 / (a - ai)) *** Term : ai *** Type : Ratio Integer *** Does not match : Int
Where did I go wrong or what did I leave out?
Michael
--- On Sun, 3/29/09, Lennart Augustsson
wrote: From: Lennart Augustsson
Subject: Re: [Haskell-cafe] Rational and % operator remix To: "michael rice" Cc: haskell-cafe@haskell.org Date: Sunday, March 29, 2009, 2:29 PM You can use floor in a Rational directly, no need to take it apart and divide.
There is no need to write (toRational 1), just write 1.
Don't write (subtract ai a), write (ai - i).
You also have a type error; the ai should no be a Rational, so you need to move to toRational call to the comparison.
-- Lennart
Hi,
Thanks again for the help last night.
The second function cf2 is an attempt to reverse the process of
2009/3/29 michael rice
: the first function, i.e., given a rational number it returns a list of integers, possibly infinite, but you shouldn't get into trouble if you use 98%67 as input (output should be [1,2,6,5]). The interpreter is complaining about the '=' following the 'in' keyword. Is there a better way to state this?
Michael
import Data.Ratio cf :: [Int] -> Rational cf (x:[]) = toRational x cf (x:xs) = toRational x + 1 / cf xs
cf2 :: Rational -> [Int] cf2 a = let ai = toRational (floor ((numerator a) / (denominator a))) in if a = ai then [a] else ai : cf2 ((toRational 1) / (subtract ai a))
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 2009 Mar 29, at 16:19, michael rice wrote:
but I'm still at least one error short of a clean run
Main> :load cf.hs ERROR "cf.hs":7 - Type error in application *** Expression : ai : cf2 (1 / (a - ai)) *** Term : ai *** Type : Ratio Integer *** Does not match : Int
Where did I go wrong or what did I leave out?
cf2 returns [Int], but you're using it as the tail of a list starting with ai, which is a Rational. Need to decide which you actually want. At a guess:
cf2 a = let ai = floor a rai = toRational ai in if a == rai then [ai] else rai : cf2 (1 / (a - ai))
-- 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

On 2009 Mar 29, at 16:27, Brandon S. Allbery KF8NH wrote:
cf2 a = let ai = floor a rai = toRational ai in if a == rai then [ai] else rai : cf2 (1 / (a - ai))
Nope, went a bit too fast there, ignore me -- 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
participants (3)
-
Brandon S. Allbery KF8NH
-
michael rice
-
Miguel Mitrofanov