Moorani
Moorani

Reputation: 129

Tuples inside Tuples type different or similar?

Is [([(True, False),(True, False)])] equivalent to [((True, False),(True, False))] in terms of typing?

Upvotes: 0

Views: 90

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476537

No. We can query the type in GHCi, and obtain:

Prelude> :t [([(True, False),(True, False)])]
[([(True, False),(True, False)])] :: [[(Bool, Bool)]]
Prelude> :t [((True, False),(True, False))]
[((True, False),(True, False))] :: [((Bool, Bool), (Bool, Bool))]

So the first expression is a list of lists of 2-tuples of Bools; the latter is a list of 2-tuples of 2-tuples of Bools.

A list is not a tuple. In Haskell in a list all elements have the same type, furthermore the length of the list is not fixed at compile time.

A tuple on the other hand has a fixed arity (number of elements), and the type of the elements can vary.

We can also see that in the syntax: (True, False) is a 2-tuple with two booleans, but in the first variant, we put this expression twice between square brackets (hence a list), whereas in the latter, it is between round brackets (hence a tuple, or brackets to "group" an expression).

Upvotes: 6

Related Questions