Trying to understand function types eg iterate (a -> a)

iterate' :: (a -> a) -> a -> [a] I am trying going to go ahead and write my own iterate function. But before I do I want to be clear on types. Looking at :: (a -> a) -> a -> [a] The first part is (a -> a) Now because it is in parentheses it is a function? I can call iterate like this: take 5 $ iterate (*2) 5 So (*2) is a possible function. Does the brackets mean it is a function, the left hand a is indicating a general type and the right hand a means the return type must be the same as the function type. Eg in the case of (*2) the 2 is an Int so the function returns and Int? Is my understanding correct? How could this be better explained? The last bit is easier to understand. a -> [a] meaning a singleton of general type and [a] means a list of same type.

When you wrap an operator in parentheses and supply just one of its
arguments, then it's called an operator section. It's syntactic sugar for
partially applying the operator, as you can with other functions.
This:
(*2)
is exactly equivalent to:
(\x -> x*2)
and it's type is something like:
Int -> Int
And:
(2:)
is equivalent to:
(\x -> 2:x)
etc
On 31 December 2013 14:46, Angus Comber
iterate' :: (a -> a) -> a -> [a]
I am trying going to go ahead and write my own iterate function. But before I do I want to be clear on types.
Looking at :: (a -> a) -> a -> [a]
The first part is (a -> a) Now because it is in parentheses it is a function?
I can call iterate like this: take 5 $ iterate (*2) 5
So (*2) is a possible function. Does the brackets mean it is a function, the left hand a is indicating a general type and the right hand a means the return type must be the same as the function type. Eg in the case of (*2) the 2 is an Int so the function returns and Int? Is my understanding correct?
How could this be better explained?
The last bit is easier to understand. a -> [a] meaning a singleton of general type and [a] means a list of same type.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Angus,
Your understanding is correct. Parenthesis is need in a -> a to specify
that it is function from a to a because associativity of -> is from right
to left. (*2) is section, you can read more
about here :
http://www.haskell.org/haskellwiki/Section_of_an_infix_operator.
Type of (*2) is (Num a) :: a -> a rather than particular type Int.
Thanks
Divyanshu Ranjan
On Tue, Dec 31, 2013 at 8:16 PM, Angus Comber
iterate' :: (a -> a) -> a -> [a]
I am trying going to go ahead and write my own iterate function. But before I do I want to be clear on types.
Looking at :: (a -> a) -> a -> [a]
The first part is (a -> a) Now because it is in parentheses it is a function?
I can call iterate like this: take 5 $ iterate (*2) 5
So (*2) is a possible function. Does the brackets mean it is a function, the left hand a is indicating a general type and the right hand a means the return type must be the same as the function type. Eg in the case of (*2) the 2 is an Int so the function returns and Int? Is my understanding correct?
How could this be better explained?
The last bit is easier to understand. a -> [a] meaning a singleton of general type and [a] means a list of same type.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Angus Comber
-
divyanshu ranjan
-
Peter Hall