Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Type of function or type of the output of    function?
      (Matthew Moppett)


----------------------------------------------------------------------

Message: 1
Date: Sat, 8 Dec 2012 21:02:15 +1100
From: Matthew Moppett <[email protected]>
Subject: Re: [Haskell-beginners] Type of function or type of the
        output of       function?
To: [email protected], [email protected]
Message-ID:
        <camlejzbev1tfonnp22jotqoz-cj1xgxvkpcr8b852zjdvhq...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

As Divyanshu said, (Num a, Ord a) => [a] is the type of a value, not of a
function.

[a] just means "A list of values having some type (could be anything,
really)".
(Num a, Ord a) => means "Whatever type *a* has, that type is a member of
the Num typeclass and of the Ord typeclass" -- these are called type
constraints.

*a* has to be a member of the Ord typeclass (the class of types that can be
compared in value -- either equal, smaller than, or less than) because
you've used a function like (<=) or (>) in your quicksort function that
makes those sorts of comparisons.

*a* has to be a member of the Num typeclass because you've used number
literals in your list. These could be Ints, Integers, Floats... but ghci
has no way of knowing which one.

If you do this:
>let a = 3 :: Int
>:t quicksort [a,2,1,-10]

You'll get back this result:
quicksort [a,2,1,-10] :: [Int]

In this case ghci now knows your talking about a specific type (Int), so it
doesn't need to specify type constraints.





On Sat, Dec 8, 2012 at 8:11 PM, Trung Quang Nguyen <[email protected]>wrote:

> Hi there,
>
> I have a quickSort implementation, then I try to use :t in ghci to inspect
> what type I receive. I got this.
>
> *Main> :t quickSort "Hello the world"
> quickSort "Hello the world" :: [Char]
> *Main> :t quickSort [3,2, 1,-10]
> quickSort [3,2, 1,-10] :: (Num a, Ord a) => [a]
>
> The first one looks like :t return type of output of quickSort [Char]
>
> The second one looks like :t return the type of function (Num a, Ord a) =>
> [a]
>
> Anybody know why this happens? When we will receive type of output, or
> type of function? And why (Num a, Ord a) => [a], even quickSort doesn't
> have explicit type, so this type comes from input type and type of function
> is defined in runtime?
>
> Thanks a lot!
>
> Cheers,
> Trung
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20121208/e64bae42/attachment-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 54, Issue 12
*****************************************

Reply via email to