Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between lists and sequences

I'm trying to understand the difference between sequences and lists.

In F# there is a clear distinction between the two. However in C# I have seen programmers refer to IEnumerable collections as a sequence. Is what makes IEnumerable a sequence the fact that it returns an object to iterate through the collection?

Perhaps the real distinction is purely found in functional languages?

like image 456
Peanut Avatar asked Aug 11 '26 23:08

Peanut


1 Answers

Not really - you tend to have random access to a list, as well as being able to get its count quickly etc. Admittedly linked lists don't have the random access nature... but then they don't implement IList<T>. There's a grey area between the facilities provided by a particular platform and the general concepts.

Sequences (as represented by IEnumerable<T>) are read-only, forward-only, one item at a time, and potentially infinite. Of course any one implementation of a sequence may also be a list (e.g. List<T>) but when you're treating it as a sequence, you can basically iterate over it (repeatedly) and that's it.

like image 86
Jon Skeet Avatar answered Aug 14 '26 23:08

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!