[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
karthick.gururaj at gmail.com
Thu Mar 3 07:09:41 CET 2011
I'm learning Haskell from the extremely well written (and well
illustrated as well!) tutorial - http://learnyouahaskell.com/chapters.
I have couple of questions from my readings so far.
In "typeclasses - 101"
there is a paragraph that reads:
Enum members are sequentially ordered types - they can be enumerated.
The main advantage of the Enum typeclass is that we can use its types
in list ranges. They also have defined successors and predecesors,
which you can get with the succ and pred functions. Types in this
class: (), Bool, Char, Ordering, Int, Integer, Float and Double.
What is the "()" type? Does it refer to a tuple? How can tuple be
ordered, let alone be enum'd? I tried:
Prelude> take 10 [(1,1) ..]
No instance for (Enum (t, t1))
arising from the arithmetic sequence `(1, 1) .. '
Possible fix: add an instance declaration for (Enum (t, t1))
In the second argument of `take', namely `[(1, 1) .. ]'
In the expression: take 10 [(1, 1) .. ]
In the definition of `it': it = take 10 [(1, 1) .. ]
This is expected and is logical.
Prelude> (1,1) > (1,2)
Prelude> (2,2) > (1,1)
Prelude> (1,2) > (2,1)
Prelude> (1,2) < (2,1)
So tuples are in "Ord" type class atleast. What is the ordering logic?
Another question, on the curried functions - specifically for infix
functions. Suppose I need a function that takes an argument and adds
five to it. I can do:
Prelude> let addFive = (+) 5
Prelude> addFive 4
The paragraph: "Infix functions can also be partially applied by using
sections. To section an infix function, simply surround it with
parentheses and only supply a parameter on one side. That creates a
function that takes one parameter and then applies it to the side
that's missing an operand": describes a different syntax. I tried that
Prelude> let addFive' = (+5)
Prelude> addFive' 3
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?
More information about the Haskell-Cafe