Replies: 4 comments
-
Two lists of equal content and two arrays of equal content are considered equal. In your code you seem to show you're using sets, that contain an array, and another a list. Are you trying to compare a list to an array? Or two different sets (different by containing types)? Because by definition, they will always be different, just like a string and an int is always different, even if the string contains an integer value. If your intent was indeed to compare different types, then you should use a custom comparison if you want different types to compare equal under certain circumstances. This is true for any dotnet language (but not necessarily any language, famously, Perl can compare different objects equally, which can lead to subtle bugs. Type safe languages usually don't have such feature by default). If I misunderstood your code, could you please show code that explains the bug in a bit more detail?
This statement isn't true, lists and arrays are vastly different and represent different types of data. Lists are a linked data structure that requires traversal, while arrays are structural storage type that can be compared block wise. Lists have O(n) lookup time by index, arrays have O(1) lookup time. Different types of data are represented by each of them. Surely, they look a little bit the same, but that could also be said for sets and maps, or singly linked and doubly linked lists, but in reality they're very different. F# has a way of treating collections in a similar way by using sequences, which can be used with virtually every collection type. |
Beta Was this translation helpful? Give feedback.
-
You misunderstood what I meant. note that The elements order of the two sets ["";"a"] < ["a"] and in array: [|"a"|] < [|"";"a"|] More specifically: (compare ["a"] ["";"a"]) > 0
(compare [|"a"|] [|"";"a"|]) < 0 |
Beta Was this translation helpful? Give feedback.
-
Aha, that certainly clarifies your point, I didn't catch that from the original description (perhaps for other visitors, you could update the original post for clarity?). I agree that it certainly makes sense that these should behave equal. It would be interesting to find out whether this difference is somehow deliberate, whether it's the same in all F# versions and whether similar differences exist in other dotnet collections. |
Beta Was this translation helpful? Give feedback.
-
Note: this is a discussion as changing it would surely be a breaking change that we won't be making I haven't dived into this beyond just looking at the GitHub code so I could be wrong, but this routine may be of interest: fsharp/src/fsharp/FSharp.Core/prim-types.fs Line 934 in 99a1667 Note that arrays and lists are very different structures. An F# list is a discriminated union and has its comparison generated for it. Arrays are not. |
Beta Was this translation helpful? Give feedback.
-
arrays and lists have different comparison rules, I prefer list comparison rules, why their comparison rules are different, I always thought they were the same. Maybe it's not an bug, it's a feature.
Repro steps
Here's the same data, saved in an array and placed in a set. The another is saved with a list and placed in set.
Expected behavior
The comparison rule for array should be the same as comparison rule for list. Because arrays and lists logically represent the same data structure, they are sequences of data elements, and they should behave the same way.
Actual behavior
The comparison rule for array and list are different
Known workarounds
Replace the array with a list When using the comparison feature
Beta Was this translation helpful? Give feedback.
All reactions