[Haskell-beginners] Haskell type definitions

Christopher Young chris at chrisyoung.net
Wed Apr 10 17:38:41 CEST 2013


This line

take' :: (Num i, Ord i) => i -> [a] -> [a]

just means that take' is a function takes an i and a list of a's and
returns a list of a's. The

(Num i, Ord i) =>

bit just gives you a constraint on what i can be: it must implement both
the Num and the Ord typeclasses. If you call take' with a string and a list
it will fail, e.g., since strings don't implement those typeclasses.

I'm just starting out too, so please correct me if I'm wrong, people.

Best,
Chris


On Wed, Apr 10, 2013 at 5:32 AM, Angus Comber <anguscomber at gmail.com> wrote:

> I am learning Haskell using Learn You a Haskell.  On page 54 is an
> implementation of take like so:
>
>     take' :: (Num i, Ord i) => i -> [a] -> [a]
>     take' n _
>         | n <= 0 = []
>     take' _ []   = []
>     take' n (x:xs) = x : take' (n-1) xs
>
> I understand all the code apart from the first line.
>
> The :: part I understand as meaning this is a type definition?
>
> (Num i, Ord i) is a tuple.  The first element of the tuple has to be
> numeric, fair enough.  the second param has to be able to be ordered.
> The parameter is the same - both are i.  This means that the types
> have to be the same?
>
> Why is it not (Num i, Ord j)?  Isn't the 2nd tuple element
> referring to the list?  Which could be of any type?
>
> What does => signify?
>
> i -> [a] -> [a] means first parameter is numeric?  2nd param is any
> type list, 3rd param is any type list.  So this is saying first param
> numeric, 2nd param a list of any type and it returns a list of any
> type.  Well that is understandable I suppose.
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130410/1e119361/attachment.htm>


More information about the Beginners mailing list