I have appreciated named fields of D tuples since the beginning, I have found them quite handy. With them sometimes you don't need to unpack a tuple, you can just access its fields with a nice name, avoiding to move around more than one variable.

In Haskell to solve this problem there is the @ syntax for tuples. As an example usage, this Haskell function vNorm normalizes a Vec (a 2D vector). Its input is a Vec, that is unpacked in its x and y fields, but vNorm al gives a name to the whole input Vec, naming it 'v'. So you are able to call the other function vLen with no need to pack again x and y in a Vec:

vLen :: Vec -> Double
vLen x = sqrt $ vDot x x

vNorm :: Vec -> Vec
vNorm v@(Vec x y) = Vec (x / l) (y / l) where l = vLen v

(In my first post in this thread I have not listed such extra features for tuples because I think they are less important than a good unpacking syntax in those four cases.)

Bye,
bearophile

Reply via email to