Using Pairs to create linked lists

Pairs seem to be so obscure that I suspect a lot of people aren’t even aware that they exist.
I’m curious whether anyone has used Pairs to implement linked lists. If so, what are the advantages/disadvantages compared to just adding a link property to a class and linking objects that way?
The language reference doesn’t go into a lot of detail on this, and it’s not clear how easy it would be to traverse such a list, or add/delete elements.

1 Like

One disadvantage is that you can’t have both a forward pointer and a backward pointer. I also like that the pointer is part of the object and can be addressed like any other property of the object. Having the Next pointer outside the object feels foreign to me.

2 Likes

Yes, the lack of backward links is a disadvantage, though I did a lot of LISP programming many years ago, and it uses exactly the same construct to build the lists which are its fundamental data structure. I don’t recall the lack of backward links being much of an issue for most things.
I was hoping some people would jump in here and give some examples of things that they do use pairs for, but it looks like there aren’t any. :frowning:

I have used pairs, but not really for their intended purpose. I created a library of complex math functions, and I use a pair to represent a complex number. The left member is the real part and the right member is the imaginary part. The only real benefit of this is that it makes for nice simple syntax when entering constant values into an expression.

I use Pairs a lot. Useful if you are passing around coordinates, e.g. Latitude/Longitude, RA/Declination, Altitude/Azimuth, or key/value pairs, etc. I don’t use them for linked lists. I just use arrays for the most part, since they are easily resizable, easily deletable and insertable, and random-access.

I also created a Tuple class to pass around items with more than two values (although you can’t use the “:” syntactic sugar there). You can use Operator_Subscript though, which makes the tuple element values easy to access.

1 Like