"Use the below function with two parameters:
nth :: (a,a,a,a,a) -> Int -> a
where the Int value should return the Int-th value of the five-element tuple." I tried:
nth (a,b,c,d,e) x = (a,b,c,d,e) !! x
But GHC gave me an error message:
file.hs:11:21: error:
* Couldn't match expected type `[a1]'
with actual type `(a, b, c, d, e)'
* In the first argument of `(!!)', namely `(a, b, c, d, e)'
In the expression: (a, b, c, d, e) !! x
In an equation for `nth':
nth (a, b, c, d, e) x = (a, b, c, d, e) !! x
* Relevant bindings include
e :: e (bound at file.hs:11:14)
d :: d (bound at file.hs:11:12)
c :: c (bound at file.hs:11:10)
b :: b (bound at file.hs:11:8)
a :: a (bound at file.hs:11:6)
nth :: (a, b, c, d, e) -> Int -> a1
(bound at file.hs:11:1)
What should I do? How should I write the tuple part of this equation? Thanks for your answers in advance!
nth (a,b,c,d,e) x = [a,b,c,d,e] !! x
. It might not be an efficient implementation but I'll argue n-tuples are not really supposed to be a homogeneous container type, nor efficient random access withInt
anyway. Plus it's easy to type :) – Voltnth :: (a,a,a,a,a) -> Int -> a
and I see no correctness issue. – Volt