[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
cdsmith at gmail.com
Thu Mar 3 07:18:42 CET 2011
On Thu, 2011-03-03 at 11:39 +0530, Karthick Gururaj wrote:
> What is the "()" type? Does it refer to a tuple? How can tuple be
> ordered, let alone be enum'd? I tried:
The () type is pronounced "unit". It is a type with only 1 value, also
called () and pronounced "unit". Since it only has one possible value,
it conveys no information at all, and is sometimes used in situations
analogous to C's 'void' keyword.
Okay, actually that was a little bit of a lie; () has two "values": ()
and bottom. Bottom is the "value" that corresponds to the program
hanging in an infinite loop or dying with an error message. But if you
have an actual, honest-to-goodness value that's not bottom, it has to be
> But, surprise:
> Prelude> (1,1) > (1,2)
> Prelude> (2,2) > (1,1)
> Prelude> (1,2) > (2,1)
> Prelude> (1,2) < (2,1)
Okay, so this is no longer Enum, but just Ord. The ordering defined in
the Ord instance for tuples is the normal lexicographic order: the
comparison between the first elements dominates; but if the first
elements coincide, then the second are compared instead. For larger
tuple types, the same pattern continues.
Think of it like organizing words in alphabetical order, where here you
know the words all have the same number of letters.
> Ok. Works. But on a non-commutative operation like division, we get:
> Prelude> let x = (/) 20.0
> Prelude> x 10
> Prelude> let y = (/20.0)
> Prelude> y 10
> So a curried infix operator fixes the first argument and a "sectioned
> infix" operator fixes the second argument?
Sections can be either left sections or right sections, so you can pick
which argument is provided.
Prelude> let y = (/ 20.0)
Prelude> y 10
Prelude> let y = (20.0 /)
Prelude> y 10
Hope that helps,
More information about the Haskell-Cafe