simple x y z = x * (y + z)
From what we have learnt so far:
[(2, 3), (4, 5)] :: [(Integer, Integer)]
['z', 42] :: {- Error: different types cannot be mixed in a list -}
('z', -42) :: (Char, Integer)
simple 'a' 'b' 'c' :: {- Error: simple cannot be called with Char types -}
(simple 1 2 3, simple) :: (Integer, Integer -> Integer -> Integer -> Integer)
Hugs gives a slightly more general type for these:
[(2, 3), (4, 5)] :: (Num a, Num b) => [(a,b)]
('z', -42) :: Num a => (Char,a)
(simple 1 2 3, simple) :: (Num a, Num b) => (b,a -> a -> a -> a)
3 responses to “Exercise 1.3”
For the last example, would anyone mind explaining how a function with no arguments can be in a tuple with a function with arguments? How does this result in three successive outputs? Why do there seem to be only two inputs, when there should be three?
I’m just starting to learn too, but I’ll try to explain:
(Num a, Num b) => (b,a -> a -> a -> a)
this can be read as: being a of type Num and b of type Num, then the type is (b,a -> a -> a -> a)
which is the same as saying that the type is (Num,Num -> Num -> Num -> Num)
The elements of a tuple don’t have to be of the same value.
(simple 1 2 3, simple) is a tuple composed of:
– in the first position, a value of type Num. This is a value because ‘simple 1 2 3’ contains all the inputs defined as values, so ‘simple 1 2 3’ is calculated and the result ‘5’ is used instead.
– in the second position there is a function with 3 inputs and 1 output, all of type Num. In this case the inputs are not defined and thus ‘simple’ can’t be calculated, ‘simple’ is then just the definition of the function, which type is
(Num a) => a -> a -> a -> a
There is a simpe explanation.
In the book there is said that the function simple is of type:
simple :: Integer->Integer->Integer->Integer
and
simple x y z = x*(y+z) (its output is Integer)
So (simple 1 2 3, simple) – simple 1 2 3 is Integer because it can be calculated
simple can not be calculated because no inputs are defined, so its type remains Integer->Integer->Integer->Integer