0

Is there a way to check how many elements (,) has? I know I can access first and second element of a tuple with fst and snd but and thought I could somehow sum elements and then compare it with fst tuple snd tuple and check like this:

tuple = (1,2)

sum tuple == fst tuple + snd tuple

and then I get True for this case and get False for triple = (1,2,3). Eitherway I cant ask fst (1,2,3) nor I can do sum tuple

Is there a way to check if I have a tuple or not?

Something like this:

is_tuple :: (a,b) -> a
is_tuple (a,_) = a

but to get True when I input tuple and False when I give (1,2,3) or (1,2,3,4) and so on... as a input.

i.e:

is_tuple :: Tuple -> Bool
is_tuple x = if x is Tuple 
                then True
                else False

?

cheshire
  • 1,109
  • 3
  • 15
  • 37
  • Would length LISTNAME do what you're looking for? You could use this with an IF to check if you're dealing with a 2 item list or not? – tomh1012 Nov 18 '18 at 22:00
  • 1
    You are already checking that by the type signature at the compile time. – Redu Nov 18 '18 at 22:00
  • @tomh1012 No, I wan't to do this with tuples, triples, quadruples and so on – cheshire Nov 18 '18 at 22:02
  • @Redu I want to check if a argument I give to another function that I wrote is a tuple. If it is a tuple then the Program should continue and if not then I get "not Tuple" or something similar – cheshire Nov 18 '18 at 22:04
  • 3
    No, all the various tuples are distinct and unrelated types. – melpomene Nov 18 '18 at 22:07
  • 1
    Shouldn't you just be using lists instead? – that other guy Nov 18 '18 at 22:08
  • 1
    Strictly typed languages such as Haskell don't work like that. You can not invoke a function with a parameter that doesn't fit the type signature. It won't compile. For such possible to fail cases you are expected to use `Maybe` or `Either` type. – Redu Nov 18 '18 at 22:09
  • 5
    This seems like a prime example of [the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). @cheshire: What are you actually trying to accomplish? – glennsl Nov 18 '18 at 22:10
  • If you checked the type of the variable would that help? https://stackoverflow.com/q/4131552/4677628 – tomh1012 Nov 18 '18 at 22:12
  • 3
    @cheshire First a terminology note: Both `(x,y)` and `(x,y,z)` are tuples. The word for a 2-element tuple is "pair". Now if you have a function that takes a pair as an argument and you try to call it with an argument other than a pair, you will get an error - at compile time. Type checking is already a built-in feature. So the runtime check you are trying to write would be pointless. In the cases where your hypothetical function would return false, the code wouldn't compile in the first place. – sepp2k Nov 18 '18 at 22:40

1 Answers1

7

Is there a way to check how many elements (,) has?

No, because the answer is always 2.

The type (,) is the type constructor for a tuple of two values, or a 2-tuple. It is a distinct type from (,,), the constructor for 3-tuples. Likewise, both those types are distinct from (,,,), the constructor for 4-tuples, and so on.

When you write a function with type (Foo, Bar) -> Baz, the typechecker will reject any attempts to call the function with a tuple of a different number of values (or something that isn’t a tuple at all). Therefore, your isTuple function only has one logical implementation,

isTuple :: (a, b) -> Bool
isTuple _ = True

…since it is impossible to ever actually call isTuple with a value that is not a 2-tuple.

Without using typeclasses, it is impossible in Haskell to write a function that accepts a tuple of arbitrary size; that is, you cannot be polymorphic over the size of a tuple. This is because, unlike lists, tuples are heterogenous—they can contain values of different types. A function that accepts a tuple of varying length would have no way to predict which elements of the tuple are of which type, and therefore it wouldn’t be able to actually do anything useful.

Very rarely, when doing advanced, type-level trickery, it can be useful to have a type that represents a tuple of varying length, which in Haskell is frequently known as an HList (for heterogenous list). These can be implemented as a library using fancy typeclass machinery and type-level programming. However, if you are a beginner, this is definitely not what you want.

It is difficult to actually give advice on what you should do because, as a commenter points out, your question reads like an XY problem. Consider asking a different question that gives a little more context about the problem you were actually trying to solve that made you want to find the list of a tuple in the first place, and you’ll most likely get more helpful answers.

Alexis King
  • 43,109
  • 15
  • 131
  • 205