[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
Karthick Gururaj
karthick.gururaj at gmail.com
Thu Mar 3 07:59:44 CET 2011
On Thu, Mar 3, 2011 at 11:48 AM, Chris Smith <cdsmith at gmail.com> wrote:
> 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
> ().
Thanks - is this the same "unit" that accompanies IO in "IO ()" ? In
any case, my question is answered since it is not a tuple.
>
>> But, surprise:
>> Prelude> (1,1) > (1,2)
>> False
>> Prelude> (2,2) > (1,1)
>> True
>> Prelude> (1,2) > (2,1)
>> False
>> Prelude> (1,2) < (2,1)
>> True
>
> 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
>> 2.0
>> Prelude> let y = (/20.0)
>> Prelude> y 10
>> 0.5
>>
>> 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
> 0.5
> Prelude> let y = (20.0 /)
> Prelude> y 10
> 2.0
>
> Hope that helps,
Thanks, it does!
More information about the Haskell-Cafe
mailing list