How can I declare an empty tuple?

11,643

Solution 1

A tuple is a type that is always the same length and always has the same types. So (Int, Int) is a different type from (Int, Int, Int) and a different type from (Int, String).

With this in mind, you could have an empty tuple. However, this would just be a type with a single value. This type is written as () and pronounced unit. The only value of this type is also ().

Solution 2

Note that (,) and friends act as functions.

ghci> (,) 2 3
(2,3)

Lists have two constructors: [] and :, which gives them the possibility of emptiness ([] representing "empty"). Tuples, however, only have one constructor each. (,) is the only constructor for the two-tuple type, meaning that tuples do not provide the possibility of emptiness. Whenever you have a two-tuple, you are guaranteed that it actually has two elements in it.

Share:
11,643
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I can declare empty list as [], but, how can I declare empty tuple?

    I have tried:

    for ( , ) 
    ghci>(1,0 ) : [(,)] 
    

    but it gives me an error!