
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.

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Yes, it is a type signature. It's "function name :: optional type
constraints => function of type a -> function of type b -> type c"
take' :: Num i => i -> [a] -> [a] = take' is a function whose first
argument is of type i, where the i type must be an instance of the Num
typeclass. The function also takes a list of values of type a (any specific
type), and returns a list of values of the same type.
take' :: (Num i, Ord i) => i -> [a] -> [a] = take' is a function whose
first argument is of type i, where the i type must be an instance of both
the Num and Ord typeclasses. The function also takes a list of values of
type a (any one type), and returns a list of values of the same type.
So the tuple behind the => doesn't mean that one of the arguments is a
tuple, or that the constraints apply to the arguments positionally, but
rather that the specified type, e.g. i, must satisfy some constraints--in
this case implement Ord and Eq so that you can check if it's below 0 (Ord)
and use it as a number (Num.) take' :: i -> [a] -> [a] would have been
valid too, but you wouldn't know if you can compare against i, or use it as
a number.
If you wanted to sort the [a], then indeed, you would need to make it take'
:: (Num i, Ord i, Ord a), since sort from Data.List has a type signature of
sort :: Ord a => [a] -> [a]
On Wed, Apr 10, 2013 at 11:32 AM, Angus Comber
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Sorry, that's "implement Num and Ord", not "implement Ord and Eq"! On Wed, Apr 10, 2013 at 5:39 PM, Patrick Mylund Nielsen < haskell@patrickmylund.com> wrote:
Yes, it is a type signature. It's "function name :: optional type constraints => function of type a -> function of type b -> type c"
take' :: Num i => i -> [a] -> [a] = take' is a function whose first argument is of type i, where the i type must be an instance of the Num typeclass. The function also takes a list of values of type a (any specific type), and returns a list of values of the same type.
take' :: (Num i, Ord i) => i -> [a] -> [a] = take' is a function whose first argument is of type i, where the i type must be an instance of both the Num and Ord typeclasses. The function also takes a list of values of type a (any one type), and returns a list of values of the same type.
So the tuple behind the => doesn't mean that one of the arguments is a tuple, or that the constraints apply to the arguments positionally, but rather that the specified type, e.g. i, must satisfy some constraints--in this case implement Ord and Eq so that you can check if it's below 0 (Ord) and use it as a number (Num.) take' :: i -> [a] -> [a] would have been valid too, but you wouldn't know if you can compare against i, or use it as a number.
If you wanted to sort the [a], then indeed, you would need to make it take' :: (Num i, Ord i, Ord a), since sort from Data.List has a type signature of sort :: Ord a => [a] -> [a]
On Wed, Apr 10, 2013 at 11:32 AM, Angus Comber
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Also, this syntax can be used in general to specify the type of a value. i.e. a :: (c1,c2,...,cn) => t means that the name a has type t, where the type that t represents is constrained by all the constraints ci. e.g. foo :: (Num a, Ord b, Eq a) => (a,b) -> (a -> b) -> (b,b) notes that the type of foo is a function from pairs of a and b to functions from functions from a to b to pairs of a and b. However, the a and b previously noted are subject to the constraints that: - a must be an instance of Num and Eq - b must be an instance of Ord This is useful in many cases, which include: - Documenting the type of a function - Coercing a literal to a specific type (e.g. 23 :: Float) - When checking type inference, using (undefined :: a) as a parameter allows me to check types without having to invent some input - When using type aliases and typeclasses, telling GHCi that your expression has some type will either give you a type error or clean up the type signature for your expression. e.g. :t "foo" is [Char], but :t "foo" :: String is String, so you can check whether the more concise type signature is also correct. Best regards, and wishing you much enjoyment studying Haskell, Gesh
participants (4)
-
Angus Comber
-
Christopher Young
-
Gesh
-
Patrick Mylund Nielsen