I am working through Graham Hutton’s Programming in Haskell, and an exercise in Chapter 3 asks “What is the type?” for the function twice f x = f (f x).
I think I understand why the answer is twice :: (t -> t) -> t -> t. (Edit: I did not understand why. See my comment on Paolo’s answer.) However, to experiment I wrote another function thrice f x = f (f (f x)).
What I definitely don’t understand is why thrice also has a type of thrice :: (t -> t) -> t -> t.
They work the way I would expect (see below), but I can’t see how the type of thrice makes sense.
From ghci:
>> twice tail [0,1,2,3,4]
[2,3,4]
>> thrice tail [0,1,2,3,4]
[3,4]
Sorry, maybe I don’t understand your question, but if you look at your own example with list, you’ll see that in both twice and thrice’s case, the inputs are a function from list to list (
tail) and a list ([0,1,2,3,4]) and the return type is a list.So both
twiceandthricematch the signature(t -> t) -> t -> t: a function from t to t (in your casetail), a t (in your case a list) and another t (list) in return