[Haskell-beginners] ghc installation on mac osx 10.4.11(intel), Xcode 2.4?

Daniel Fischer daniel.is.fischer at web.de
Tue Feb 24 05:25:53 EST 2009

Am Dienstag, 24. Februar 2009 08:21 schrieb 7stud:
> Hi,
> Also, a question about "Beware of enumerating floating
> point numbers" on p. 11.  The book doesn't explain
> what it means to enumerate floating point numbers
> without a step, so when it says to beware of
> the non-intuitive behavior of:
> ghci> [1.0..1.8]
> how would a reader know what the intuitive behavior is?
> Is the step .1, .01, .001?  Is there a default
> step?  Who knows?

Experienced Haskellers know :-)
The default step is 1, and

Prelude> [1.0 .. 1.8]

, which is what is meant by "the non-intuitive behaviour".
Of course, a learner won't know, so a request to the authors: elaborate that 
in the second edition.

The fact behind it is that [a .. b] is syntactic sugar for

enumFromTo a b

, enumFromTo is a method in the class Enum. As the name suggests, this class 
is for types where you can enumerate the elements, every element - except 
boundary elements - should have a well defined predecessor and successor.
So, enumFromTo a b is in these cases the sequence of elements c with 
index a <= index c <= index b, the indices increasing by 1.
This doesn't make much sense for floating point types or Rational, of course.
However, arithmetic sequences do make a lot of sense for these types, so they 
are made instances of the class Enum, too. Since their elements don't have a 
natural indexing, these instances are by necessity somewhat broken.
The enumFrom and enumFromTo methods proceed in steps of 1, which in absence of 
a reasonable successor function is probably the best choice. However, 
enumFromTo a b doesn't stop when the value exceeds b, but when it exceeds 
The behaviour is defined in section 6.3.4 of the Haskell report.

> On p. 10 there is an error.  Below the last code example
>  on the page, it says:
> "In the latter case, the list is quite sensibly missing the
> endpoint of the enumeration, because it isn't an
> element of the series we defined."
> But the last line of the code example is this:
> ghci> [10,9..1]
> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
> which quite clearly includes both endpoints.

They probably meant to write

[10,8 .. 1].

More information about the Beginners mailing list