P. 1


|Views: 39|Likes:
Publicado porRiccardo Ricci
Haskell 98 Tutorial
Haskell 98 Tutorial

More info:

Published by: Riccardo Ricci on Oct 02, 2011
Direitos Autorais:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






Of the standard numeric types, Int, Integer, Float, and Double are primitive. The others are
made from these by type constructors.

Complex (found in the library Complex) is a type constructor that makes a complex type in
class Floating from a RealFloat type:

data (RealFloat a) =) Complex a = !a :+ !a deriving (Eq, Text)

The ! symbols are strictness flags; these were discussed in Section 6.3. Notice the context
RealFloat a, which restricts the argument type; thus, the standard complex types are Complex Float
and Complex Double. We can also see from the data declaration that a complex number is written
x :+ y; the arguments are the cartesian real and imaginary parts, respectively. Since :+is a data
constructor, we can use it in pattern matching:

conjugate (x:+y)

(RealFloat a) =) Complex a -) Complex a
x :+ (-y)

Similarly, the type constructor Ratio (found in the Rational library) makes a rational type in
class RealFrac from an instance of Integral. (Rational is a type synonym for Ratio Integer.)
Ratio, however, is an abstract type constructor. Instead of a data constructor like :+, rationals
use the '%' function to form a ratio from two integers. Instead of pattern matching, component
extraction functions are provided:


numerator, denominator

(Integral a) =) a -) a -) Ratio a
(Integral a) =) Ratio a -) a

Why the difference? Complex numbers in cartesian form are unique-there are no nontrivial
identities involving :+. On the other hand, ratios are not unique, but have a canonical (reduced)
form that the implementation of the abstract data type must maintain; it is not necessarily the
case, for instance, that numerator (x%y) is equal to x, although the real part of x: +y is always x.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->