[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
Paul Sujkov
psujkov at gmail.com
Thu Mar 3 15:30:40 CET 2011
Hi,
you can always check the types using GHCi prompt:
*Prelude> :i (,)
data (,) a b = (,) a b -- Defined in GHC.Tuple
instance (Bounded a, Bounded b) => Bounded (a, b)
-- Defined in GHC.Enum
instance (Eq a, Eq b) => Eq (a, b) -- Defined in Data.Tuple
instance Functor ((,) a) -- Defined in Control.Monad.Instances
instance (Ord a, Ord b) => Ord (a, b) -- Defined in Data.Tuple
instance (Read a, Read b) => Read (a, b) -- Defined in GHC.Read
instance (Show a, Show b) => Show (a, b) -- Defined in GHC.Show
that's for a tuple. You can see that tuple has an instance for the Ord
class.
*Prelude> :i ()
data () = () -- Defined in GHC.Unit
instance Bounded () -- Defined in GHC.Enum
instance Enum () -- Defined in GHC.Enum
instance Eq () -- Defined in Data.Tuple
instance Ord () -- Defined in Data.Tuple
instance Read () -- Defined in GHC.Read
instance Show () -- Defined in GHC.Show
and that's for a unit type.
On 3 March 2011 08:09, Karthick Gururaj <karthick.gururaj at gmail.com> wrote:
> Hello,
>
> 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"
> (http://learnyouahaskell.com/types-and-typeclasses#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) ..]
> <interactive>:1:8:
> No instance for (Enum (t, t1))
> arising from the arithmetic sequence `(1, 1) .. '
> at <interactive>:1:8-17
> 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.
>
> 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
>
> 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
> 9
>
> 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
> as well:
>
> Prelude> let addFive' = (+5)
> Prelude> addFive' 3
> 8
>
> 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?
>
> Regards,
> Karthick
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
--
Regards, Paul Sujkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110303/71afc43d/attachment.htm>
More information about the Haskell-Cafe
mailing list