[Haskell-cafe] Rational and % operator remix

michael rice nowgate at yahoo.com
Mon Mar 30 20:30:50 EDT 2009


Thanks for the information.

Michael

--- On Mon, 3/30/09, Ryan Ingram <ryani.spam at gmail.com> wrote:

From: Ryan Ingram <ryani.spam at gmail.com>
Subject: Re: [Haskell-cafe] Rational and % operator remix
To: "michael rice" <nowgate at yahoo.com>
Cc: "Ketil Malde" <ketil at malde.org>, haskell-cafe at haskell.org
Date: Monday, March 30, 2009, 1:21 PM

2009/3/30 michael rice <nowgate at yahoo.com>:
> I'm still not sure what some of the error messages I was getting were about.
> As I wrote the function I tried to be aware of what "mixed mode" operations
> were kosher ala

This is a mistake, but understandable given your lispy background;
there aren't really "mixed mode" operations in Haskell.

One thing that might be confusing you: Numeric literals are really
calls to "fromInteger"; that is, "5" is really "fromInteger (5 ::
Integer)"

Generally you will find that :t in the interpreter is your friend:

ghci> :t 1
1 :: (Num t) => t

This says that the expression "1" can be of any type that is an instance of Num.

ghci> :t (\a b -> a - b)
(\a b -> a - b) :: (Num a) => a -> a -> a

"-" takes two arguments that are of the same type, and returns
something of that type.

ghci> :m Data.Ratio
ghci> :t (%)
(%) :: (Integral a) => a -> a -> Ratio a

"%" lifts objects from an integral type into a type that represents
the ratio of two integers.

ghci> :t floor
floor :: (RealFrac a, Integral b) :: a -> b

So "floor" is a "cast" operation that converts between any fractional
type to any integral type.

This is why you need to either use "fromInteger" or "%" on the result
of "floor" to get it back as a Rational.

  -- ryan



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090330/2c08ef55/attachment.htm


More information about the Haskell-Cafe mailing list