user1709828
user1709828

Reputation:

Add two tuples in SML

Is there a function in SML that adds two tuples of equal lengthes like (3,1,2) and (4,3,1) and returns (7,4,3)?

Thank You

Upvotes: 2

Views: 2113

Answers (1)

sepp2k
sepp2k

Reputation: 370397

No, there is no such function.

There also is no way to write one yourself without hard-coding the length, i.e. you can write a function that takes two tuples of length 2 or a function that takes two tuples of length 3, but it's not possible to write one that takes two tuples of arbitrary (but equal) length. SML's type system simply does not allow you to abstract over a tuple's length like that.

For a specific length, you can, of course, easily define it yourself:

fun addPairs (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)

Upvotes: 2

Related Questions