piotrek
piotrek

Reputation: 14550

Do tuples use special syntax?

I'm learning haskell. I know that infix operator can be used in two ways:

But what about tuples? I can write: (,) 1 2 but I can't write 1 , 2. Why? Why do I have to use parenthesis and write (1,2)? Is it some kind of special syntax or i'm just missing something?

Upvotes: 5

Views: 137

Answers (1)

user395760
user395760

Reputation:

Yes, tuple syntax is special. Tuple types are special too (syntactic sugar for a data type with a single variant). Note that (a, b, c) and ((a, b), c) and (a, (b, c)) are all distinct types: Unlike ordinary operators, tuple construction is not just a binary operation that can be nested, there are n-ary tuple construction operators for (virtually) any n. Special casing the binary case wouldn't be considered elegant, I suppose.

Trying to allow n-ary tuple construction without parentheses can also make for a more complicated grammar with some surprising corner cases (cf. Python).

Upvotes: 11

Related Questions